硒渣

scrapy-selenium的Python项目详细描述


#硒渣[pypi](https://img.shields.io/pypi/v/scrapy selenium.svg)(https://pypi.python.org/pypi/scrapy-selenium)[![构建状态](https://travis-ci.org/clemfromspace/scrapy-selenium.svg?branch=master)(https://travis-ci.org/clemfromspace/scrapy-selenium)[![测试覆盖率](https://api.codeculate.com/v1/badges/5c737098dc38a835ff96/test-coverage)(https://codeculate.com/github/clemfromspace/scrapy-selenium/test-coverage)[![可维护性](https://api.codeculate.com/v1/badges/5c737098dc38a835ff96/maintability)(https://codeculate.com/github/clemfromspace/scrapy selenium/maintability)


scrapy中间件处理使用selenium的javascript页面。

=3.6**。
您还需要selenium[兼容浏览器]之一(http://www.seleniumhq.org/about/platforms.jsp)。

添加要使用的浏览器、驱动程序可执行文件的路径,以及要传递给可执行文件的参数到scrapy设置:
``python
from shuil import which

selenium驱动程序可执行程序路径=which('geckodriver')
selenium驱动程序参数=['-headless'].'--headless'如果使用chrome而不是firefox
```

或者,设置浏览器可执行文件的路径:
``python
selenium\u browser\u executable\u path=which('firefox')
````

2。将“seleniummiddleware”添加到下载程序中间软件:
``python
downloader\u middleware={
`scrapy\u selenium.seleniummiddleware':800
}
``````
`usage
使用“scrapy\u selenium.selenium request”而不是下面这样的“scrapy内置”请求:
``python
selenium request

yield seleniumrequest(url,self.parse-result)
`````
请求将由selenium处理,请求将有一个名为“driver”的附加“meta”键,其中包含处理请求的selenium驱动程序。
``python
def parse-result(self,响应:
打印(response.request.meta['driver'].title)
````
有关可用驱动程序方法和属性的详细信息,请参阅[selenium python文档](http://selenium python.readthedocs.io/api.html module selenium.webdriver.remote.webdriver)

selector响应属性正常工作(但包含由selenium驱动程序处理的html)。
``python
def parse廑result(self,响应:
print(response.selector.xpath('///title/@text'))
````



``scrapy_selenium.seleniumrumrequest`接受4个附加参数:


` wait `//`wait `


br/>selenium使用时,将执行[显式等待](http://selenium-python.readthedocs.io/waits/waits)selenium,seleni.html(显式等待)在返回对蜘蛛的响应之前。
``python
from selenium.webdriver.common.by import by
from selenium.webdriver.support import expected_conditions as ec


yield seleniumrequest(
url=url,
callback=self.parse_result,
wait_time=10,
wait_until=ec.element_to_be可单击((by.id,‘someid’))


````

`
``截图`
``使用时,硒会截图一页,捕捉到的.png二进制数据会添加到响应``meta`:
```python
yielseleniumrequest(
url=url,
callbackback=self.parse=self.parse ` result,
screenshot=true





def parse `` result ````解析结果`
(自我,响应:
以open('image.png','wb')作为图像文件:
image_file.write(response.meta['screenshot'])
`````

````python
yield seleniumrequest(
url,
self.parse嫒result,
script=window.scrollto(0,document.body.scrollHeight);',





欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
添加组件后,java JTable为空   java将json发送到php并插入mysql,但返回null   java Spring引导JNDI CommonJ资源   从不同PC创建和合并后的Java servlet问题   java如何在使用findelements时从xpath获取文本   java使用spring boot使用gmail smtp发送电子邮件   java在不使用pojo、bean或getter和setter的情况下获取Json标题的Json数组   Java中的OpenFile对话框将null作为响应   JavaBuilder模式。扩展接口   java中无需替换的数据结构选取   java如何评价Encog中的预测神经网络   java如何在安卓中使用实际的HttpURLConnection进行单元测试?   java使用XML配置禁用WebSocket中的CSRF保护   java如何通过hibernate从多表查询中获取数据?   mysql如何在java中获取更新的行Id   java AEM/CQ组件单一组件/有限组件   java FFmpeg Javacv延迟问题   显示整数数组的java不起作用