{"id":1552,"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 Urllib\u5e93","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1552.html","title":{"rendered":"Python Urllib\u5e93"},"content":{"rendered":"


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

\n

Python Urllib\u5e93<\/h1>\n

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

urllib<\/h2>\n
\n \u5728Python2.x\u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7urllib \u6216\u8005urllib2 \u8fdb\u884c\u7f51\u9875\u6293\u53d6\uff0c\u4f46\u662f\u518dPython3.x \u79fb\u9664\u4e86urllib2\u3002\u53ea\u80fd\u901a\u8fc7urllib\u8fdb\u884c\u64cd\u4f5c\n <\/div>\n
\n
 import <\/span>urllib.request
# \u7f51\u7ad9\u662fhttps\u8bbf\u95ee\u7684\u9700\u8981\u5f15\u5165ssl\u6a21\u5757<\/span>
import <\/span>ssl
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1<\/span>
ssl._create_default_https_context = ssl._create_unverified_context
response = urllib.request.urlopen<\/span>('https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>)
print(response.read<\/span>().decode<\/span>('utf-8'<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
 <html lang=\"en\">
<head>
   <meta charset=\"UTF-8\">
   <title>python spider <\/title>
<\/head>
<body>
   Hello\uff0cI am lidihuo
   Welcome to Python spider
<\/body>
<\/html> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5e26\u53c2\u6570\u7684urllib\n <\/div>\n
\n
 import <\/span>urllib.request
# \u7f51\u7ad9\u662fhttps\u8bbf\u95ee\u7684\u9700\u8981\u5f15\u5165ssl\u6a21\u5757<\/span>
import <\/span>ssl
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1<\/span>
ssl._create_default_https_context = ssl._create_unverified_context
url = 'https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>
url = url + '?'<\/span> + key + '='<\/span><\/span> + value1 + '&'<\/span> + key2 + '=' + value2
response = urllib.request.urlopen<\/span>(url)
# print(response.read().decode('utf-8'))<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n urllib.request\u5b9a\u4e49\u4e86\u4ee5\u4e0b\u51fd\u6570\uff1a\n <\/div>\n
\n
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)<\/code><\/pre>\n<\/p><\/div>\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/h3>\n

url\uff1a<\/b> \u7f51\u9875\u5730\u5740\uff0c\u6253\u5f00url\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u6216\u8005\u662fRequest\u5bf9\u8c61\u3002<\/span>
\n data\uff1a<\/b> \u4e00\u4e2a\u5b9a\u4e49\u4e86\u5411\u670d\u52a1\u5668\u6240\u53d1\u9001\u989d\u5916\u6570\u636e\u7684\u5bf9\u8c61\uff0c\u6216\u8005\u5982\u679c\u6ca1\u6709\u5fc5\u8981\u6570\u636e\u7684\u8bdd\uff0c\u5c31\u662fNone\u503c\u3002<\/span>
\n timeout:<\/b> \u53c2\u6570\u6307\u5b9a\u963b\u6b62\u8bf8\u5982\u8fde\u63a5\u5c1d\u8bd5\u7b49\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09<\/span>
\n context:<\/b> \u63cf\u8ff0\u5404\u79cdSSL\u9009\u9879\u7684ssl.SSLContext\u5b9e\u4f8b\u3002 <\/span>
\n cafile\/capath:<\/b> \u4e3aHTTPS\u8bf7\u6c42\u6307\u5b9a\u4e00\u7ec4\u53ef\u4fe1\u7684CA\u8bc1\u4e66\u3002<\/span> <\/p>\n

\u8fd4\u56de\u503c\uff1a<\/h3>\n
\n \u8be5\u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u53ef\u4ee5\u4f5c\u4e3acontext manager\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u5e76\u4e14\u5177\u6709\u65b9\u6cd5\uff0c\u4f8b\u5982:\n <\/div>\n

geturl():<\/b> \u8fd4\u56de\u68c0\u7d22\u7684\u8d44\u6e90\u7684URL\uff0c\u901a\u5e38\u7528\u4e8e\u786e\u5b9a\u662f\u5426\u9075\u5faa\u91cd\u5b9a\u5411;<\/span>
\n info():<\/b> \u4ee5email.message_from_string\uff08\uff09\u5b9e\u4f8b\u7684\u5f62\u5f0f\u8fd4\u56de\u9875\u9762\u7684\u5143\u4fe1\u606f\uff08\u5982headers\uff09\uff08\u53ef\u5feb\u901f\u53c2\u8003HTTP Headers\uff09;<\/span>
\n getcode():<\/b> \u8fd4\u56de\u54cd\u5e94\u7684HTTP\u72b6\u6001\u7801\u3002<\/span> <\/p>\n

\n \u5bf9\u4e8eHTTP\u548cHTTPS URL\uff0c\u6b64\u51fd\u6570\u8fd4\u56de\u7a0d\u5fae\u4fee\u6539\u7684http.client.HTTPResponse\u5bf9\u8c61\u3002
\n
\u9664\u4e0a\u8ff0\u4e09\u79cd\u65b0\u65b9\u6cd5\u4e4b\u5916\uff0cmsg\u5c5e\u6027\u8fd8\u5305\u542b\u4e0ereason\u5c5e\u6027\uff08\u670d\u52a1\u5668\u8fd4\u56de\u7684\u539f\u56e0\u77ed\u8bed\uff09\u76f8\u540c\u7684\u4fe1\u606f\uff0c\u800c\u4e0d\u662fHTTPResponse\u6587\u6863\u4e2d\u6307\u5b9a\u7684\u54cd\u5e94\u6807\u5934\u3002
\n
\u5bf9\u4e8e\u7531\u4f20\u7edfURLopener\u548cFancyURLopener\u7c7b\u660e\u786e\u5904\u7406\u7684FTP\uff0c\u6587\u4ef6\u548c\u6570\u636eURL\u548c\u8bf7\u6c42\uff0c\u6b64\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aurllib.response.addinfourl\u5bf9\u8c61\u3002
\n
\u5728\u534f\u8bae\u9519\u8bef\u4e0a\u5f15\u53d1URLError\u3002
\n
\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u6ca1\u6709\u5904\u7406\u8bf7\u6c42\u7684handler\uff0c\u5219\u53ef\u80fd\u8fd4\u56deNone\uff08\u5c3d\u7ba1\u9ed8\u8ba4\u5b89\u88c5\u7684\u5168\u5c40OpenerDirector\u4f7f\u7528UnknownHandler\u6765\u786e\u4fdd\u6c38\u4e0d\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff09\u3002
\n
\u53e6\u5916\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u4ee3\u7406\u8bbe\u7f6e\uff08\u4f8b\u5982\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u8bf8\u5982http_proxy\u7684* _proxy\u73af\u5883\u53d8\u91cf\uff09\uff0c\u5219\u9ed8\u8ba4\u5b89\u88c5ProxyHandler\uff0c\u5e76\u786e\u4fdd\u901a\u8fc7\u4ee3\u7406\u5904\u7406\u8bf7\u6c42\u3002
\n
\u6765\u81eaPython 2.6\u53ca\u66f4\u65e9\u7248\u672c\u7684\u9057\u7559urllib.urlopen\u51fd\u6570\u5df2\u505c\u6b62\u4f7f\u7528; urllib.request.urlopen\uff08\uff09\u5bf9\u5e94\u4e8e\u65e7\u7684urllib2.urlopen\u3002
\n
\u901a\u8fc7\u4f7f\u7528ProxyHandler\u5bf9\u8c61\u53ef\u4ee5\u83b7\u5f97\u901a\u8fc7\u5c06\u5b57\u5178\u53c2\u6570\u4f20\u9012\u7ed9urllib.urlopen\u5b8c\u6210\u7684\u4ee3\u7406\u5904\u7406\u3002\n <\/div>\n

POST\/GET\u8bf7\u6c42<\/h2>\n
\n \u4e0a\u9762\u7684\u7a0b\u5e8f\u6f14\u793a\u4e86\u6700\u57fa\u672c\u7684\u7f51\u9875\u6293\u53d6\uff0c\u4e0d\u8fc7\uff0c\u73b0\u5728\u5927\u591a\u6570\u7f51\u7ad9\u90fd\u662f\u52a8\u6001\u7f51\u9875\uff0c\u9700\u8981\u4f60\u52a8\u6001\u5730\u4f20\u9012\u53c2\u6570\u7ed9\u5b83\uff0c\u5b83\u505a\u51fa\u5bf9\u5e94\u7684\u54cd\u5e94\u3002POST\u548cGET\u8bf7\u6c42\u6700\u91cd\u8981\u7684\u533a\u522b\u662fGET\u65b9\u5f0f\u662f\u76f4\u63a5\u4ee5\u94fe\u63a5\u5f62\u5f0f\u8bbf\u95ee\uff0c\u94fe\u63a5\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u53c2\u6570\uff0cPOST\u5219\u4e0d\u4f1a\u5728\u7f51\u5740\u4e0a\u663e\u793a\u6240\u6709\u7684\u53c2\u6570\u3002\n <\/div>\n

POST\u65b9\u5f0f\uff1a<\/h3>\n
\n \u4e0b\u9762\u6f14\u793aPOST\u8bf7\u6c42\u65b9\u5f0f\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-19<\/span>
#post \u8bf7\u6c42
<\/span> import <\/span>urllib.request
import <\/span>urllib.parse
base_url=\"\u4f60\u7684\u8bf7\u6c42\u7f51\u5740\"<\/span>
# \u4f60\u8bf7\u6c42\u7f51\u5740\u7684\u53c2\u6570
<\/span> data={
    \"name\"<\/span>:\"www.lidihuo.com\"<\/span>,
    \"pass\"<\/span>:\"xxxx\"<\/span>
}
headers={\"User-Agent\"<\/span>:\"Mozilla\/5.0 <\/span>(Windows NT 6.1; WOW64) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/63.0.3239.132 Safari\/537.36 QIHU 360SE\"<\/span>}
postdata=urllib.parse.urlencode<\/span>(data).encode<\/span>('utf-8'<\/span><\/span>)
req=urllib.request.Request<\/span>(url=base_url,headers=headers,data=postdata,method='POST'<\/span>)
response=urllib.request.urlopen<\/span>(req)
html=response.read<\/span>()
# html=response.read<\/span>().decode<\/span>('utf-8') #\u8fd9\u91cc\u8bb2\u89e3\u4e00\u4e0bdecode()\u662f\u628abytes\u8f6c\u5316\u89e3\u7801\u4e3a\u4e86 str ,
<\/span> print(html)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684base_url\u548cdata\u53c2\u6570\u9700\u6839\u636e\u7f51\u5740\u81ea\u884c\u8bbe\u7f6e\uff0c\u4e0b\u9762\u5229\u7528urllib\u7684urlencode\u65b9\u6cd5\u5c06\u5b57\u5178\u7f16\u7801\uff0c\u547d\u540d\u4e3adata\uff0c\u6784\u5efarequest\u65f6\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff0curl\u548cdata\uff0c\u8fd0\u884c\u7a0b\u5e8f\uff0c\u8fd4\u56de\u7684\u4fbf\u662fPOST\u540e\u5448\u73b0\u7684\u9875\u9762\u5185\u5bb9\u3002\n <\/div>\n

GET\u65b9\u5f0f\u8bf7\u6c42\uff1a<\/h3>\n
\n GET\u65b9\u5f0f\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u628a\u53c2\u6570\u5199\u5230\u7f51\u5740\u4e0a\u9762\uff0c\u76f4\u63a5\u6784\u5efa\u4e00\u4e2a\u5e26\u53c2\u6570\u7684URL\u51fa\u6765\u5373\u53ef\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-19<\/span>
from <\/span>urllib import <\/span>parse, request
import <\/span>random
url = 'https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>
keyvalue = 'url\u53c2\u6570'<\/span>
encoded_wd = parse.urlencode<\/span>(wd)
new_url = url + '?'<\/span> + encoded_wd
req = request.Request<\/span>(url)
# \u4e3a\u4e86\u9632\u6b62\u88ab\u7f51\u7ad9\u5c01ip\uff0c\u6a21\u4eff\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u7ad9
<\/span> ua_list = [
    \"Mozilla\/5.0 <\/span>(Macintosh; Intel Mac OS X 10.6; rv2.0.1) Gecko\/20100101 Firefox\/4.0.1\"<\/span>,
    \"Mozilla\/5.0 <\/span>(Windows NT 6.1; rv2.0.1) Gecko\/20100101 Firefox\/4.0.1\"<\/span>,
    \"Opera\/9.80 <\/span>(Macintosh; Intel Mac OS X 10.6.8; U; en) Presto\/2.8.131 Version\/11.11\"<\/span>,
    \"Opera\/9.80 <\/span>(Windows NT 6.1; U; en) Presto\/2.8.131 Version\/11.11\"<\/span>,
    \"Mozilla\/5.0 <\/span>(Macintosh; Intel Mac OS X 10_7_0) AppleWebKit\/535.11 <\/span>(KHTML, like Gecko) Chrome\/17.0.963.56 Safari\/535.11\"<\/span>
]
# \u5728User-Agent\u5217\u8868\u91cc\u968f\u673a\u9009\u62e9\u4e00\u4e2aUser-Agent ;\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20
<\/span> user_agent = random.choice<\/span>(ua_list)
req.add_header<\/span>('User-Agent'<\/span>, user_agent)
response = request.urlopen<\/span>(req)
print(response.read<\/span>().decode<\/span>('utf-8'<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

PUT\/DELETE\u8bf7\u6c42<\/h2>\n
\n http\u534f\u8bae\u6709\u516d\u79cd\u8bf7\u6c42\u65b9\u6cd5\uff0cget,head,put,delete,post,options\uff0c\u6211\u4eec\u6709\u65f6\u5019\u9700\u8981\u7528\u5230PUT\u65b9\u5f0f\u6216\u8005DELETE\u65b9\u5f0f\u8bf7\u6c42\u3002\n <\/div>\n
\n PUT\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u6bd4\u8f83\u5c11\u89c1\u3002HTML\u8868\u5355\u4e5f\u4e0d\u652f\u6301\u8fd9\u4e2a\u3002\u672c\u8d28\u4e0a\u6765\u8bb2\uff0c
\n
PUT\u548cPOST\u6781\u4e3a\u76f8\u4f3c\uff0c\u90fd\u662f\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u4e00\u4e2a\u91cd\u8981\u533a\u522b\uff0cPUT\u901a\u5e38\u6307\u5b9a\u4e86\u8d44\u6e90\u7684\u5b58\u653e\u4f4d\u7f6e\uff0c\u800cPOST\u5219\u6ca1\u6709\uff0cPOST\u7684\u6570\u636e\u5b58\u653e\u4f4d\u7f6e\u7531\u670d\u52a1\u5668\u81ea\u5df1\u51b3\u5b9a\u3002
\n
DELETE\uff1a\u5220\u9664\u67d0\u4e00\u4e2a\u8d44\u6e90\u3002\u57fa\u672c\u4e0a\u8fd9\u4e2a\u4e5f\u5f88\u5c11\u89c1\uff0c\u4e0d\u8fc7\u8fd8\u662f\u6709\u4e00\u4e9b\u5730\u65b9\u6bd4\u5982amazon\u7684S3\u4e91\u670d\u52a1\u91cc\u9762\u5c31\u7528\u7684\u8fd9\u4e2a\u65b9\u6cd5\u6765\u5220\u9664\u8d44\u6e90\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>urllib.request
DATA=b'some data'<\/span>
req = urllib.request.Request<\/span>(url='\u7f51\u5740'<\/span>, data='\u53c2\u6570'<\/span>,method='PUT\/DELETE'<\/span>)
f = urllib.request.urlopen<\/span>(req)
print(f.status)
print(f.reason)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8bbe\u7f6eHeaders<\/h2>\n
\n \u6253\u5f00\u6211\u4eec\u7684\u6d4f\u89c8\u5668\uff0c\u8c03\u8bd5\u6d4f\u89c8\u5668F12\uff0c\u80fd\u770b\u5230\u6d4f\u89c8\u5668\u5f88\u591aheader\u4fe1\u606f\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a\n <\/div>\n

Python Urllib\u5e93_https:\/\/bianchenghao6.com_\u3010Python \u722c\u866b\u6559\u7a0b\u3011_\u7b2c1\u5f20
\n User-Agent : \u6709\u4e9b\u670d\u52a1\u5668\u6216 Proxy \u4f1a\u901a\u8fc7\u8be5\u503c\u6765\u5224\u65ad\u662f\u5426\u662f\u6d4f\u89c8\u5668\u53d1\u51fa\u7684\u8bf7\u6c42\u3002<\/span>
\n Content-Type : \u5728\u4f7f\u7528 REST \u63a5\u53e3\u65f6\uff0c\u670d\u52a1\u5668\u4f1a\u68c0\u67e5\u8be5\u503c\uff0c\u7528\u6765\u786e\u5b9a HTTP Body \u4e2d\u7684\u5185\u5bb9\u8be5\u600e\u6837\u89e3\u6790\u3002<\/span>
\n application\/xml \uff1a \u5728 XML RPC\uff0c\u5982 RESTful\/SOAP \u8c03\u7528\u65f6\u4f7f\u7528\u3002<\/span>
\n application\/json \uff1a \u5728 JSON RPC \u8c03\u7528\u65f6\u4f7f\u7528\u3002<\/span>
\n application\/x-www-form-urlencoded \uff1a \u6d4f\u89c8\u5668\u63d0\u4ea4 Web \u8868\u5355\u65f6\u4f7f\u7528\u3002<\/span> <\/p>\n

\n \u5176\u4e2d\uff0cagent\u5c31\u662f\u8bf7\u6c42\u7684\u8eab\u4efd\uff0cuser-agent\u80fd\u591f\u4f7f\u670d\u52a1\u5668\u8bc6\u522b\u51fa\u7528\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ca\u7248\u672c\u3001cpu\u7c7b\u578b\u3001\u6d4f\u89c8\u5668\u7c7b\u578b\u548c\u7248\u672c\u3002\u5f88\u591a\u7f51\u7ad9\u4f1a\u8bbe\u7f6euser-agent\u767d\u540d\u5355\uff0c\u53ea\u6709\u5728\u767d\u540d\u5355\u8303\u56f4\u5185\u7684\u8bf7\u6c42\u624d\u80fd\u6b63\u5e38\u8bbf\u95ee\u3002\u6240\u4ee5\u5728\u6211\u4eec\u7684\u722c\u866b\u4ee3\u7801\u4e2d\u9700\u8981\u8bbe\u7f6euser-agent\u4f2a\u88c5\u6210\u4e00\u4e2a\u6d4f\u89c8\u5668\u8bf7\u6c42\u3002\u6709\u65f6\u5019\u670d\u52a1\u5668\u8fd8\u53ef\u80fd\u4f1a\u6821\u9a8cReferer\uff0c\u6240\u4ee5\u8fd8\u53ef\u80fd\u9700\u8981\u8bbe\u7f6eReferer(\u7528\u6765\u8868\u793a\u6b64\u65f6\u7684\u8bf7\u6c42\u662f\u4ece\u54ea\u4e2a\u9875\u9762\u94fe\u63a5\u8fc7\u6765\u7684)\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>
# \u4f2a\u88c5\u6d4f\u89c8\u5668\u7684\u722c\u866b
<\/span> from <\/span>urllib import <\/span>request
import <\/span>re
import <\/span>ssl
url=\"https:\/\/www.lidihuo.com\/python\/spider-test.html\"<\/span>
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1
<\/span> ssl._create_default_https_context = ssl._create_unverified_context
# \u6784\u9020\u8bf7\u6c42\u5934\u4fe1\u606f
<\/span> header={
    'User-Agent'<\/span>:'Mozilla\/5.0 <\/span>(Windows NT 6.1; WOW64) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/63.0.3239.132 Safari\/537.36'<\/span>
}
req=request.Request<\/span>(url,headers=header)
# \u53d1\u9001\u8bf7\u6c42.\u83b7\u53d6\u54cd\u5e94\u4fe1\u606f
<\/span> reponse=request.urlopen<\/span>(req).read<\/span>().decode<\/span>() #\u89e3\u7801---\uff08\u7f16\u7801encode()\uff09
<\/span> pat=r\"(.*?)<\/title>\"<\/span><br> data=re.<span class=\"mediumblue\">findall<\/span>(pat,reponse)<br> <span class=\"yellow\">#\u7531\u4e8edata\u8fd4\u56de\u7684\u662f\u5217\u8868\u5f62\u5f0f\u7684\u6570\u636e\u7528data[0]\u76f4\u63a5\u53d6\u503c<br><\/span> print(data[0])<br> <\/span><\/code><\/pre>\n<\/p><\/div>\n<div class=\"desc\">\n \u8fd0\u884c\u7ed3\u679c\uff1a\n <\/div>\n<div class=\"codebox codeshow\">\n<pre class=\"pure-highlightjs\"><code class=\"null\">python spider \n<br><\/code><\/pre>\n<\/p><\/div>\n<h2>Proxy\uff08\u4ee3\u7406\uff09\u7684\u8bbe\u7f6e<\/h2>\n<div class=\"desc\">\n \u52a8\u6001\u8bbe\u7f6e\u4ee3\u7406\uff0c\u5c31\u662f\u4e8b\u5148\u51c6\u5907\u4e00\u5806User-Agent.\u6bcf\u6b21\u53d1\u9001\u8bf7\u6c42\u65f6\u5c31\u4ece\u4e2d\u95f4\u968f\u673a\u9009\u53d6\u4e00\u4e2a\uff0c\u91c7\u7528 random\u968f\u673a\u6a21\u5757\u7684choice\u65b9\u6cd5\u968f\u673a\u9009\u62e9User-Agent\uff0c\u8fd9\u6837\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u4ece\u4e2d\u9009\u62e9\uff0c\u8bf7\u6c42\u5f88\u9891\u7e41\u7684\u8bdd\u5c31\u591a\u627e\u51e0\u4e2auser-agent\uff0c\u5177\u4f53\u5982\u4e0b\uff1a\n <\/div>\n<div class=\"codebox codeshow\">\n<pre class=\"pure-highlightjs\"><code class=\"null\"><span> <span class=\"yellow\"># Filename : example.py<\/span><br><span class=\"yellow\"># Copyright : 2020 By Lidihuo<\/span><br><span class=\"yellow\"># Author by : www.lidihuo.com<\/span><br><span class=\"yellow\"># Date : 2020-08-20<\/span><br><span class=\"brown\">def <\/span>load_page(url, form_data):<br>     USER_AGENTS = [<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident\/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(compatible; MSIE 8.0; Windows NT 6.0; Trident\/4.0; WOW64; Trident\/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/4.<span class=\"mediumblue\">0 <\/span>(compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(Windows; U; Windows NT 5.<span class=\"mediumblue\">1; zh-CN) AppleWebKit\/523.15 <\/span>(KHTML, like Gecko, Safari\/419.<span class=\"mediumblue\">3) Arora\/0.3 <\/span>(Change: 287 c9dfb30)\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(X11; U; Linux; en-US) AppleWebKit\/527+ (KHTML, like Gecko, Safari\/419.3) Arora\/0.6\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko\/20070215 K-Ninja\/2.1.1\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko\/20080705 Firefox\/3.0 Kapiko\/3.0\"<\/span>,<br>         <span class=\"brown\">\"Mozilla\/5.<span class=\"mediumblue\">0 <\/span>(X11; Linux i686; U;) Gecko\/20070322 Kazehakase\/0.4.5\"<\/span><br>     ]<br>  <br>     user_agent = random.<span class=\"mediumblue\">choice<\/span>(USER_AGENTS)<br>  <br>     headers = {<br>         <span class=\"brown\">'User-Agent'<\/span>:user_agent<br>     }<br> <\/span><\/code><\/pre>\n<\/p><\/div>\n<div class=\"desc\">\n \u540c\u65f6\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u6362\u4e00\u4e2a\u4ee3\u7406IP\uff0c\u5c31\u7b97\u4ee3\u7406IP\u88ab\u7981\u6b62\uff0c\u4f9d\u7136\u53ef\u4ee5\u6362\u4e2aIP\u7ee7\u7eed\u722c\u53d6\u3002\n <\/div>\n<div class=\"codebox codeshow\">\n<pre class=\"pure-highlightjs\"><code class=\"null\"><span> <span class=\"yellow\"># Filename : example.py<\/span><br><span class=\"yellow\"># Copyright : 2020 By Lidihuo<\/span><br><span class=\"yellow\"># Author by : www.lidihuo.com<\/span><br><span class=\"yellow\"># Date : 2020-08-20<\/span><br> <span class=\"brown\">from <\/span>urllib.error <span class=\"brown\">import <\/span>URLError<br> <span class=\"brown\">from <\/span>urllib.request <span class=\"brown\">import <\/span>ProxyHandler, build_opener<br> proxy = <span class=\"brown\">'127.0.0.1:8088'<\/span><br> <span class=\"yellow\"># \u53c2\u6570\u662f\u5b57\u5178\uff0c\u952e\u540d\u662f\u534f\u8bae\u7c7b\u578b\uff0c\u5065\u503c\u662f\u4ee3\u7406<br><\/span> proxy_handler = ProxyHandler({<br>     <span class=\"brown\">'http'<\/span>: <span class=\"brown\">'http:\/\/'<\/span> + proxy,<br>     <span class=\"brown\">'https'<\/span>: <span class=\"brown\">'https:\/\/'<\/span> + proxy<br> })<br> <span class=\"yellow\"># Opener\u5df2\u7ecf\u8bbe\u7f6e\u597d\u4ee3\u7406\u4e86<br><\/span> opener = build_opener(proxy_handler)<br> try:<br>     response = opener.<span class=\"mediumblue\">open<\/span>(<span class=\"brown\">'https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>)<br>     <span class=\"yellow\"># \u8fd0\u884c\u7ed3\u679c\u662f\u4e00\u4e2aJSON<br><\/span>     print(response.<span class=\"mediumblue\">read<\/span>().<span class=\"mediumblue\">decode<\/span>(<span class=\"brown\">'utf-8'<\/span>))<br> <span class=\"brown\">except <\/span>URLError<span class=\"brown\"> as <\/span>e:<br> <\/span><\/code><\/pre>\n<\/p><\/div>\n<div class=\"desc\">\n \u4e0a\u9762\u7684\u4ee3\u7406\u53ef\u4ee5\u81ea\u5df1\u672c\u5730\u642d\u5efa\uff0c\u8fd8\u53ef\u4ee5\u8d2d\u4e70\u4e00\u4e9b\u4ee3\u7406\u670d\u52a1\u5668\n <\/div>\n<div class=\"codebox codeshow\">\n<pre class=\"pure-highlightjs\"><code class=\"null\"><span> <span class=\"yellow\"># Filename : example.py<\/span><br><span class=\"yellow\"># Copyright : 2020 By Lidihuo<\/span><br><span class=\"yellow\"># Author by : www.lidihuo.com<\/span><br><span class=\"yellow\"># Date : 2020-08-20<\/span><br><span class=\"brown\">from <\/span>urllib.error <span class=\"brown\">import <\/span>URLError<br> <span class=\"brown\">from <\/span>urllib.request <span class=\"brown\">import <\/span>ProxyHandler, build_opener<br> <span class=\"yellow\"># \u52a0\u5165\u4ee3\u7406\u8ba4\u8bc1\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801<br><\/span> proxy = <span class=\"brown\">'\u7528\u6237\u540d:\u5bc6\u7801@ip:port'<\/span><br> proxy_handler = ProxyHandler({<br>     <span class=\"brown\">'http'<\/span>: <span class=\"brown\">'http:\/\/'<\/span> + proxy,<br>     <span class=\"brown\">'https'<\/span>: <span class=\"brown\">'https:\/\/'<\/span> + proxy<br> })<br> opener = build_opener(proxy_handler)<br> try:<br>     response = opener.<span class=\"mediumblue\">open<\/span>(<span class=\"brown\">'https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>)<br>     print(response.<span class=\"mediumblue\">read<\/span>().<span class=\"mediumblue\">decode<\/span>(<span class=\"brown\">'utf-8'<\/span>))<br> <span class=\"brown\">except <\/span>URLError<span class=\"brown\"> as <\/span>e:<br>     print(e.reason)<br> <\/span><\/code><\/pre>\n<\/p><\/div>\n<p> <\/body><br \/>\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python Urllib\u5e93zh-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-1552","post","type-post","status-publish","format-standard","hentry","category-pythonpcjc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1552"}],"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=1552"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1552\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}