|
| 1 | +# 浏览器渲染 |
| 2 | + |
| 3 | +采集动态页面时(Ajax渲染的页面),常用的有两种方案。一种是找接口拼参数,这种方式比较复杂但效率高,需要一定的爬虫功底;另外一种是采用浏览器渲染的方式,直接获取源码,简单方便 |
| 4 | + |
| 5 | +框架内置一个浏览器渲染池,默认的池子大小为1,请求时重复利用浏览器实例,只有当代理失效请求异常时,才会销毁、创建一个新的浏览器实例 |
| 6 | + |
| 7 | +## 使用方式: |
| 8 | + |
| 9 | +```python |
| 10 | +def start_requests(self): |
| 11 | + yield feapder.Request("https://news.qq.com/", render=True) |
| 12 | +``` |
| 13 | +在返回的Request中传递`render=True`即可 |
| 14 | + |
| 15 | +框架支持`CHROME`和`PHANTOMJS`两种浏览器渲染,可通过[配置文件](source_code/配置文件)进行配置。相关配置如下: |
| 16 | + |
| 17 | +```python |
| 18 | +# 浏览器渲染 |
| 19 | +WEBDRIVER = dict( |
| 20 | + pool_size=1, # 浏览器的数量 |
| 21 | + load_images=True, # 是否加载图片 |
| 22 | + user_agent=None, # 字符串 或 无参函数,返回值为user_agent |
| 23 | + proxy=None, # xxx.xxx.xxx.xxx:xxxx 或 无参函数,返回值为代理地址 |
| 24 | + headless=False, # 是否为无头浏览器 |
| 25 | + driver_type="CHROME", # CHROME 或 PHANTOMJS, |
| 26 | + timeout=30, # 请求超时时间 |
| 27 | + window_size=(1024, 800), # 窗口大小 |
| 28 | + executable_path=None, # 浏览器路径,默认为默认路径 |
| 29 | + render_time=0, # 渲染时长,即打开网页等待指定时间后再获取源码 |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +`feapder.Request` 也支持`render_time`参数, 优先级大于配置文件中的`render_time` |
| 34 | + |
| 35 | +## 设置User-Agent |
| 36 | + |
| 37 | +> 每次生成一个新的浏览器实例时生效 |
| 38 | +
|
| 39 | +### 方式1: |
| 40 | + |
| 41 | +通过配置文件的 `user_agent` 参数设置 |
| 42 | + |
| 43 | +### 方式2: |
| 44 | + |
| 45 | +通过 `feapder.Request`携带,优先级大于配置文件, 如: |
| 46 | + |
| 47 | +```python |
| 48 | +def download_midware(self, request): |
| 49 | + request.headers = { |
| 50 | + "User-Agent": "xxxxxxxx" |
| 51 | + } |
| 52 | + return request |
| 53 | +``` |
| 54 | + |
| 55 | +## 设置代理 |
| 56 | + |
| 57 | +> 每次生成一个新的浏览器实例时生效 |
| 58 | +
|
| 59 | +### 方式1: |
| 60 | + |
| 61 | +通过配置文件的 `proxy` 参数设置 |
| 62 | + |
| 63 | +### 方式2: |
| 64 | + |
| 65 | +通过 `feapder.Request`携带,优先级大于配置文件, 如: |
| 66 | + |
| 67 | +```python |
| 68 | +def download_midware(self, request): |
| 69 | + request.proxies = { |
| 70 | + "http": "http://xxx.xxx.xxx.xxx:xxxx" |
| 71 | + } |
| 72 | + return request |
| 73 | +``` |
| 74 | + |
| 75 | +或者 |
| 76 | + |
| 77 | +```python |
| 78 | +def download_midware(self, request): |
| 79 | + request.proxies = { |
| 80 | + "https": "https://xxx.xxx.xxx.xxx:xxxx" |
| 81 | + } |
| 82 | + return request |
| 83 | +``` |
| 84 | + |
| 85 | +## 设置Cookie |
| 86 | + |
| 87 | +通过 `feapder.Request`携带,如: |
| 88 | + |
| 89 | +```python |
| 90 | +def download_midware(self, request): |
| 91 | + request.headers = { |
| 92 | + "Cookie": "key=value; key2=value2" |
| 93 | + } |
| 94 | + return request |
| 95 | +``` |
| 96 | + |
| 97 | +或着 |
| 98 | + |
| 99 | +```python |
| 100 | +def download_midware(self, request): |
| 101 | + request.cookies = { |
| 102 | + "key": "value", |
| 103 | + "key2": "value2", |
| 104 | + } |
| 105 | + return request |
| 106 | +``` |
| 107 | + |
| 108 | +## 操作浏览器对象 |
| 109 | + |
| 110 | +通过 `response.browser` 获取浏览器对象 |
| 111 | + |
| 112 | +代码示例:请求百度,搜索feapder |
| 113 | + |
| 114 | +```python |
| 115 | +import time |
| 116 | + |
| 117 | +import feapder |
| 118 | +from feapder.utils.webdriver import WebDriver |
| 119 | + |
| 120 | + |
| 121 | +class TestRender(feapder.AirSpider): |
| 122 | + def start_requests(self): |
| 123 | + yield feapder.Request("http://www.baidu.com", render=True) |
| 124 | + |
| 125 | + def parse(self, request, response): |
| 126 | + browser: WebDriver = response.browser |
| 127 | + browser.find_element_by_id("kw").send_keys("feapder") |
| 128 | + browser.find_element_by_id("su").click() |
| 129 | + time.sleep(5) |
| 130 | + print(browser.page_source) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + TestRender().start() |
| 135 | + |
| 136 | +``` |
0 commit comments