Use the interface to crawl data!
參數介紹
url: 必填,請求的網址
data: 選填,字典,元組列表,bytes or to send toURL的文件對象.
json: 可選,發送到指定URL的JSON對象.
files: 可選,要發送到URL的文件字典
allow_redirects:可選.用於啟用/禁用重定向的布爾值.
默認True(允許重定向)
auth: 可選.用於啟用某種HTTP身份驗證的元組.
默認None
cert:可選.指定證書文件或密鑰的字符串或元組.
默認None
cookies: 可選.要發送到指定網址的Cookie字典.
默認None
headers:可選.要發送到指定網址的HTTP標頭字典.
默認None
proxies: 可選.URL代理協議字典.
默認None
stream: 可選.如果響應應立即下載(False)或流式傳輸(True)的布爾指示.
默認False
timeout: 可選.一個數字或一個元組,指示等待客戶端建立連接和/或發送響應的秒數.
默認值None表示請求將繼續,直到連接關閉.
verify: 可選.用於驗證服務器TLS證書的布爾值或字符串指示.默認True
import requests
import configparser
import pprint
import json
url = '這裡寫具體的url'
json = {
'Write the specific resources to be obtained here,字典格式'
}
headers = {
"User-Agent": 'Write some information about the user agent here,打開網站,點檢查,點網絡,Click on the first element below,Found under request headersUser-Agent貼過來',
"cookie": '和找到User-Agent方法一樣,在下邊找到cookie'}
session = requests.session()
# 傳入相關參數,開始請求
response = session.post(url=url, json=json , headers=headers)
pprint.pprint(json.loads(response.text))
return response
# JSONThe format is website and API使用的通用標准格式,它是一種數據結構,將對象用文本形式表示出來.
# JSONFormat string example:
# JSONThe data looks like onePython字典,以鍵值對的形式傳遞數據.然而,JSONData can also be strings、數字、Boolean or list.
{
"name": "United States",
"population": 331002651,
"capital": "Washington D.C.",
"languages": [
"English",
"Spanish"
]
}
# 在JSON流行之前,XMLis the more common choice,But it looks relatively complicated
# XMLExample of the same information:
<?xml version="1.0" encoding="UTF-8"?>
<country>
<name>United States</name>
<population>331002651</population>
<capital>Washington D.C.</capital>
<languages>
<language>English</language>
<language>Spanish</language>
</languages>
</country>
# Python原生支持JSON數據,Python Json模塊是標准庫的一部分.
# 這個JSON模塊可以將JSON數據從JSONformat is converted to the equivalentPython對象,例如字典和列表,It can also be converted in reverse.
# 將JSON字符串解析為Python字典(Parsing the format of the data mainly depends on what the input data is)
# eg:
import json
country = '{
"name": "United States", "population": 331002651}'
# loads: 代表的是字符串,loads方法用於讀取JSON字符串.
country_dict = json.loads(country)
# country_dict 此時已經是Python字典
# 除了loads()方法,還有load()方法,load()用於讀取文件中的JSON數據.load()方法接收一個文件對象並返回解析為Python對象的JSON數據.
# 將Python對象轉換為JSON對象也稱為序列化或JSON編碼.可以使用函數dumps()來實現.
import json
languages = ["English","French"]
country = {
"name": "Canada",
"population": 37742154,
"languages": languages,
"president": None,
}
country_string = json.dumps(country)
print(country_string)
結果:
{
"name": "Canada", "population": 37742154, "languages": ["English", "French"],
"president": null}
import requests
response = requests.post(url=url)
import requests
session = requests.session()
response = session.post(url=url)
# 獲取到cookies
cookie = session.cookies
requests.post:The connection is closed after the call is complete,所以cookies就隨著connect的消亡而消亡.
session.post:高級用法,The context connection was not closed,Sessions remain in the connection pool,即cookies是一直有效的.
總結:
1、request占用資源較少,安全性比較高,But not much information can be obtained.
2、sessionRelatively speaking, it consumes more resources,安全性相對較低,But more information is available,例如cookies.
3、requestEquivalent to a local variable accessed at one time,sessionEquivalent to client-side global variables.
參考文章:
Python Requests post() 方法.
request和session的區別.
使用Python讀取和解析JSON數據教程.
python調用request.post請求與Postman方法.
Love only affects how fast I can code!