requeste的使用


准備工作

安裝requests庫

pip install requests

初步使用

獲取網頁信息直接采用 get() 方法

import requests

#get方法發送網址)

res=requests.get('https://www.csdn.net')

#查看它的類

print(type(r))

#查看狀態碼

print(r.status_code)

#從HTTP header中猜測的響應內容編碼方式

print(r.encoding)

 

#打印響應內容類型

print(type(r.text))

#HTTP響應內容的字符串形式

print(r.text)

 

#打印cookie

print(r.cookies)

HTTP請求

GET請求

使用 get() 方法主要用來獲取網頁信息,就像我們在瀏覽器里面輸入內容然后獲得信息。

基本上沒有什么限制,像一些需要登陸后保持Cookies的情況先不說,但是也要滿足網站的要求,比如很多網站都需要加上headers信息才能訪問,這樣能偽裝成瀏覽器

import requests

headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36'
}
r = requests.get('https://www.zhihu.com', headers=headers)
print(r.status_code)
print(r.text)

可以直接獲取到主頁信息,User-Agent字段信息,也就是瀏覽器標識信息。如果不加這個,知乎會禁止抓取。一般情況下,使用 get() 方法時只要注意好headers信息就好了,針對網站所需要的信息適當選取內容。

對於params參數,就是請求時附加的額外信息。

比如,假設要添加兩個參數username為name、password為pwd,可以把鏈接進行更改:http://httpbin.org/get?username=name&password=pwd,與 post() 的data參數是類似的
POST請求

使用 post() 方法主要是附加數據再提交,比如表單信息或JSON數據,把信息放在data參數里面。

可以請求http://httpbin.org/post來看一下情況,該網站會判斷如果客戶端發起的是POST請求的話,它返回相應的請求信息

import requests

data = {'page': 1, 'pwd': s}

r = requests.post("http://httpbin.org/post", data=data)

發送JSON數據:
import json
import requests

payload = {'page': 1, 'pwd': 's'}

r = requests.post("http://httpbin.org/post", data=json.dumps(payload))

print(r.text)

Cookies

如果某個響應包含一些 cookie,可以直接訪問它們,比如:

如果在一個響應中包含了cookie,那么可以利用cookies屬性拿到這個返回的cookie值:

  1. import requests 
  2. url = "http://www.renren.com/PLogin.do"
    data = {"email":"970138074@qq.com",'password':"pythonspider"}
  3. resp = requests.get('http://www.baidu.com/')
  4. print(resp.cookies)
  5. print(resp.cookies.get_dict()) 

要想發送 cookies 到服務器,可以使用 cookies 參數:

import requests

url = 'http://httpbin.org/cookies'

cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)

 

超時

可以告訴 requests 在經過以 timeout 參數設定的秒數時間之后停止等待響應。基本上所有的生產代碼都應該使用這一參數。如果不使用,程序可能會永遠失去響應:

requests.get('http://github.com', timeout=0.001)

session:

之前使用urllib庫,是可以使用opener發送多個請求,多個請求之間是可以共享cookie的。那么如果使用requests,也要達到共享cookie的目的,那么可以使用requests庫給我們提供的session對象。注意,這里的session不是web開發中的那個session,這個地方只是一個會話的對象而已。還是以登錄人人網為例,使用requests來實現。示例代碼如下:

import requests

url = "http://www.renren.com/PLogin.do"

data = {"email":"970138074@qq.com",'password':"pythonspider"}

headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"}

# 登錄

session = requests.session()

session.post(url,data=data,headers=headers)

# 訪問大鵬個人中心

resp = session.get('http://www.renren.com/880151247/profile')

print(resp.text)

使用代理:

使用requests添加代理也非常簡單,只要在請求的方法中(比如get或者post)傳遞proxies參數就可以了。示例代碼如下:

import requests

url = "http://httpbin.org/get"

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',}

proxy = {'http': '171.14.209.180:27829'} 

resp = requests.get(url,headers=headers,proxies=proxy)

 

 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM