Requests库基本使用
Requests库基本使用 requests是python实现的最简单易用的HTTP库,建议爬虫使用requests import requests url = “ 获取某个网页 import requests r = requests.get(“ print(r) # print(type(r)) # print(r.status_code) # 200 各种请求 # 发送一个 HTTP POST 请求: r = requests.post(“http://httpbin.org/post“,data = { key : value }) r = requests.delete( http://httpbin.org/delete ) # 发送一个 HTTP delete 请求: r = requests.head( http://httpbin.org/get ) # 发送一个 HTTP head 请求: r = requests.options( http://httpbin.org/get ) # 发送一个 HTTP options 请求: ` get 传递URL参数 ?+键值对 response1 = requests.get(“http://httpbin.org/get?key1=value1“) print(response1.url) #http://httpbin.org/get?key1=value1 requests提供了params关键字参数来传递参数 parameter = { “key1“:“value1“, “key2“:“value2“ } response2 = requests.get(“http://httpbin.org/get“,params = parameter) print(response2.url) # http://httpbin.org/get?key1=value1 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36“ } response = requests.get(“http