{"id":1553,"date":"2023-03-25T11:26:10","date_gmt":"2023-03-25T03:26:10","guid":{"rendered":""},"modified":"2023-03-25T11:26:10","modified_gmt":"2023-03-25T03:26:10","slug":"Python Requests","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1553.html","title":{"rendered":"Python Requests"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python Requests<\/h1>\n

Python Requests\u8be6\u7ec6\u6559\u7a0b<\/span>\n <\/div>\n

\n Requests\u7ee7\u627f\u4e86urllib\u7684\u7279\u6027\u3002Requests\u652f\u6301HTTP\u8fde\u63a5\u4fdd\u6301\u548c\u8fde\u63a5\u6c60\uff0c\u652f\u6301\u4f7f\u7528cookie\u4fdd\u6301\u4f1a\u8bdd\uff0c\u652f\u6301\u6587\u4ef6\u4e0a\u4f20\uff0c\u652f\u6301\u81ea\u52a8\u786e\u5b9a\u54cd\u5e94\u5185\u5bb9\u7684\u7f16\u7801\uff0c\u652f\u6301\u56fd\u9645\u5316\u7684 URL \u548c POST \u6570\u636e\u81ea\u52a8\u7f16\u7801\u3002\n <\/div>\n

pip\u5b89\u88c5Requests<\/h2>\n
\n \u5229\u7528 pip \u5b89\u88c5 \u6216\u8005\u5229\u7528 easy_install \u90fd\u53ef\u4ee5\u5b8c\u6210\u5b89\u88c5\uff1a\n <\/div>\n
\n
 $ pip install requests
$ easy_install requests <\/span><\/code><\/pre>\n<\/p><\/div>\n

GET\u8bf7\u6c42\uff08headers\u53c2\u6570 \u548c parmas\u53c2\u6570\uff09<\/h2>\n
\n 1. \u6700\u57fa\u672c\u7684GET\u8bf7\u6c42\u53ef\u4ee5\u76f4\u63a5\u7528get\u65b9\u6cd5\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import requests
response = requests.get<\/span>(\"https:\/\/www.lidihuo.com\/python\/spider-test.html\"<\/span><\/span>)
# \u4e5f\u53ef\u4ee5\u8fd9\u4e48\u5199
<\/span> # response = requests.request<\/span>(\"get\"<\/span>, \"https:\/\/www.lidihuo.com\/python\/spider-test.html\")
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6dfb\u52a0 headers \u548c \u67e5\u8be2\u53c2\u6570<\/b>\uff0c\u5982\u679c\u60f3\u6dfb\u52a0 headers\uff0c\u53ef\u4ee5\u4f20\u5165headers\u53c2\u6570\u6765\u589e\u52a0\u8bf7\u6c42\u5934\u4e2d\u7684headers\u4fe1\u606f\u3002\u5982\u679c\u8981\u5c06\u53c2\u6570\u653e\u5728url\u4e2d\u4f20\u9012\uff0c\u53ef\u4ee5\u5229\u7528 params \u53c2\u6570\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
kw = {'wd'<\/span>:'\u722c\u866b-\u7acb\u5730\u8d27'<\/span>}
headers = {\"User-Agent\"<\/span>: \"Mozilla\/5.0 <\/span>(Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/54.0.2840.99 Safari\/537.36\"<\/span>}
 
# params \u63a5\u6536\u4e00\u4e2a\u5b57\u5178\u6216\u8005\u5b57\u7b26\u4e32\u7684\u67e5\u8be2\u53c2\u6570\uff0c\u5b57\u5178\u7c7b\u578b\u81ea\u52a8\u8f6c\u6362\u4e3aurl\u7f16\u7801\uff0c\u4e0d\u9700\u8981urlencode()
<\/span> response = requests.get<\/span>(\"http:\/\/www.baidu.com\/s?\"<\/span>, params = kw, headers = headers)
 
# \u67e5\u770b\u54cd\u5e94\u5185\u5bb9\uff0cresponse.text \u8fd4\u56de\u7684\u662fUnicode\u683c\u5f0f\u7684\u6570\u636e
<\/span> print(response.text)
 
# \u67e5\u770b\u54cd\u5e94\u5185\u5bb9\uff0cresponse.content\u8fd4\u56de\u7684\u5b57\u8282\u6d41\u6570\u636e
<\/span> print(respones.content)
 
# \u67e5\u770b\u5b8c\u6574url\u5730\u5740
<\/span> print(response.url)
 
# \u67e5\u770b\u54cd\u5e94\u5934\u90e8\u5b57\u7b26\u7f16\u7801
<\/span> print(response.encoding)
 
# \u67e5\u770b\u54cd\u5e94\u7801
<\/span> print(response.status_code)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f7f\u7528response.text \u65f6\uff0cRequests \u4f1a\u57fa\u4e8e HTTP \u54cd\u5e94\u7684\u6587\u672c\u7f16\u7801\u81ea\u52a8\u89e3\u7801\u54cd\u5e94\u5185\u5bb9\uff0c\u5927\u591a\u6570 Unicode \u5b57\u7b26\u96c6\u90fd\u80fd\u88ab\u65e0\u7f1d\u5730\u89e3\u7801\u3002
\n
\u4f7f\u7528response.content \u65f6\uff0c\u8fd4\u56de\u7684\u662f\u670d\u52a1\u5668\u54cd\u5e94\u6570\u636e\u7684\u539f\u59cb\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff0c\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58\u56fe\u7247\u7b49\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\n <\/div>\n

\u57fa\u672cPOST\u8bf7\u6c42\uff08data\u53c2\u6570\uff09<\/h2>\n
\n \u6700\u57fa\u672c\u7684GET\u8bf7\u6c42\u53ef\u4ee5\u76f4\u63a5\u7528post\u65b9\u6cd5\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
response = requests.post<\/span>(\"https:\/\/www.lidihuo.com\/python\/spider-test.html\"<\/span>, data = data)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f20\u5165data\u6570\u636e<\/b>\u5bf9\u4e8e POST \u8bf7\u6c42\u6765\u8bf4\uff0c\u6211\u4eec\u4e00\u822c\u9700\u8981\u4e3a\u5b83\u589e\u52a0\u4e00\u4e9b\u53c2\u6570\u3002\u90a3\u4e48\u6700\u57fa\u672c\u7684\u4f20\u53c2\u65b9\u6cd5\u53ef\u4ee5\u5229\u7528 data \u8fd9\u4e2a\u53c2\u6570\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
formdata = {\u8bf7\u6c42\u53c2\u6570}
url = \"https:\/\/xxxx.com\"<\/span>
headers={ \"User-Agent\"<\/span>: \"Mozilla\/5.0 <\/span>(Windows NT 10.0; WOW64) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/51.0.2704.103 Safari\/537.36\"<\/span>}
response = requests.post<\/span>(url, data = formdata, headers = headers)
print(response.text)
# \u5982\u679c\u662fjson\u6587\u4ef6\u53ef\u4ee5\u76f4\u63a5\u663e\u793a
<\/span> print(response.json<\/span>())
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4ee3\u7406\uff08proxies\u53c2\u6570\uff09<\/h2>\n
\n \u5982\u679c\u9700\u8981\u4f7f\u7528\u4ee3\u7406\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e3a\u4efb\u610f\u8bf7\u6c42\u65b9\u6cd5\u63d0\u4f9b proxies \u53c2\u6570\u6765\u914d\u7f6e\u5355\u4e2a\u8bf7\u6c42\uff1a\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
# \u6839\u636e\u534f\u8bae\u7c7b\u578b\uff0c\u9009\u62e9\u4e0d\u540c\u7684\u4ee3\u7406
<\/span> proxies = {
  \"http\"<\/span>: \"http:\/\/127.0.0.1:8088\"<\/span><\/span>,
  \"https\"<\/span>: \"http:\/\/127.0.0.1:8088\",
}
 
response = requests.get<\/span>(\"https:\/\/www.lidihuo.com\/python\/spider-test.html\"<\/span>, proxies = proxies)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u79c1\u5bc6\u4ee3\u7406\u9a8c\u8bc1 \u548c Web\u5ba2\u6237\u7aef\u9a8c\u8bc1<\/h2>\n
\n \u76f8\u6bd4\u4e8eurllib\uff0crequests\u8bf7\u6c42\u66f4\u7b80\u5355\u53ea\u9700\u8981\u4e00\u6b65\uff1a\n <\/div>\n

\u79c1\u5bc6\u4ee3\u7406<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
# \u5982\u679c\u4ee3\u7406\u9700\u8981\u4f7f\u7528HTTP Basic Auth\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u79cd\u683c\u5f0f\uff1a
<\/span> proxy = { \"http\"<\/span>: \"username:password@ip:port\"<\/span> }
 
response = requests.get<\/span>(url, proxies = proxy)
 
print(response.text)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

web\u5ba2\u6237\u7aef\u9a8c\u8bc1<\/h3>\n
\n \u5982\u679c\u662fWeb\u5ba2\u6237\u7aef\u9a8c\u8bc1\uff0c\u9700\u8981\u6dfb\u52a0 auth = (\u8d26\u6237\u540d, \u5bc6\u7801)\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
auth=(username, password)
response = requests.get<\/span>(url, auth = auth)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

HTTPS\u8bf7\u6c42 SSL\u8bc1\u4e66\u9a8c\u8bc1<\/h2>\n
\n Requests\u4e5f\u53ef\u4ee5\u4e3aHTTPS\u8bf7\u6c42\u9a8c\u8bc1SSL\u8bc1\u4e66\uff1a\u8981\u60f3\u68c0\u67e5\u67d0\u4e2a\u4e3b\u673a\u7684SSL\u8bc1\u4e66\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 verify \u53c2\u6570\uff08\u4e5f\u53ef\u4ee5\u4e0d\u5199\uff09\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-20<\/span>
import <\/span>requests
response = requests.get<\/span>(\"https:\/\/www.lidihuo.com\/python\/spider-test.html\"<\/span><\/span>, verify=True)
 
# \u4e5f\u53ef\u4ee5\u7701\u7565\u4e0d\u5199
<\/span> # response = requests.get<\/span>(\"https:\/\/www.lidihuo.com\/python\/spider-test.html\")
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python Requestszh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145],"tags":[],"class_list":["post-1553","post","type-post","status-publish","format-standard","hentry","category-pythonpcjc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1553"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1553"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1553\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}