{"id":402926,"date":"2024-02-21T06:37:16","date_gmt":"2024-02-20T22:37:16","guid":{"rendered":"https:\/\/www.idc.net\/help\/402926\/"},"modified":"2024-02-21T06:37:16","modified_gmt":"2024-02-20T22:37:16","slug":"%e6%9c%89%e5%85%b3python%e6%a8%a1%e5%9d%97%e5%86%85%e5%ae%b9%e8%af%b4%e6%98%8e%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/402926\/","title":{"rendered":"\u6709\u5173Python\u6a21\u5757\u5185\u5bb9\u8bf4\u660e\u4ecb\u7ecd"},"content":{"rendered":"<p>\u5728Python\u8bed\u8a00\u4e2d\u5177\u6709\u5927\u91cf\u4f18\u79c0\u7684\u5e93\u53ef\u4ee5\u88ab\u76f4\u63a5\u8c03\u7528\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u4e0d\u540c\u9700\u6c42\u7684\u5de5\u4f5c\uff0c\u8fd9\u91cc\u5217\u4e3e\u4e00\u4e9b\u5e38\u89c1\u5e38\u7528\u7684\u5e93\uff0c\u548c\u4e00\u4e9bPython\u6a21\u5757\uff0c\u5728Python\u7a0b\u5e8f\u4e2d\u8c03\u7528C++\u5f00\u53d1\u7684\u6269\u5c55\u529f\u80fd\u51fd\u6570\u3002<\/p>\n<p>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528<font>Python<\/font>\u6293\u53d6\u548c\u89e3\u6790\u7f51\u9875\u3002\u9996\u5148\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e2a\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u6253\u5f00\u4f4d\u4e8e\u672c\u5730\u548cWeb\u4e0a\u7684HTML\u6587\u6863\u7684<font>Python\u6a21\u5757<\/font>\u3002\u540e\uff0c\u6211\u4eec\u8bba\u8ff0\u5982\u4f55\u4f7f\u7528<font>Python\u6a21\u5757<\/font>\u6765\u8fc5\u901f\u89e3\u6790\u5728HTML\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u4ece\u800c\u5904\u7406\u7279\u5b9a\u7684\u5185\u5bb9\uff0c\u5982\u94fe\u63a5\u3001\u56fe\u50cf\u548cCookie\u7b49\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u89c4\u6574HTML\u6587\u4ef6\u7684\u683c\u5f0f\u6807\u7b7e\u7684\u4f8b\u5b50\uff0c\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\u60a8\u4f1a\u53d1\u73b0\u4f7f\u7528<font>python<\/font>\u5904\u7406HTML\u6587\u4ef6\u7684\u5185\u5bb9\u662f\u975e\u5e38\u7b80\u5355\u7684\u4e00\u4ef6\u4e8b\u60c5\u3002\u901a\u8fc7<font>Python<\/font>\u6240\u5e26\u7684urlparse\u6a21\u5757\uff0c\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u5730\u628aURL\u5206\u89e3\u6210\u5143\u4ef6\uff0c\u4e4b\u540e\uff0c\u8fd8\u80fd\u5c06\u8fd9\u4e9b\u5143\u4ef6\u91cd\u65b0\u7ec4\u88c5\u6210\u4e00\u4e2aURL\u3002\u5f53\u6211\u4eec\u5904\u7406HTML \u6587\u6863\u7684\u65f6\u5019\uff0c\u8fd9\u9879\u529f\u80fd\u662f\u975e\u5e38\u65b9\u4fbf\u7684\u3002<\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>import&nbsp;urlparse &nbsp;<\/span><\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000<span><font>parsedTuple<\/font><span>&nbsp;=&nbsp;<span><font>urlparse<\/font><span>.urlparse( &nbsp;<\/span><\/span><\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000\"http:\/\/www.google.com\/search? &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000<span><font>hl<\/font><span>=<span><font>en<\/font><span>&amp;<span><font>q<\/font><span>=<span><font>urlparse<\/font><span>&amp;<span><font>btnG<\/font><span>=<span><font>Google<\/font><span>+Search\") &nbsp;<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000<span><font>unparsedURL<\/font><span>&nbsp;=&nbsp;<span><font>urlparse<\/font><span>.urlunparse((URLscheme,&nbsp;\\ &nbsp;<\/span><\/span><\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000URLlocation,&nbsp;URLpath,&nbsp;'',&nbsp;'',&nbsp;'')) &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000<span><font>newURL<\/font><span>&nbsp;=&nbsp;<span><font>urlparse<\/font><span>.urljoin(unparsedURL, &nbsp;<\/span><\/span><\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000\"\/module-urllib2\/request-objects.html\")&nbsp;<\/span><\/li>\n \n \n <\/ol><\/pre>\n<p>\u51fd\u6570urlparse(urlstring [, default_scheme [, allow_fragments]])\u7684\u4f5c\u7528\u662f\u5c06URL\u5206\u89e3\u6210\u4e0d\u540c\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u4eceurlstring\u4e2d\u53d6\u5f97URL\uff0c\u5e76\u8fd4\u56de\u5143\u7ec4 (scheme, netloc, path, parameters, query, fragment)\u3002\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u8fd9\u4e2a\u5143\u7ec4\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u7f51\u7edc\u534f\u8bae(HTTP\u3001FTP\u7b49\u7b49 )\u3001\u670d\u52a1\u5668\u5730\u5740\u3001\u6587\u4ef6\u8def\u5f84\uff0c\u7b49\u7b49\u3002<\/p>\n<p>\u51fd\u6570urlunparse(tuple)\u7684\u4f5c\u7528\u662f\u5c06URL\u7684\u7ec4\u4ef6\u88c5\u914d\u6210\u4e00\u4e2aURL\uff0c\u5b83\u63a5\u6536\u5143\u7ec4(scheme, netloc, path, parameters, query, fragment)\u540e\uff0c\u4f1a\u91cd\u65b0\u7ec4\u6210\u4e00\u4e2a\u5177\u6709\u6b63\u786e\u683c\u5f0f\u7684URL\uff0c\u4ee5\u4fbf\u4f9b<font>Python<\/font>\u7684\u5176\u4ed6HTML\u89e3\u6790\u6a21\u5757\u4f7f\u7528\u3002<\/p>\n<p>\u51fd\u6570urljoin(base, url [, allow_fragments]) \u7684\u4f5c\u7528\u662f\u62fc\u63a5URL\uff0c\u5b83\u4ee5\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u5176\u57fa\u5730\u5740\uff0c\u7136\u540e\u4e0e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e2d\u7684\u76f8\u5bf9\u5730\u5740\u76f8\u7ed3\u5408\u7ec4\u6210\u4e00\u4e2a\u7edd\u5bf9URL\u5730\u5740\u3002\u51fd\u6570urljoin\u5728\u901a\u8fc7\u4e3aURL\u57fa\u5730\u5740\u9644\u52a0\u65b0\u7684\u6587\u4ef6\u540d\u7684\u65b9\u5f0f\u6765\u5904\u7406\u540c\u4e00\u4f4d\u7f6e\u5904\u7684\u82e5\u5e72\u6587\u4ef6\u7684\u65f6\u5019\u683c\u5916\u6709\u7528\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u57fa\u5730\u5740\u5e76\u975e\u4ee5\u5b57\u7b26\/\u7ed3\u5c3e\u7684\u8bdd\uff0c\u90a3\u4e48URL\u57fa\u5730\u5740\u6700\u53f3\u8fb9\u90e8\u5206\u5c31\u4f1a\u88ab\u8fd9\u4e2a\u76f8\u5bf9\u8def\u5f84\u6240\u66ff\u6362\u3002\u6bd4\u5982\uff0cURL\u7684\u57fa\u5730\u5740\u4e3aHttp:\/\/www.testpage.com\/pub\uff0cURL\u7684\u76f8\u5bf9\u5730\u5740\u4e3atest.html\uff0c\u90a3\u4e48\u4e24\u8005\u5c06\u5408\u5e76\u6210\u5982\u679c\u5e0c\u671b\u5728\u8be5\u8def\u5f84\u4e2d\u4fdd\u7559\u672b\u7aef\u76ee\u5f55\uff0c\u5e94\u786e\u4fddURL\u57fa\u5730\u5740\u4ee5\u5b57\u7b26\/\u7ed3\u5c3e\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e0a\u9762\u51e0\u4e2a\u51fd\u6570\u7684\u8be6\u7ec6\u4e00\u70b9\u7684\u7528\u6cd5\u4e3e\u4f8b\uff1a<\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>Date:&nbsp;Fri,&nbsp;26&nbsp;Jun&nbsp;2009&nbsp;10:22:11&nbsp;GMT &nbsp;<\/span><\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Server:&nbsp;Apache\/2.2.9&nbsp;(Debian)&nbsp;DAV\/2&nbsp;SVN\/1.5.1&nbsp;mod_ssl\/2.2.9&nbsp;OpenSSL\/0.9.8g&nbsp;mod_wsgi\/2.3&nbsp;<font>Python<\/font>\/2.5.2 &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Last-Modified:&nbsp;Thu,&nbsp;25&nbsp;Jun&nbsp;2009&nbsp;09:44:54&nbsp;GMT &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000ETag:&nbsp;\"105800d-46e7-46d29136f7180\" &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Accept-Ranges:&nbsp;bytes &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Content-Length:&nbsp;18151 &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Connection:&nbsp;close &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Content-Type:&nbsp;text\/html &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000\u4ecehttp:\/\/www.<font>python<\/font>.org\u8bfb\u53d6\u4e8618151&nbsp;\u5b57\u8282\u6570\u636e. &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Content-Type:&nbsp;text\/html &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Content-Length:&nbsp;865 &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000Last-modified:&nbsp;Fri,&nbsp;26&nbsp;Jun&nbsp;2009&nbsp;10:16:10&nbsp;GMT &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>\u3000\u3000\u4eceindex.html\u8bfb\u53d6\u4e86865&nbsp;\u5b57\u8282\u6570\u636e.&nbsp;<\/span><\/li>\n \n \n <\/ol><\/pre>\n<p>\u82e5\u8981\u901a\u8fc7urllib\u6a21\u5757\u4e2d\u7684urlopen(url [,data])\u51fd\u6570\u6253\u5f00\u4e00\u4e2aHTML\u6587\u6863\uff0c\u5fc5\u987b\u63d0\u4f9b\u8be5\u6587\u6863\u7684URL\u5730\u5740\uff0c\u5305\u62ec\u6587\u4ef6\u540d\u3002\u51fd\u6570urlopen\u4e0d\u4ec5\u53ef\u4ee5\u6253\u5f00\u4f4d\u4e8e\u8fdc\u7a0bweb\u670d\u52a1\u5668\u4e0a\u7684\u6587\u4ef6\uff0c\u800c\u4e14\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u672c\u5730\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7c7b\u4f3c\u6587\u4ef6\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be5\u5bf9\u8c61\u4eceHTML\u6587\u6863\u4e2d\u8bfb\u51fa\u6570\u636e\u3002<\/p>\n<p>\u4e00\u65e6\u6253\u5f00\u4e86HTML\u6587\u6863\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u5e38\u89c4\u6587\u4ef6\u4e00\u6837\u4f7f\u7528read([nbytes])\u3001readline()\u548creadlines()\u51fd\u6570\u6765\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u64cd\u4f5c\u3002\u82e5\u8981\u8bfb\u53d6\u6574\u4e2aHTML\u6587\u6863\u7684\u5185\u5bb9\u7684\u8bdd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528read()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002<\/p>\n<p>\u6253\u5f00\u4e00\u4e2a\u5730\u5740\u4e4b\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<font>Python\u6a21\u5757<\/font>\u53d6\u5f97\u88ab\u83b7\u53d6\u7f51\u9875\u7684\u771f\u6b63\u7684URL\u3002\u8fd9\u662f\u5f88\u6709\u7528\u7684\uff0c\u56e0\u4e3aurlopen(\u6216\u4f7f\u7528\u7684opener\u5bf9\u8c61)\u4e5f\u8bb8\u4f1a\u4f34\u968f\u4e00\u4e2a\u91cd\u5b9a\u5411\u3002\u83b7\u53d6\u7684\u7f51\u9875URL\u4e5f\u8bb8\u548c\u8981\u6c42\u7684\u7f51\u9875URL\u4e0d\u4e00\u6837\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\u662f\u4f4d\u4e8e\u4eceurlopen\u8fd4\u56de\u7684\u7c7b\u6587\u4ef6\u5bf9\u8c61\u4e2d\u7684info()\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8fd4\u56deURL\u4f4d\u7f6e\u6709\u5173\u7684\u5143\u6570\u636e\uff0c\u6bd4\u5982\u5185\u5bb9\u957f\u5ea6\u3001\u5185\u5bb9\u7c7b\u578b\uff0c\u7b49\u7b49\u3002\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u4f8b\u5b50\u6765\u5bf9\u8fd9\u4e9b\u51fd\u6570\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<p>\u3010\u7f16\u8f91\u63a8\u8350\u3011<\/p>\n<ol>\n<li>\u6709\u5173Python\u7cfb\u7edf\u6587\u4ef6\u8fdb\u884c\u4ecb\u7ecd\u6307\u5bfc <\/li>\n<li>\u5982\u4f55\u6b63\u786e\u7684\u4f7f\u7528Python\u51fd\u6570<\/li>\n<li>\u5bf9Python \u6784\u5efa\u5de5\u5177\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u5206\u6790 <\/li>\n<li>PythonAndroid\u6d45\u6790Python\u4f18\u52bf\u6240\u5728<\/li>\n<li>\u5982\u4f55\u4f7f\u7528Python\u6a21\u5757\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\uff1f <\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u8bed\u8a00\u4e2d\u5177\u6709\u5927\u91cf\u4f18\u79c0\u7684\u5e93\u53ef\u4ee5\u88ab\u76f4\u63a5\u8c03\u7528\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u4e0d\u540c\u9700\u6c42\u7684\u5de5\u4f5c\uff0c\u8fd9\u91cc\u5217\u4e3e\u4e00\u4e9b\u5e38\u89c1\u5e38\u7528\u7684\u5e93\uff0c\u548c\u4e00\u4e9b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-402926","post","type-post","status-publish","format-standard","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/402926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=402926"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/402926\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=402926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=402926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=402926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}