{"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
Python Requests\u8be6\u7ec6\u6559\u7a0b<\/span>\n <\/div>\n <\/body>pip\u5b89\u88c5Requests<\/h2>\n
$ pip install requests
$ easy_install requests <\/span><\/code><\/pre>\n<\/p><\/div>\nGET\u8bf7\u6c42\uff08headers\u53c2\u6570 \u548c parmas\u53c2\u6570\uff09<\/h2>\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 # 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.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
# 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 # 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
# 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
\u79c1\u5bc6\u4ee3\u7406<\/h3>\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>\nweb\u5ba2\u6237\u7aef\u9a8c\u8bc1<\/h3>\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>\nHTTPS\u8bf7\u6c42 SSL\u8bc1\u4e66\u9a8c\u8bc1<\/h2>\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
\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}]}}