requests是一个简单易用的python实现的HTTP库。使用起来比urllib简单很多,因为是第三方库,所以使用前需要先cmd安装pipinstallrequests。安装后导入,正常就可以开始使用了。基本用法:requests.get()用于请求目标网站,类型为HTTPresponse类型importrequestsresponse=requests.get('http://www.baidu.com')print(response.status_code)#打印状态codeprint(response.url)#打印请求urlprint(response.headers)#打印header信息print(response.cookies)#打印cookie信息print(response.text)#打印网页源码文本print(response.content)#inbytes以流形式打印运行结果:状态码:200各种请求方式:importrequestsrequests.get('http://httpbin.org/get')requests.post('http://httpbin.org/post')要求。put('http://httpbin.org/put')requests.delete('http://httpbin.org/delete')requests.head('http://httpbin.org/get')requests.options('http://httpbin.org/get')basicgetrequestimportrequestsresponse=requests.get('http://httpbin.org/get')print(response.text)GETrequestwithparameters:第一个放参数直接在urlimportrequestsresponse=requests.get(http://httpbin.org/get?name=gemey&age=22)print(response.text)parsejsonimportrequestsresponse=requests.get('http://httpbin.org/get')print(response.text)print(response.json())#response.json()方法同json.loads(response.text)print(type(response.json()))以上就是python安装requests库的详细内容。希望对您有所帮助。阅读原文:点击查看原文
