使用代理:在请求方法中,传递proxies参数就可以了
import requests
proxy = {
'http': '123.9.149.25:8118'
}
response = requests.get("http://httpbin.org/ip", proxies=proxy)
print(response.text)
输出结果:
{
"origin": "123.9.149.25"
}
处理cookies:
想要在多次请求中共享cookies,那么应该使用session:
import requests
login_url = "http://www.renren.com/PLogin.do"
data = {
'email': 'xxxxx@qq.com',
'password': 'xxxxxx'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
session = requests.Session()
session.post(login_url, data=data, headers=headers)
response=session.get('http://www.renren.com/880151247/profile')
with open('renren.html','w',encoding='utf-8') as fp:
fp.write(response.text)