怎么在Python中使用Post請求爬取數據?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
import requests import json headers = { 'Accept':'application/json, text/javascript, */*; q=0.01', 'X-Requested-With':'XMLHttpRequest', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36', 'Content-Type':'application/json', 'Accept-Encoding':'gzip, deflate', 'Accept-Language':'zh-CN,zh;q=0.8', 'Cache-Control':'no-cache', } #空的對象,body參數 data = {} data = json.dumps(data) page = 0 url = '網站地址,后面為參數?param1=1¶m1='+str(page) response = requests.post(url = url,data=data ,headers =headers ) print(response.url) print(response.text)
總結
現在相關工具中發現現象
請求方式確定:post還是get或者其它
參數類型:form-data還是raw或者其它
參數位置:若是post請求,在query中還是body中,還是二者皆有
ps:python requests 發起http POST 請求
python requests 發起http POST 請求,帶參數,帶請求頭:
#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json url = 'http://official-account/app/messages/group' body = {"type": "text", "content": "測試文本", "tag_id": "20717"} headers = {'content-type': "application/json", 'Authorization': 'APP appid = 4abf1a,token = 9480295ab2e2eddb8'} #print type(body) #print type(json.dumps(body)) # 這里有個細節,如果body需要json形式的話,需要做處理 # 可以是data = json.dumps(body) response = requests.post(url, data = json.dumps(body), headers = headers) # 也可以直接將data字段換成json字段,2.4.3版本之后支持 # response = requests.post(url, json = body, headers = headers) # 返回信息 print response.text # 返回響應頭 print response.status_code
python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。
看完上述內容,你們掌握怎么在Python中使用Post請求爬取數據的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。