requests:
官方文档:http://cn.python-requests.org/zh_CN/latest/
get请求:
requests.get(url)
定制头部:requests.get(url,headers=headers,params=data)
相应对象:
r.text :字符串形式查看响应
r.content :字节类型查看响应
r.encoding :查看或设置编码类型
r.status_code:查看状态码
r.headers :查看响应头部
r.url :查看请求的url
r.json() :查看json数据
简单实例:
import requests
url='https://www.baidu.com/s'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
data = {
'ie':'utf8',
'kw':'资源管理站',
}
# parameters:参数的意思
r=requests.get(url,headers=headers,params=data)
print(r.url)
print(r.status_code)
#设置编码:
r.encoding='utf8'
with open('baidu.html','wb') as fp:
fp.write(r.content)
post请求:
r=requests.post(url,headers=headers,data=data)
简单实例:
import requests
url='https://cn.bing.com/ttranslationlookup?&IG=5DE53400113440E2BBD53D54FE5B1685&IID=translator.5038.4'
text=input('请输入你要翻译的英文单词:')
data = {
'from':'en',
'to':'zh-CHS',
'text':text,
}
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
r=requests.post(url,headers=headers,data=data)
r.json()
print(r.text)
设置代理:
proxies={'http':'http://125.126.192.172:9999',}
r=requests.get(url,headers=headers,proxies=proxies)
import requests
url='https://www.baidu.com/s?wd=IP'
proxies={
'http':'http://125.126.192.172:9999',
}
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
r=requests.get(url,headers=headers,proxies=proxies)
with open('daili.html','wb') as fp:
fp.write(r.content)
print(r.text)
cookie
#碰到回话相关的问题,首先要创建一个回话
s=requests.Session()
#后面所有的操作,全部使用s.post,s.get操作
import requests
#碰到回话相关的问题,首先要创建一个回话
#后面所有的操作,全部使用s.post,s.get 操作
s=requests.Session()
post_url='http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019161145603'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
formdata = {
'email':'hoochon@163.com',
'icode':'',
'origURL':'http://www.renren.com/home',
'domain':'renren.com',
'key_id':'1',
'captcha_type':'web_login',
'password':'111111',
'rkey':'08dfc31399cf0baa6abdfeb324016e2b',
'f':'',
}
r=s.post(url=post_url,headers=headers,data=formdata)
print(r.text)
#登陆后再次请求个人主页
get_url='http://www.renren.com/1868758764/profile'
r=s.get(url=get_url,headers=headers)
with open('renren.html','wb') as fp:
fp.write(r.content)