{"id":928954,"date":"2024-12-26T16:46:24","date_gmt":"2024-12-26T08:46:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/928954.html"},"modified":"2024-12-26T16:46:27","modified_gmt":"2024-12-26T08:46:27","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8b%e8%bd%bdurllib","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/928954.html","title":{"rendered":"Python\u5982\u4f55\u4e0b\u8f7durllib"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064411\/c30e949d-4dfd-426f-bc50-4e0f3e1e8f7d.webp\" alt=\"Python\u5982\u4f55\u4e0b\u8f7durllib\" \/><\/p>\n<p><p> Python\u7684<code>urllib<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65e0\u9700\u5355\u72ec\u4e0b\u8f7d\u6216\u5b89\u88c5\u5b83\u3002<strong>\u53ea\u9700\u786e\u4fdd\u5df2\u5b89\u88c5Python\uff0c\u4fbf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>urllib<\/code>\u5e93\u3001<code>urllib<\/code>\u5e93\u901a\u5e38\u5305\u62ec\u5728Python\u7684\u6807\u51c6\u5b89\u88c5\u5305\u4e2d<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u53ea\u8981\u4f60\u5b89\u88c5\u4e86Python\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528<code>urllib<\/code>\u3002\u5982\u679c\u4f60\u53d1\u73b0\u7f3a\u5c11\u8be5\u5e93\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86\u4e0d\u5b8c\u6574\u7684Python\u73af\u5883\u6216\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u5b89\u88c5Python\u6216\u68c0\u67e5\u865a\u62df\u73af\u5883\u914d\u7f6e\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5982\u4f55\u786e\u8ba4<code>urllib<\/code>\u5df2\u5b89\u88c5<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5Python\u7248\u672c<\/strong>\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662fPython 3.x\uff0c\u56e0\u4e3aPython 2.x\u5df2\u7ecf\u505c\u6b62\u652f\u6301\uff0c\u800c<code>urllib<\/code>\u7684\u4f7f\u7528\u5728\u8fd9\u4e24\u4e2a\u7248\u672c\u4e2d\u7565\u6709\u4e0d\u540c\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u6765\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5\u5bfc\u5165<code>urllib<\/code><\/strong>\uff1a\u5728Python\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u68c0\u67e5\u662f\u5426\u53ef\u4ee5\u6210\u529f\u5bfc\u5165<code>urllib<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u5219\u8868\u793a<code>urllib<\/code>\u5e93\u5df2\u7ecf\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>urllib<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u5373\u4f7f\u4e0d\u9700\u8981\u4e0b\u8f7d\u6216\u5b89\u88c5<code>urllib<\/code>\uff0c\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u5b83\u4f9d\u7136\u5f88\u91cd\u8981\u3002<code>urllib<\/code>\u5e93\u4e3b\u8981\u7528\u4e8e\u5904\u7406URL\u548c\u7f51\u7edc\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u5176\u4e3b\u8981\u6a21\u5757\u548c\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001<code>urllib.request<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>urllib.request<\/code>\u6a21\u5757\u7528\u4e8e\u6253\u5f00\u548c\u8bfb\u53d6URLs\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u7684\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<p>response = urllib.request.urlopen(&#39;http:\/\/example.com\/&#39;)<\/p>\n<p>html = response.read()<\/p>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\u5e76\u8bfb\u53d6\u54cd\u5e94\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u8bf7\u6c42\u5934<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<p>req = urllib.request.Request(&#39;http:\/\/example.com\/&#39;)<\/p>\n<p>req.add_header(&#39;User-Agent&#39;, &#39;Mozilla\/5.0&#39;)<\/p>\n<p>response = urllib.request.urlopen(req)<\/p>\n<p>html = response.read()<\/p>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>Request<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u4ee5\u6a21\u62df\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u6216\u8bbe\u5907\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2\u3001<code>urllib.parse<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>urllib.parse<\/code>\u6a21\u5757\u7528\u4e8e\u89e3\u6790URLs\u3002\u5b83\u63d0\u4f9b\u4e86URL\u89e3\u6790\u548c\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u89e3\u6790URL<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from urllib.parse import urlparse<\/p>\n<p>result = urlparse(&#39;http:\/\/www.example.com\/index.html;user?id=5#comment&#39;)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u6a21\u5757\u53ef\u4ee5\u5206\u89e3URL\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u83b7\u53d6\u534f\u8bae\u3001\u57df\u540d\u3001\u8def\u5f84\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6784\u5efaURL<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from urllib.parse import urlencode, urlunparse<\/p>\n<p>params = {&#39;name&#39;: &#39;John Doe&#39;, &#39;age&#39;: 30}<\/p>\n<p>query_string = urlencode(params)<\/p>\n<p>url = urlunparse((&#39;http&#39;, &#39;www.example.com&#39;, &#39;\/path&#39;, &#39;&#39;, query_string, &#39;&#39;))<\/p>\n<p>print(url)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6784\u5efaURL\uff0c\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u5e26\u6709\u67e5\u8be2\u53c2\u6570\u7684\u5b8c\u6574URL\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>3\u3001<code>urllib.error<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>urllib.error<\/code>\u6a21\u5757\u5b9a\u4e49\u4e86\u5f02\u5e38\u7c7b\uff0c\u7528\u4e8e\u5904\u7406<code>urllib.request<\/code>\u5f15\u53d1\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6355\u83b7HTTP\u9519\u8bef<\/strong>\uff1a\n<pre><code class=\"language-python\">import urllib.request<\/p>\n<p>from urllib.error import HTTPError, URLError<\/p>\n<p>try:<\/p>\n<p>    response = urllib.request.urlopen(&#39;http:\/\/www.nonexistentwebsite.com\/&#39;)<\/p>\n<p>except HTTPError as e:<\/p>\n<p>    print(&#39;HTTP error:&#39;, e.code)<\/p>\n<p>except URLError as e:<\/p>\n<p>    print(&#39;URL error:&#39;, e.reason)<\/p>\n<p>else:<\/p>\n<p>    html = response.read()<\/p>\n<p>    print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u4e2d\u7684\u9519\u8bef\u3002<\/li>\n<\/p>\n<\/ul>\n<p><h4>4\u3001<code>urllib.robotparser<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>urllib.robotparser<\/code>\u6a21\u5757\u7528\u6765\u89e3\u6790robots.txt\u6587\u4ef6\uff0c\u5224\u65ad\u67d0\u4e2a\u722c\u866b\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u67d0\u4e2a\u7f51\u9875\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u89e3\u6790robots.txt<\/strong>\uff1a\n<pre><code class=\"language-python\">from urllib.robotparser import RobotFileParser<\/p>\n<p>rp = RobotFileParser()<\/p>\n<p>rp.set_url(&#39;http:\/\/www.example.com\/robots.txt&#39;)<\/p>\n<p>rp.read()<\/p>\n<p>print(rp.can_fetch(&#39;*&#39;, &#39;http:\/\/www.example.com\/page&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u5e2e\u52a9\u5f00\u53d1\u8005\u9075\u5faa\u7f51\u7ad9\u7684\u722c\u866b\u534f\u8bae\uff0c\u907f\u514d\u8bbf\u95ee\u4e0d\u5141\u8bb8\u7684\u9875\u9762\u3002<\/li>\n<\/p>\n<\/ul>\n<p><h3>\u4e09\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>ImportError: No module named &#39;urllib&#39;<\/strong>\uff1a\u786e\u4fdd\u4f7f\u7528\u7684\u662fPython 3.x\uff0cPython 2.x\u7684<code>urllib<\/code>\u5728Python 3.x\u4e2d\u62c6\u5206\u4e3a\u591a\u4e2a\u5b50\u6a21\u5757\uff08\u5982<code>urllib.request<\/code>\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Connection errors<\/strong>\uff1a\u82e5\u9047\u5230\u7f51\u7edc\u8fde\u63a5\u9519\u8bef\uff0c\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u662f\u5426\u6b63\u5e38\uff0c\u6216\u8005\u76ee\u6807URL\u662f\u5426\u6709\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>SSL: CERTIFICATE_VERIFY_F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>LED<\/strong>\uff1a\u5982\u679c\u8bf7\u6c42HTTPS\u8d44\u6e90\u65f6\u51fa\u73b0\u6b64\u9519\u8bef\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u8bbe\u7f6e\u5ffd\u7565SSL\u9a8c\u8bc1\uff08\u4ec5\u7528\u4e8e\u5f00\u53d1\u73af\u5883\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ssl<\/p>\n<p>ssl._create_default_https_context = ssl._create_unverified_context<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Timeout errors<\/strong>\uff1a\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u53c2\u6570\uff0c\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u7b49\u5f85\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response = urllib.request.urlopen(&#39;http:\/\/example.com\/&#39;, timeout=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3Python\u6807\u51c6\u5e93\u4e2d\u7684<code>urllib<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u3001\u89e3\u6790\u548c\u6784\u5efaURLs\uff0c\u5e76\u9075\u5faa\u4e92\u8054\u7f51\u722c\u866b\u534f\u8bae\u8fdb\u884c\u6570\u636e\u6293\u53d6\u3002\u867d\u7136\u4e0d\u9700\u8981\u5355\u72ec\u4e0b\u8f7d<code>urllib<\/code>\u5e93\uff0c\u4f46\u638c\u63e1\u5176\u7528\u6cd5\u662f\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u3002\u501f\u52a9<code>urllib<\/code>\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u5404\u79cdHTTP\u8bf7\u6c42\u548cURL\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528pip\u5b89\u88c5urllib\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b89\u88c5urllib\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u3002\u53ea\u9700\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\uff0c\u8f93\u5165<code>pip install urllib3<\/code>\u5373\u53ef\u3002\u8bf7\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\u5de5\u5177\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165urllib\u5e93\uff0c\u8fdb\u884c\u76f8\u5e94\u7684\u7f51\u7edc\u8bf7\u6c42\u548c\u6570\u636e\u5904\u7406\u3002<\/p>\n<p><strong>urllib\u4e0eurllib3\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>urllib\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406URL\u64cd\u4f5c\uff0c\u5982\u6253\u5f00\u548c\u8bfb\u53d6URL\u3002urllib3\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5305\u62ec\u8fde\u63a5\u6c60\u3001\u6587\u4ef6\u4f20\u8f93\u548c\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u4f7f\u7528urllib3\u4f1a\u66f4\u4e3a\u9ad8\u6548\u548c\u7075\u6d3b\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u4e0b\u8f7durllib\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4e0b\u8f7d\u548c\u5b89\u88c5urllib\u65f6\uff0c\u786e\u4fdd\u60a8\u7684Python\u7248\u672c\u4e0eurllib\u5e93\u517c\u5bb9\u3002\u6b64\u5916\uff0c\u907f\u514d\u5728\u865a\u62df\u73af\u5883\u4e4b\u5916\u5b89\u88c5\u5e93\uff0c\u4ee5\u9632\u6b62\u7248\u672c\u51b2\u7a81\u3002\u4f7f\u7528<code>pip list<\/code>\u53ef\u4ee5\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\u53ca\u5176\u7248\u672c\uff0c\u786e\u4fdd\u60a8\u7684\u73af\u5883\u6574\u6d01\u3002\u82e5\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u6216\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08Linux\/Mac\u7cfb\u7edf\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7684urllib\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65e0\u9700\u5355\u72ec\u4e0b\u8f7d\u6216\u5b89\u88c5\u5b83\u3002\u53ea\u9700\u786e\u4fdd\u5df2 [&hellip;]","protected":false},"author":3,"featured_media":928955,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/928954"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=928954"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/928954\/revisions"}],"predecessor-version":[{"id":928956,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/928954\/revisions\/928956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/928955"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=928954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=928954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=928954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}