File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ Urllib2:data为string
4545```
4646
4747### 2. 对于反爬虫机制的处理
48+
4849** 2.1 模拟登陆情况 **
4950这种属于post请求情况,先向服务器发送表单数据,服务器再将返回的cookie存入本地。
5051```
@@ -57,7 +58,8 @@ Urllib2:data为string
5758 data = urllib.urlencode(data) # 编码工作,由dict转为string
5859 req = urllib2.Request(url=url, data=data) # POST请求发送,可用于用户名密码登陆情况
5960 response = urllib2.urlopen(req)
60- ```
61+ ```
62+
6163** 2.2 使用cookie登陆情况 **
6264使用cookie登陆,服务器会认为你是一个已登陆的用户,所以就会返回给你一个已登陆的内容。因此,需要验证码的情况可以使用带验证码登陆的cookie解决。
6365```
@@ -73,7 +75,8 @@ response2 = requests_session.get(url_login) # 已登陆,因为之前拿到了R
7375response3 = requests_session.get(url_results) # 已登陆,因为之前拿到了Response Cookie!
7476```
7577相关参考:[ 网络爬虫-验证码登陆] ( http://www.lining0806.com/6-%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB-%E9%AA%8C%E8%AF%81%E7%A0%81%E7%99%BB%E9%99%86/ )
76- 参考项目:[ 爬取知乎网站] ( https://github.com/lining0806/ZhihuSpider )
78+ 参考项目:[ 爬取知乎网站] ( https://github.com/lining0806/ZhihuSpider )
79+
7780** 2.3 伪装成浏览器,或者反“反盗链” **
7881```
7982headers = {'User-Agent':'XXXXX'} # 伪装成浏览器访问,适用于拒绝爬虫的网站
You can’t perform that action at this time.
0 commit comments