python学习之6 requests模拟浏览器抓数据

来源:互联网 发布:网络神学院网站 编辑:程序博客网 时间:2024/06/07 20:44

python学习之6 requests模拟浏览器抓数据

在爬网站时,有时候会遇到返回500,被服务器拒绝的情况。
需要做的是,模拟浏览器登录。
即增加,headers

headers = {    'Host': 'blog.csdn.net',    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',    'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',    'Accept-Encoding': 'gzip, deflate',    'Referer': 'http://www.baidu.com',    'Connection': 'keep-alive',    'Cache-Control': 'max-age=0',}

其中referer比较重要,即来源网站。用百度谷歌,一般就可以。

使用时候如下,

练习源码

#coding = utf-8##################################################### coding by 刘云飞####################################################import requestsresponse = requests.get(blog, headers=headers)print(response.status_code) 
0 0
原创粉丝点击