登录  /  注册

Python利用Requests库写爬虫的实例详解

零下一度
发布: 2017-06-30 18:00:56
原创
2489人浏览过

基本get请求:

#-*- coding:utf-8 -*-import requests
url = 'www.baidu.com'r = requests.get(url)print r.text
登录后复制

带参数Get请求:

#-*- coding:utf-8 -*-import requests
url = 'http://www.baidu.com'payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get(url, params=payload)print r.text
登录后复制

POST请求模拟登陆及一些返回对象的方法:

#-*- coding:utf-8 -*-import requests
url1 = 'www.exanple.com/login'#登陆地址url2 = "www.example.com/main"#需要登陆才能访问的地址data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",            "Accept-Encoding":"gzip",            "Accept-Language":"zh-CN,zh;q=0.8",            "Referer":"www.example.com/",            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
            }
res1 = requests.post(url1, data=data, headers=headers)
res2 = requests.get(url2, cookies=res1.cookies, headers=headers)print res2.conten
登录后复制

t#获得二进制响应内容print res2.raw#获得原始响应内容,需要stream=Trueprint res2.raw.read(50)print type(res2.text)#返回解码成unicode的内容print res2.urlprint res2.history#追踪重定向print res2.cookiesprint res2.cookies['example_cookie_name']print res2.headersprint res2.headers['Content-Type']print res2.headers.get('content-type')print res2.json#讲返回内容编码为jsonprint res2.encoding#返回内容编码print res2.status_code#返回http状态码print res2.raise_for_status()#返回错误状态码

使用Session()对象的写法(Prepared Requests):

#-*- coding:utf-8 -*-import requests
s = requests.Session()
url1 = 'www.exanple.com/login'#登陆地址url2 = "www.example.com/main"#需要登陆才能访问的地址data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",            "Accept-Encoding":"gzip",            "Accept-Language":"zh-CN,zh;q=0.8",            "Referer":"http://www.example.com/",            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
            }
prepped1 = requests.Request('POST', url1,
    data=data,
    headers=headers
).prepare()
s.send(prepped1)'''
也可以这样写
res = requests.Request('POST', url1,
data=data,
headers=headers
)
prepared = s.prepare_request(res)
# do something with prepped.body
# do something with prepped.headers
s.send(prepared)
'''prepare2 = requests.Request('POST', url2,
    headers=headers
).prepare()
res2 = s.send(prepare2)print res2.content
登录后复制

另一种写法 :

#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'www.exanple.com/login'#登陆地址url2 = "www.example.com/main"#需要登陆才能访问的页面地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",            "Accept-Encoding":"gzip",            "Accept-Language":"zh-CN,zh;q=0.8",            "Referer":"http://www.example.com/",            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"            }res1 = s.post(url1, data=data)res2 = s.post(url2)print(resp2.content)
SessionApi
其他的一些请求方式
>>> r = requests.put("http://httpbin.org/put")>>> r = requests.delete("http://httpbin.org/delete")>>> r = requests.head("http://httpbin.org/get")>>> r = requests.options("http://httpbin.org/get")
登录后复制

遇到的问题:

在cmd下执行,遇到个小错误:

UnicodeEncodeError:'gbk' codec can't encode character u'\xbb' in position 23460: illegal multibyte sequence

分析:
1、Unicode是编码还是解码

UnicodeEncodeError

很明显是在编码的时候出现了错误

2、用了什么编码

'gbk' codec can't encode character

使用GBK编码出错

解决办法:

确定当前字符串,比如

#-*- coding:utf-8 -*-import requests
url = 'www.baidu.com'r = requests.get(url)print r.encoding
>utf-8
登录后复制

已经确定html的字符串是utf-8的,则可以直接去通过utf-8去编码。

print r.text.encode('utf-8')

以上就是Python利用Requests库写爬虫的实例详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号