Python爬虫基础(四)Requests库的使用

时间:2021-08-17 08:52:27

requests文档

首先需要安装:pip install requests

get请求

最基本的get:

# -*- coding: utf-8 -*-
import requests
response = requests.get("http://www.baidu.com/")

添加headers及查询参数:

# -*- coding: utf-8 -*-
import requests kw = {'wd':'秦时明月'}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} # params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers) # 查看响应内容,response.text 返回的是Unicode格式的数据
print response.text # 查看响应内容,response.content返回的字节流数据
print respones.content # 查看完整url地址
print response.url # 查看响应头部字符编码
print response.encoding # 查看响应码
print response.status_code

post请求

最基本的post:

# -*- coding: utf-8 -*-
import requests
response = requests.post("http://www.baidu.com/", data = data)

传入查询参数:

使用有道翻译的例子

# -*- coding: utf-8 -*-
import requests formdata = {
"i":" python",
"from":"AUTO",
   "to":"AUTO",
"smartresult":" dict",
   "client":" fanyideskweb",
   "salt":"",
   "sign":" 2a6d78290492d163dbd6803b29e2489c",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_ENTER",
"typoResult":"true"
} url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"} response = requests.post(url, data = formdata, headers = headers) print response.text # 如果是json文件可以直接显示
print response.json()

设置代理

# -*- coding: utf-8 -*-
import requests # 根据协议类型,选择不同的代理
proxies = {
"http": "http://12.34.56.79:9527",
"https": "http://12.34.56.79:9527",
}
# 私密代理
# proxy = { "http": "账户:密码@61.158.163.130:16816" }
response = requests.get("http://www.baidu.com", proxies = proxies)
print response.text

web客户端验证

# -*- coding: utf-8 -*-
import requests auth=('账户', '密码') response = requests.get('http://192.168.23.19', auth = auth) print response.text

比urllib2简单很多

cookie

如果一个响应中包含了cookie,那么我们可以利用 cookies参数拿到:

# -*- coding: utf-8 -*-
import requests response = requests.get("http://www.baidu.com/") # 返回CookieJar对象:
cookiejar = response.cookies # 将CookieJar转为字典:
cookiedict = requests.utils.dict_from_cookiejar(cookiejar) print cookiejar print cookiedict

模拟登录:

# -*- coding: utf-8 -*-
import requests # 创建session对象,可以保存Cookie值
ssion = requests.session()
# 需要登录的用户名和密码
data = {"email":"账户", "password":"密码"} # 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里
ssion.post("http://www.renren.com/PLogin.do", data = data) # ssion包含用户登录后的Cookie值,可以直接访问那些登录后才可以访问的页面
response = ssion.get("http://www.renren.com/410046543/profile") print response.text

处理SSL验证

# -*- coding: utf-8 -*-
import requests
response = requests.get("https://www.baidu.com/", verify=True) # 也可以省略不写
# response = requests.get("https://www.baidu.com/")
# 如果SSL证书验证不通过,或者不信任服务器的安全证书,则会报出SSLError
print response.text # 如果我们想跳过 12306 的证书验证,把 verify 设置为 False 就可以正常请求了
import requests
response = requests.get("https://www.12306.cn/mormhweb/", verify = False)
print response.text