python requests模块
一、方法
import requests
# get请求
r = requests.get(url=url)
# post 请求
r = requests.post(url=url)
# put请求
r = requests.put(url=url)
# delete请求
r = requests.delete(url=url)
# head请求
r = reuqests.head(url=url)
# options
r = requests.options(url=url)
二、传递参数
import requests
headers = {"User-Agent": "Mozilla/5.0"}
url = "http://www.baidu.com/s?"
key = input("请输入要搜索的内容:")
params = {"word": key}
res = requests.get(url, headers=headers, params=params)
# 指定编码utf-8
res.enconding = "utf-8"
result = res.text
print(result)
三、响应内容
import requests
r = requests.get(url=url, headers=headers)
r.encoding #获取当前的编码
r.encoding = 'utf-8' #设置编码
r.text #以encoding解析返回内容。字符串方式的响应体,会自动根据响应头部的字符编码进行解码。
r.content #以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
r.headers #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
r.status_code #响应状态码
r.raw #返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()
r.ok # 查看r.ok的布尔值便可以知道是否登陆成功
#*特殊方法*#
r.json() #Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常
r.raise_for_status() #失败请求(非200响应)抛出异常
import requests
url = "http://www.baidu.com/"
headers = {"User-Agent":"Mozilla/5.0"}
# 发请求获响应
response = requests.get(url,headers=headers)
response.encoding = "utf-8"
# 获取字符串
print(type(response.text))
# 获取字节流
print(type(response.content))
# 返回服务器响应码
print(response.status_code)
# 返回数据的URL
print(respone.url)
爬取图片
import requests
url = "http://dingyue.nosdn.127.net/LftiDijLShX6y8NWnEI606fo0TJmFMRIByj4HniV9GypR1539753512019.jpg"
headers = {"User-Agent":"Mozilla/5.0"}
res = requests.get(url,headers=headers)
html = res.content
with open("xxx.jpg","wb") as f:
f.write(html)
print("图片下载成功")
四、post请求
import requests
import json
# 请输入你要翻译的内容
key = input("请输入要翻译的内容:")
# post方法要求data为字典格式
data = {"i": key,
"from":"AUTO",
"to":"AUTO",
"smartresult":"dict",
"client":"fanyideskweb",
"salt":"1540373170893",
"sign":"a5d9b838efd03c9b383dc1dccb742038",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_REALTIME",
"typoResult":"false"
}
# 发请求,获取响应
# url为POST的地址,抓包工具抓到的,此处去掉 _o
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
headers = {"User-Agent":"Mozilla/5.0"}
# 此处data为form表单数据
res = requests.post(url,data=data,headers=headers)
res.encoding = "utf-8"
html = res.text
# 把json格式字符串转换为Python中字典
r_dict = json.loads(html)
result = r_dict['translateResult'][0][0]["tgt"]
print(result)