基本例項
import各種請求方式requests
url= '
'response =requests.get(url)
(type(response))
print(response.status_code)#
狀態碼print
(type(response.text))
print(response.text)#
開啟網頁源**
print(response.cookies)#
獲取cookies
import帶引數的get請求requests
url= '
'requests.get(url)
requests.put(url)
requests.delete(url)
requests.head(url)
requests.options(url)
import解析jsonrequests
data=
reponse = requests.get(url,params=data)
import獲取二進位制資料和儲存requests
import
json
reponse =requests.get(url)
(requests.json())
print(json.loads(reponse.text))
importrequests
import
json
reponse =requests.get(url)
(reponse.text)
print(reponse.content)
import新增headersrequests
import
json
reponse =requests.get(url)
with open(
'','
') as f:
f.write(reponse.content)
f.close()
import基本post請求requests
import
json
headers =
response = requests.get(url,headers=headers)
mport requestsreponse屬性import
json
data =
headers=
response = requests.post(url,data=data,headers=headers)
import檔案上傳requests
url= '
'response =requests.get(url)
(type(response))
print(response.status_code)#
狀態碼print
(type(response.text))
print(response.text)#
開啟網頁源**
print(response.cookies)#
獲取cookies
(response.history)
print(response.url)
import維持會話requests
files =
reponse = requests.post(url,files=files)
import證書認證requests
s =requests.session()
s.get(url_1)
response = s.get(url_2)
import異常處理requests
from requests.packages import
urllib3
urllib3.disable_warnings()
#消除警告
pip3 install 'requests[socks]' 使用socks**
import認證設定requests
from requests.exceptions import
readtimeout
try:
response = requests.get(url,timeout=)
except
readtimeout:
print("
time out
")
request庫的基本用法
request urllib 獲取url 獲取狀態碼 request.states code response.getcode 返回html request.text response.read 獲得頭部資訊 request.headers response.info 返回請求的url reques...
請求庫 request使用
coding utf 8 import requests from urllib.parse import urlencode keyword input strip res urlencode encoding utf 8 url res print url response requests.g...
Request庫的使用
request庫 封裝了自動爬取html頁面,自動網路請求提交的方法的庫 request庫的7個主要方法 requests.request 構造乙個請求,是一下個方法的基礎 requests.get 獲取html網頁的主要方法,對應於http的get requests.head 獲取html網頁頭部...