安装
request常用方法
响应对象response的方法
- response.text() 返回Unicode格式的数据
- response.content 返回字节流数据(二进制)
- response.content.decode(‘utf-8’) 手动解码
- response.url 返回url
- response.encode()=‘编码’
import requests
response=requests.get("https://www.baidu.com/")
print(response.status_code)
print(response.url)
print(response.encoding)
print(response.content.decode('utf-8'))
print(response.content)
print(response.text)
get请求
import requests
url="https://www.baidu.com/s"
params={"wd":"中国"}
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0"
}
response=requests.get(url=url,params=params,headers=headers)
with open('baidu.html','w',encoding='utf-8') as fp:
fp.write(response.content.decode('utf-8'))
print(response.url)
post请求
- 格式
response=requests.post(url,data=data)
- 拉钩网为例
import requests
data={
"first":"true",
"kd":"true",
"pn":1
}
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Referer":"https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput="
}
response=requests.post("https://www.lagou.com/jobs/companyAjax.json?needAddtionalResult=false&first=true&kd=python&pn=1",data=data,headers=headers)
print(response.json())