We have already explained urllib Python built-in module for accessing network resources. However, it uses up more trouble, and the lack of many useful advanced features. The better solution is to use requests. It is a Python third party libraries , URL processing resources, especially convenient.
Installation requests
If the Anaconda installation, requests had been available. Otherwise, you need to install by pip at the command line:$ pip install requests
requests_get
Access a page via GET
import requests
#带参数的get请求
url ='https://www.baidu.com/s?'
data ={'wd':'中国'}
header ={'User-Agent':'Mozilla/5.0 (X11; U; Linux x86_64;'' zh-CN; rv:1.9.2.10) Gecko/20100922'' Ubuntu/10.10 (maverick) Firefox/3.6.10'}
r = requests.get(url, headers=header, params=data)#print(r.text)#print(r.status_code)#print(r.headers)#print(r.url)withopen('Requests_file/zhongguo.html','wb')as fp:
fp.write(r.content)
requests_cookie
import requests
#创建一个会话
s = requests.Session()
post_url ='http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019341636849 HTTP/1.1'
header ={'User-Agent':'Mozilla/5.0 (X11; U; Linux x86_64;'' zh-CN; rv:1.9.2.10) Gecko/20100922'' Ubuntu/10.10 (maverick) Firefox/3.6.10'}
formdata ={'email':'17320015926','password':'123456','icode':'','origURL':'http://www.renren.com/home','domain':'renren.com','key_id':'1','captcha_type':'web_login','f':'https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3D_4eOtFSXfVrfNtOlNBgoyTjnVMk2CRdO44Rf-7VG4AG%26wd%3D%''26eqid%3D8b5865030001e71f000000035caefb80',}
r = s.post(url=post_url, headers=header, data=formdata)#print(r.text)
get_url ='http://www.renren.com/969564068/profile'
r = s.get(url=get_url, headers=header)print(r.text)withopen('renrenzhuyie.html','wb')as fp:
fp.write(r.content)