{"id":25625,"date":"2023-10-06T06:02:05","date_gmt":"2023-10-05T22:02:05","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=25625"},"modified":"2023-10-06T06:02:05","modified_gmt":"2023-10-05T22:02:05","slug":"python-urllib","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-urllib.html","title":{"rendered":"\u5b66\u4e60Python\u7684urllib\u6a21\u5757"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u00a0urllib \u6a21\u5757\u4f5c\u4e3aPython 3 \u5904\u7406 URL \u7684\u7ec4\u4ef6\u96c6\u5408\uff0c\u5982\u679c\u4f60\u6709 Python 2 \u7684\u77e5\u8bc6\uff0c\u90a3\u4e48\u4f60\u5c31\u4f1a\u6ce8\u610f\u5230 Python 2 \u4e2d\u6709 urllib \u548c urllib2 \u4e24\u4e2a\u7248\u672c\u7684\u6a21\u5757\uff0c\u8fd9\u4e9b\u73b0\u5728\u90fd\u662f Python 3 \u7684 urllib \u5305\u7684\u4e00\u90e8\u5206\uff0c\u5177\u4f53\u5982\u4f55\u6765\u4f53\u73b0\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Python 3 \u7684 urllib \u6a21\u5757\u662f\u4e00\u5806\u53ef\u4ee5\u5904\u7406 URL \u7684\u7ec4\u4ef6\u96c6\u5408\u3002\u5982\u679c\u4f60\u6709 Python 2 \u7684\u77e5\u8bc6\uff0c\u90a3\u4e48\u4f60\u5c31\u4f1a\u6ce8\u610f\u5230 Python 2 \u4e2d\u6709 urllib \u548c urllib2 \u4e24\u4e2a\u7248\u672c\u7684\u6a21\u5757\u3002\u8fd9\u4e9b\u73b0\u5728\u90fd\u662f Python 3 \u7684 urllib \u5305\u7684\u4e00\u90e8\u5206\u3002\u5f53\u524d\u7248\u672c\u7684 urllib \u5305\u62ec\u4e0b\u9762\u51e0\u90e8\u5206\uff1a<\/p>\n

    \n
  • urllib.request<\/li>\n
  • urllib.error<\/li>\n
  • urllib.parse<\/li>\n
  • urllib.rebotparser<\/li>\n<\/ul>\n

    \u63a5\u4e0b\u6765\u6211\u4eec\u4f1a\u5206\u5f00\u8ba8\u8bba\u9664\u4e86 urllib.error \u4ee5\u5916\u7684\u51e0\u90e8\u5206\u3002\u5b98\u65b9\u6587\u6863\u5b9e\u9645\u63a8\u8350\u4f60\u5c1d\u8bd5\u7b2c\u4e09\u65b9\u5e93\uff0c requests\uff0c\u4e00\u4e2a\u9ad8\u7ea7\u7684 HTTP \u5ba2\u6237\u7aef\u63a5\u53e3\u3002\u7136\u800c\u6211\u4f9d\u7136\u8ba4\u4e3a\u77e5\u9053\u5982\u4f55\u4e0d\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\u6253\u5f00 URL \u5e76\u4e0e\u4e4b\u8fdb\u884c\u4ea4\u4e92\u662f\u5f88\u6709\u7528\u7684\uff0c\u800c\u4e14\u8fd9\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7406\u89e3\u4e3a\u4ec0\u4e48 requests \u5305\u662f\u5982\u6b64\u7684\u6d41\u884c\u3002<\/p>\n

    urllib.request<\/strong><\/div>\n

    urllib.request \u6a21\u5757\u671f\u521d\u662f\u7528\u6765\u6253\u5f00\u548c\u83b7\u53d6 URL \u7684\u3002\u8ba9\u6211\u4eec\u770b\u770b\u4f60\u53ef\u4ee5\u7528\u51fd\u6570 urlopen \u53ef\u4ee5\u505a\u7684\u4e8b\uff1a<\/p>\n

    >>> import urllib.request\r\n>>> url = urllib.request.urlopen('https:\/\/www.google.com\/')\r\n>>> url.geturl()\r\n'https:\/\/www.google.com\/'\r\n>>> url.info()\r\n<http.client.HTTPMessage object at 0x7fddc2de04e0>\r\n>>> header = url.info()\r\n>>> header.as_string()\r\n('Date: Fri, 24 Jun 2016 18:21:19 GMT\/n'\r\n 'Expires: -1\/n'\r\n 'Cache-Control: private, max-age=0\/n'\r\n 'Content-Type: text\/html; charset=ISO-8859-1\/n'\r\n 'P3P: CP=\"This is not a P3P policy! See '\r\n 'https:\/\/www.google.com\/support\/accounts\/answer\/151657?hl=en for more info.\"\/n'\r\n 'Server: gws\/n'\r\n 'X-XSS-Protection: 1; mode=block\/n'\r\n 'X-Frame-Options: SAMEORIGIN\/n'\r\n 'Set-Cookie: '\r\n 'NID=80=tYjmy0JY6flsSVj7DPSSZNOuqdvqKfKHDcHsPIGu3xFv41LvH_Jg6LrUsDgkPrtM2hmZ3j9V76pS4K_cBg7pdwueMQfr0DFzw33SwpGex5qzLkXUvUVPfe9g699Qz4cx9ipcbU3HKwrRYA; '\r\n 'expires=Sat, 24-Dec-2016 18:21:19 GMT; path=\/; domain=.google.com; HttpOnly\/n'\r\n 'Alternate-Protocol: 443:quic\/n'\r\n 'Alt-Svc: quic=\":443\"; ma=2592000; v=\"34,33,32,31,30,29,28,27,26,25\"\/n'\r\n 'Accept-Ranges: none\/n'\r\n 'Vary: Accept-Encoding\/n'\r\n 'Connection: close\/n'\r\n '\/n')\r\n>>> url.getcode()\r\n200<\/pre>\n

    \u5728\u8fd9\u91cc\u6211\u4eec\u5305\u542b\u4e86\u9700\u8981\u7684\u6a21\u5757\uff0c\u7136\u540e\u544a\u8bc9\u5b83\u6253\u5f00 Google \u7684 URL\u3002\u73b0\u5728\u6211\u4eec\u5c31\u6709\u4e86\u4e00\u4e2a\u53ef\u4ee5\u4ea4\u4e92\u7684 HTTPResponse \u5bf9\u8c61\u3002\u6211\u4eec\u8981\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u8c03\u7528\u65b9\u6cd5 geturl \uff0c\u5b83\u4f1a\u8fd4\u56de\u6839\u636e URL \u83b7\u53d6\u7684\u8d44\u6e90\u3002\u8fd9\u53ef\u4ee5\u8ba9\u6211\u4eec\u53d1\u73b0 URL \u662f\u5426\u8fdb\u884c\u4e86\u91cd\u5b9a\u5411\u3002
    \n\u63a5\u4e0b\u6765\u8c03\u7528 info \uff0c\u5b83\u4f1a\u8fd4\u56de\u7f51\u9875\u7684\u5143\u6570\u636e\uff0c\u6bd4\u5982\u8bf7\u6c42\u5934\u4fe1\u606f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u8d4b\u7ed9\u6211\u4eec\u7684 headers \u53d8\u91cf\uff0c\u7136\u540e\u8c03\u7528\u5b83\u7684\u65b9\u6cd5 as_string \u3002\u5c31\u53ef\u4ee5\u6253\u5370\u51fa\u6211\u4eec\u4ece Google \u6536\u5230\u7684\u5934\u4fe1\u606f\u3002\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7 getcode \u5f97\u5230\u7f51\u9875\u7684 HTTP \u54cd\u5e94\u7801\uff0c\u5f53\u524d\u60c5\u51b5\u4e0b\u5c31\u662f 200\uff0c\u610f\u601d\u662f\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n

    \u5982\u679c\u4f60\u60f3\u770b\u770b\u7f51\u9875\u7684 HTML \u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528\u53d8\u91cf url \u7684\u65b9\u6cd5 read\u3002\u6211\u4e0d\u51c6\u5907\u518d\u73b0\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u56e0\u4e3a\u8f93\u51fa\u7ed3\u679c\u592a\u957f\u4e86\u3002<\/p>\n

    \u8bf7\u6ce8\u610f request \u5bf9\u8c61\u9ed8\u8ba4\u53d1\u8d77 GET \u8bf7\u6c42\uff0c\u9664\u975e\u4f60\u6307\u5b9a\u4e86\u5b83\u7684 data \u53c2\u6570\u3002\u5982\u679c\u4f60\u7ed9\u5b83\u4f20\u9012\u4e86 data \u53c2\u6570\uff0c\u8fd9\u6837 request \u5bf9\u8c61\u5c06\u4f1a\u53d8\u6210 POST \u8bf7\u6c42\u3002<\/p>\n

    \u4e0b\u8f7d\u6587\u4ef6<\/strong><\/div>\n

    urllib \u4e00\u4e2a\u5178\u578b\u7684\u5e94\u7528\u573a\u666f\u662f\u4e0b\u8f7d\u6587\u4ef6\u3002\u8ba9\u6211\u4eec\u770b\u770b\u51e0\u79cd\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u7684\u65b9\u6cd5\uff1a<\/p>\n

    >>> import urllib.request\r\n>>> url = 'http:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2012\/06\/wxDbViewer.zip'\r\n>>> response = urllib.request.urlopen(url)\r\n>>> data = response.read()\r\n>>> with open('\/home\/mike\/Desktop\/test.zip', 'wb') as fobj:\r\n...     fobj.write(data)\r\n...\r\n<\/pre>\n

    \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u4fdd\u5b58\u5728\u6211\u7684\u535a\u5ba2\u4e0a\u7684 zip \u538b\u7f29\u6587\u4ef6\u7684 URL\u3002\u7136\u540e\u6211\u4eec\u8bfb\u51fa\u6570\u636e\u5e76\u5c06\u6570\u636e\u5199\u5230\u78c1\u76d8\u3002\u4e00\u4e2a\u66ff\u4ee3\u6b64\u64cd\u4f5c\u7684\u65b9\u6848\u662f\u4f7f\u7528 urlretrieve \uff1a<\/p>\n

    >>> import urllib.request\r\n>>> url = 'http:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2012\/06\/wxDbViewer.zip'\r\n>>> tmp_file, header = urllib.request.urlretrieve(url)\r\n>>> with open('\/home\/mike\/Desktop\/test.zip', 'wb') as fobj:\r\n...     with open(tmp_file, 'rb') as tmp:\r\n...         fobj.write(tmp.read())\r\n<\/pre>\n

    \u65b9\u6cd5 urlretrieve \u4f1a\u628a\u7f51\u7edc\u5bf9\u8c61\u62f7\u8d1d\u5230\u672c\u5730\u6587\u4ef6\u3002\u9664\u975e\u4f60\u5728\u4f7f\u7528 urlretrieve \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u4f60\u8981\u4fdd\u5b58\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5426\u5219\u8fd9\u4e2a\u6587\u4ef6\u5c06\u88ab\u62f7\u8d1d\u5230\u4e34\u65f6\u6587\u4ef6\u5939\u7684\u968f\u673a\u547d\u540d\u7684\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002\u8fd9\u4e2a\u53ef\u4ee5\u4e3a\u4f60\u8282\u7701\u4e00\u6b65\u64cd\u4f5c\uff0c\u5e76\u4e14\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u7b80\u5355\uff1a<\/p>\n

    >>> import urllib.request\r\n>>> url = 'http:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2012\/06\/wxDbViewer.zip'\r\n>>> urllib.request.urlretrieve(url, '\/home\/mike\/Desktop\/blog.zip')\r\n('\/home\/mike\/Desktop\/blog.zip',\r\n <http.client.HTTPMessage object at 0x7fddc21c2470>)\r\n<\/pre>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u5b83\u8fd4\u56de\u4e86\u6587\u4ef6\u4fdd\u5b58\u7684\u8def\u5f84\uff0c\u4ee5\u53ca\u4ece\u8bf7\u6c42\u5f97\u6765\u7684\u5934\u4fe1\u606f\u3002<\/p>\n

    \u8bbe\u7f6e\u4f60\u7684\u7528\u6237\u4ee3\u7406<\/strong><\/div>\n

    \u5f53\u4f60\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u9875\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u544a\u8bc9\u7f51\u7ad9\u5b83\u662f\u8c01\u3002\u8fd9\u5c31\u662f\u6240\u8c13\u7684 user-agent \uff08\u7528\u6237\u4ee3\u7406\uff09\u5b57\u6bb5\u3002Python \u7684 urllib \u4f1a\u8868\u793a\u5b83\u81ea\u5df1\u4e3a Python-urllib\/x.y \uff0c \u5176\u4e2d x \u548c y \u662f\u4f60\u4f7f\u7528\u7684 Python \u7684\u4e3b\u3001\u6b21\u7248\u672c\u53f7\u3002\u6709\u4e00\u4e9b\u7f51\u7ad9\u4e0d\u8ba4\u8bc6\u8fd9\u4e2a\u7528\u6237\u4ee3\u7406\u5b57\u6bb5\uff0c\u7136\u540e\u7f51\u7ad9\u53ef\u80fd\u4f1a\u6709\u5947\u602a\u7684\u8868\u73b0\u6216\u8005\u6839\u672c\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u8f9b\u8fd0\u7684\u662f\u4f60\u53ef\u4ee5\u5f88\u8f7b\u677e\u7684\u8bbe\u7f6e\u4f60\u81ea\u5df1\u7684 user-agent \u5b57\u6bb5\u3002<\/p>\n

    >>> import urllib.request\r\n>>> user_agent = ' Mozilla\/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko\/20100101 Firefox\/47.0'\r\n>>> url = 'http:\/\/www.whatsmyua.com\/'\r\n>>> headers = {'User-Agent': user_agent}\r\n>>> request = urllib.request.Request(url, headers=headers)\r\n>>> with urllib.request.urlopen(request) as response:\r\n...     with open('\/home\/mdriscoll\/Desktop\/user_agent.html', 'wb') as out:\r\n...         out.write(response.read())\r\n<\/pre>\n

    \u8fd9\u91cc\u8bbe\u7f6e\u6211\u4eec\u7684\u7528\u6237\u4ee3\u7406\u4e3a Mozilla FireFox \uff0c\u7136\u540e\u6211\u4eec\u8bbf\u95eehttp:\/\/www.whatsmyua.com\/ \uff0c \u5b83\u4f1a\u544a\u8bc9\u6211\u4eec\u5b83\u8bc6\u522b\u51fa\u7684\u6211\u4eec\u7684 user-agent \u5b57\u6bb5\u3002\u4e4b\u540e\u6211\u4eec\u5c06 url \u548c\u6211\u4eec\u7684\u5934\u4fe1\u606f\u4f20\u7ed9 urlopen \u521b\u5efa\u4e00\u4e2a Request \u5b9e\u4f8b\u3002\u6700\u540e\u6211\u4eec\u4fdd\u5b58\u8fd9\u4e2a\u7ed3\u679c\u3002\u5982\u679c\u4f60\u6253\u5f00\u8fd9\u4e2a\u7ed3\u679c\uff0c\u4f60\u4f1a\u770b\u5230\u6211\u4eec\u6210\u529f\u7684\u4fee\u6539\u4e86\u81ea\u5df1\u7684 user-agent \u5b57\u6bb5\u3002\u4f7f\u7528\u8fd9\u6bb5\u4ee3\u7801\u5c3d\u60c5\u7684\u5c1d\u8bd5\u4e0d\u540c\u7684\u503c\u6765\u770b\u770b\u5b83\u662f\u5982\u4f55\u6539\u53d8\u7684\u3002<\/p>\n

    urllib.parse<\/strong><\/div>\n

    urllib.parse \u5e93\u662f\u7528\u6765\u62c6\u5206\u548c\u7ec4\u5408 URL \u5b57\u7b26\u4e32\u7684\u6807\u51c6\u63a5\u53e3\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8f6c\u6362\u4e00\u4e2a\u76f8\u5bf9\u7684 URL \u4e3a\u7edd\u5bf9\u7684 URL\u3002\u8ba9\u6211\u4eec\u8bd5\u8bd5\u7528\u5b83\u6765\u8f6c\u6362\u4e00\u4e2a\u5305\u542b\u67e5\u8be2\u7684 URL \uff1a<\/p>\n

    >>> from urllib.parse import urlparse\r\n>>> result = urlparse('https:\/\/duckduckgo.com\/?q=python+stubbing&t=canonical&ia=qa')\r\n>>> result\r\nParseResult(scheme='https', netloc='duckduckgo.com', path='\/', params='', query='q=python+stubbing&t=canonical&ia=qa', fragment='')\r\n>>> result.netloc\r\n'duckduckgo.com'\r\n>>> result.geturl()\r\n'https:\/\/duckduckgo.com\/?q=python+stubbing&t=canonical&ia=qa'\r\n>>> result.port\r\nNone\r\n<\/pre>\n

    \u8fd9\u91cc\u6211\u4eec\u5bfc\u5165\u4e86\u51fd\u6570 urlparse \uff0c \u5e76\u4e14\u628a\u4e00\u4e2a\u5305\u542b\u641c\u7d22\u67e5\u8be2\u5b57\u4e32\u7684 duckduckgo \u7684 URL \u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u5b83\u3002\u6211\u7684\u67e5\u8be2\u5b57\u4e32\u662f\u641c\u7d22\u5173\u4e8e \u201cpython stubbing\u201d \u7684\u6587\u7ae0\u3002\u5982\u4f60\u6240\u89c1\uff0c\u5b83\u8fd4\u56de\u4e86\u4e00\u4e2a ParseResult \u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u7528\u8fd9\u4e2a\u5bf9\u8c61\u4e86\u89e3\u66f4\u591a\u5173\u4e8e URL \u7684\u4fe1\u606f\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u5230\u7aef\u53e3\u4fe1\u606f\uff08\u672c\u4f8b\u4e2d\u6ca1\u6709\u7aef\u53e3\u4fe1\u606f\uff09\u3001\u7f51\u7edc\u4f4d\u7f6e\u3001\u8def\u5f84\u548c\u5f88\u591a\u5176\u5b83\u4e1c\u897f\u3002<\/p>\n

    \u63d0\u4ea4\u4e00\u4e2a Web \u8868\u5355<\/strong><\/div>\n

    \u8fd9\u4e2a\u6a21\u5757\u8fd8\u6709\u4e00\u4e2a\u65b9\u6cd5 urlencode \u53ef\u4ee5\u5411 URL \u4f20\u8f93\u6570\u636e\u3002 urllib.parse \u7684\u4e00\u4e2a\u5178\u578b\u4f7f\u7528\u573a\u666f\u662f\u63d0\u4ea4 Web \u8868\u5355\u3002\u8ba9\u6211\u4eec\u901a\u8fc7\u641c\u7d22\u5f15\u64ce duckduckgo \u641c\u7d22 Python \u6765\u770b\u770b\u8fd9\u4e2a\u529f\u80fd\u662f\u600e\u4e48\u5de5\u4f5c\u7684\u3002<\/p>\n

    >>> import urllib.request\r\n>>> import urllib.parse\r\n>>> data = urllib.parse.urlencode({'q': 'Python'})\r\n>>> data\r\n'q=Python'\r\n>>> url = 'http:\/\/duckduckgo.com\/html\/'\r\n>>> full_url = url + '?' + data\r\n>>> response = urllib.request.urlopen(full_url)\r\n>>> with open('\/home\/mike\/Desktop\/results.html', 'wb') as f:\r\n...     f.write(response.read())\r\n<\/pre>\n

    \u8fd9\u4e2a\u4f8b\u5b50\u5f88\u76f4\u63a5\u3002\u57fa\u672c\u4e0a\u6211\u4eec\u662f\u4f7f\u7528 Python \u800c\u4e0d\u662f\u6d4f\u89c8\u5668\u5411 duckduckgo \u63d0\u4ea4\u4e86\u4e00\u4e2a\u67e5\u8be2\u3002\u8981\u5b8c\u6210\u8fd9\u4e2a\u6211\u4eec\u9700\u8981\u4f7f\u7528 urlencode \u6784\u5efa\u6211\u4eec\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u7136\u540e\u6211\u4eec\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u548c\u7f51\u5740\u62fc\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6b63\u786e URL \uff0c\u7136\u540e\u4f7f\u7528 urllib.request \u63d0\u4ea4\u8fd9\u4e2a\u8868\u5355\u3002\u6700\u540e\u6211\u4eec\u5c31\u83b7\u53d6\u5230\u4e86\u7ed3\u679c\u7136\u540e\u4fdd\u5b58\u5230\u78c1\u76d8\u4e0a\u3002<\/p>\n

    urllib.robotparser<\/strong><\/div>\n

    robotparser \u6a21\u5757\u662f\u7531\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b RobotFileParser \u6784\u6210\u7684\u3002\u8fd9\u4e2a\u7c7b\u4f1a\u56de\u7b54\u8bf8\u5982\u4e00\u4e2a\u7279\u5b9a\u7684\u7528\u6237\u4ee3\u7406\u662f\u5426\u83b7\u53d6\u5df2\u7ecf\u8bbe\u7f6e\u4e86 robot.txt \u7684\u7f51\u7ad9\u7684 URL\u3002 robot.txt \u6587\u4ef6\u4f1a\u544a\u8bc9\u7f51\u7edc\u722c\u866b\u6216\u8005\u673a\u5668\u4eba\u5f53\u524d\u7f51\u7ad9\u7684\u90a3\u4e9b\u90e8\u5206\u662f\u4e0d\u5141\u8bb8\u88ab\u8bbf\u95ee\u7684\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n

    >>> import urllib.robotparser\r\n>>> robot = urllib.robotparser.RobotFileParser()\r\n>>> robot.set_url('http:\/\/arstechnica.com\/robots.txt')\r\nNone\r\n>>> robot.read()\r\nNone\r\n>>> robot.can_fetch('*', 'http:\/\/arstechnica.com\/')\r\nTrue\r\n>>> robot.can_fetch('*', 'http:\/\/arstechnica.com\/cgi-bin\/')\r\nFalse\r\n<\/pre>\n

    \u8fd9\u91cc\u6211\u4eec\u5bfc\u5165\u4e86 robot \u5206\u6790\u5668\u7c7b\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u3002\u7136\u540e\u6211\u4eec\u7ed9\u5b83\u4f20\u9012\u4e00\u4e2a\u8868\u660e\u7f51\u7ad9 robots.txt \u4f4d\u7f6e\u7684 URL \u3002\u63a5\u4e0b\u6765\u6211\u4eec\u544a\u8bc9\u5206\u6790\u5668\u6765\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u3002\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u7ed9\u5b83\u4e86\u4e00\u7ec4\u4e0d\u540c\u7684 URL \u8ba9\u5b83\u627e\u51fa\u90a3\u4e9b\u6211\u4eec\u53ef\u4ee5\u722c\u53d6\u800c\u90a3\u4e9b\u4e0d\u80fd\u722c\u53d6\u3002\u6211\u4eec\u5f88\u5feb\u5c31\u770b\u5230\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u4e3b\u7ad9\u4f46\u662f\u4e0d\u80fd\u8bbf\u95ee cgi-bin \u8def\u5f84\u3002<\/p>\n

    \u603b\u7ed3\u4e00\u4e0b<\/strong><\/div>\n

    \u73b0\u5728\u4f60\u5c31\u6709\u80fd\u529b\u4f7f\u7528 Python \u7684 urllib \u5305\u4e86\u3002\u5728\u8fd9\u4e00\u8282\u91cc\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4e0b\u8f7d\u6587\u4ef6\u3001\u63d0\u4ea4 Web \u8868\u5355\u3001\u4fee\u6539\u81ea\u5df1\u7684\u7528\u6237\u4ee3\u7406\u4ee5\u53ca\u8bbf\u95ee robots.txt\u3002 urllib \u8fd8\u6709\u4e00\u5927\u5806\u9644\u52a0\u529f\u80fd\u6ca1\u6709\u5728\u8fd9\u91cc\u63d0\u53ca\uff0c\u6bd4\u5982\u7f51\u7ad9\u8eab\u4efd\u8ba4\u8bc1\u3002\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u5728\u4f7f\u7528 urllib \u8fdb\u884c\u8eab\u4efd\u8ba4\u8bc1\u4e4b\u524d\u5207\u6362\u5230 requests \u5e93\uff0c\u56e0\u4e3a requests \u5df2\u7ecf\u4ee5\u66f4\u6613\u7528\u548c\u6613\u8c03\u8bd5\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e86\u8fd9\u4e9b\u529f\u80fd\u3002\u6211\u540c\u65f6\u4e5f\u5e0c\u671b\u63d0\u9192\u4f60 Python \u5df2\u7ecf\u901a\u8fc7 http.cookies \u6a21\u5757\u652f\u6301 Cookies \u4e86\uff0c\u867d\u7136\u5728 request \u5305\u91cc\u4e5f\u5f88\u597d\u7684\u5c01\u88c5\u4e86\u8fd9\u4e2a\u529f\u80fd\u3002\u4f60\u5e94\u8be5\u53ef\u80fd\u8003\u8651\u540c\u65f6\u8bd5\u8bd5\u4e24\u4e2a\u6765\u51b3\u5b9a\u90a3\u4e2a\u6700\u9002\u5408\u4f60\u3002<\/p>\n

    \n

    \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.blog.pythonlibrary.org\/2016\/06\/28\/python-101-an-intro-to-urllib\/<\/a><\/p>\n

    \u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/python-urllib.html<\/a>\u7f16\u8f91\uff1a\u5f20\u96c4\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

    Python 3 \u7684 urllib \u6a21\u5757\u662f\u4e00\u5806\u53ef\u4ee5\u5904\u7406 URL \u7684\u7ec4\u4ef6\u96c6\u5408\u3002\u5982\u679c\u4f60\u6709 Python 2 \u7684\u77e5\u8bc6\uff0c\u90a3\u4e48\u4f60\u5c31\u4f1a\u6ce8\u610f\u5230 Python 2 \u4e2d\u6709 urllib \u548c urllib2 \u4e24\u4e2a\u7248\u672c\u7684\u6a21\u5757\u3002\u8fd9\u4e9b\u73b0\u5728\u90fd\u662f Python 3 \u7684 urllib \u5305\u7684\u4e00\u90e8\u5206\u3002\u5f53\u524d\u7248\u672c\u7684 urllib \u5305\u62ec\u4e0b\u9762\u51e0\u90e8\u5206\uff1a<\/p>\n","protected":false},"author":643,"featured_media":25676,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-25625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/25625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=25625"}],"version-history":[{"count":17,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/25625\/revisions"}],"predecessor-version":[{"id":80548,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/25625\/revisions\/80548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/25676"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=25625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=25625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=25625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}