{"id":969663,"date":"2024-12-27T05:19:01","date_gmt":"2024-12-26T21:19:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/969663.html"},"modified":"2024-12-27T05:19:04","modified_gmt":"2024-12-26T21:19:04","slug":"%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96python%e7%bd%91%e7%bb%9c%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/969663.html","title":{"rendered":"\u5982\u4f55\u6293\u53d6python\u7f51\u7edc\u722c\u866b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183822\/16413939-611f-49a0-9e22-515389d668fd.webp\" alt=\"\u5982\u4f55\u6293\u53d6python\u7f51\u7edc\u722c\u866b\" \/><\/p>\n<p><p> <strong>\u6293\u53d6Python\u7f51\u7edc\u722c\u866b\u7684\u5173\u952e\u5728\u4e8e\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u5904\u7406\u6570\u636e\u3001\u9075\u5b88\u7f51\u7edc\u9053\u5fb7\u3002<\/strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u7f51\u7edc\u722c\u866b\u7684\u57fa\u7840\uff0cPython\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u5e93\uff0c\u5982Requests\u548cBeautifulSoup\uff0c\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u901a\u5e38\u9700\u8981\u7ed3\u5408\u4f7f\u7528Requests\u548cBeautifulSoup\u6765\u83b7\u53d6\u548c\u89e3\u6790HTML\u6570\u636e\u3002\u5904\u7406\u6570\u636e\u662f\u6839\u636e\u9700\u6c42\u63d0\u53d6\u548c\u5b58\u50a8\u6709\u7528\u4fe1\u606f\u7684\u8fc7\u7a0b\uff0c\u8fd9\u53ef\u80fd\u6d89\u53ca\u5230\u6e05\u7406\u548c\u683c\u5f0f\u5316\u6570\u636e\u3002\u6700\u540e\uff0c\u9075\u5b88\u7f51\u7edc\u9053\u5fb7\u81f3\u5173\u91cd\u8981\uff0c\u786e\u4fdd\u722c\u866b\u884c\u4e3a\u5408\u6cd5\u548c\u9053\u5fb7\uff0c\u4f8b\u5982\u9075\u5b88robots.txt\u6587\u4ef6\u7684\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u6210\u529f\u5b9e\u65bd\u7f51\u7edc\u722c\u866b\u7684\u7b2c\u4e00\u6b65\u3002<strong>Requests\u5e93<\/strong>\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u63a5\u6536\u54cd\u5e94\u3002\u4e0e\u5176\u4ed6\u5e93\u76f8\u6bd4\uff0cRequests\u7684\u8bbe\u8ba1\u66f4\u4e3a\u4eba\u6027\u5316\uff0c\u4ee3\u7801\u66f4\u6613\u8bfb\u548c\u7ef4\u62a4\u3002\u4f7f\u7528Requests\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u4e3a\u540e\u7eed\u7684\u89e3\u6790\u548c\u6570\u636e\u5904\u7406\u5960\u5b9a\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5b9e\u65bd\u548c\u4f18\u5316Python\u7f51\u7edc\u722c\u866b\u7684\u5404\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Requests\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Requests\u5e93\u662fPython\u4e2d\u5904\u7406HTTP\u8bf7\u6c42\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\u3002\u4f7f\u7528Requests\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u76ee\u6807\u7f51\u7ad9\u53d1\u9001GET\u3001POST\u7b49\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u3002\u5b83\u652f\u6301\u4f1a\u8bdd\u3001SSL\u8ba4\u8bc1\u3001\u4ee3\u7406\u7b49\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8fdb\u884c\u57fa\u7840\u722c\u866b\u5f00\u53d1\u7684\u7528\u6237\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Requests\u5e93\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\u5e76\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801\u662f\u5426\u4e3a200\uff08\u8bf7\u6c42\u6210\u529f\uff09\uff0c\u7136\u540e\u8f93\u51fa\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528BeautifulSoup\u5e93<\/strong><\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684Python\u5e93\uff0c\u80fd\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u67e5\u627e\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u4f7f\u5f97\u89e3\u6790\u590d\u6742\u7684HTML\u7ed3\u6784\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>title = soup.find(&#39;title&#39;).text<\/p>\n<p>print(title)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7BeautifulSoup\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6807\u7b7e\u5185\u5bb9\uff0c\u4f8b\u5982\uff0c\u83b7\u53d6\u7f51\u9875\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408\u4f7f\u7528Scrapy\u6846\u67b6<\/strong><\/p>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u4e8e\u9700\u8981\u6293\u53d6\u5927\u91cf\u6570\u636e\u7684\u9879\u76ee\u3002\u4e0eRequests\u548cBeautifulSoup\u4e0d\u540c\uff0cScrapy\u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u7684\u529f\u80fd\uff0c\u5305\u62ec\u8bf7\u6c42\u8c03\u5ea6\u3001\u54cd\u5e94\u89e3\u6790\u3001\u6570\u636e\u7ba1\u9053\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Scrapy\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u722c\u866b\u9879\u76ee\uff0c\u5b9a\u4e49\u722c\u866b\u3001\u89e3\u6790\u5668\u548c\u6570\u636e\u7ba1\u9053\uff0c\u652f\u6301\u5206\u5e03\u5f0f\u722c\u866b\u548c\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u89e3\u6790HTML\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u4e86\u7f51\u9875\u7684HTML\u5185\u5bb9\u4e4b\u540e\uff0c\u89e3\u6790HTML\u7ed3\u6784\u662f\u63d0\u53d6\u6709\u7528\u6570\u636e\u7684\u5173\u952e\u3002\u901a\u8fc7BeautifulSoup\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5bfc\u822aHTML\u6587\u6863\u6811\uff0c\u67e5\u627e\u5e76\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">links = soup.find_all(&#39;a&#39;, href=True)<\/p>\n<p>for link in links:<\/p>\n<p>    print(link[&#39;href&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528BeautifulSoup\u63d0\u53d6\u6240\u6709\u94fe\u63a5\uff0c\u5e76\u8f93\u51fa\u94fe\u63a5\u7684URL\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u52a8\u6001\u7f51\u9875<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u662f\u52a8\u6001\u751f\u6210\u7684\uff0c\u4f7f\u7528JavaScript\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\u5e76\u6e32\u67d3\u5230\u9875\u9762\u4e0a\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>page_source = driver.page_source<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Selenium\u53ef\u4ee5\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u52a8\u6001\u7f51\u9875\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5904\u7406\u6570\u636e<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u6e05\u7406\u4e0e\u683c\u5f0f\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u63d0\u53d6\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u6e05\u7406\u548c\u683c\u5f0f\u5316\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u5b58\u50a8\u3002\u6570\u636e\u6e05\u7406\u53ef\u80fd\u5305\u62ec\u5220\u9664HTML\u6807\u7b7e\u3001\u53bb\u9664\u7a7a\u683c\u3001\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">clean_text = &#39; &#39;.join(title.split())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6e05\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5b58\u50a8<\/strong><\/p>\n<\/p>\n<p><p>\u6293\u53d6\u5230\u7684\u6570\u636e\u9700\u8981\u5b58\u50a8\u5230\u5408\u9002\u7684\u5b58\u50a8\u4ecb\u8d28\u4e2d\uff0c\u5e38\u89c1\u7684\u5b58\u50a8\u65b9\u5f0f\u5305\u62ecCSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5b58\u50a8\uff0c\u4f7f\u7528\u6570\u636e\u5e93\u5982MySQL\u3001MongoDB\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Title&#39;, &#39;URL&#39;])<\/p>\n<p>    writer.writerow([title, &#39;http:\/\/example.com&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6570\u636e\u5199\u5165\u5230CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u9075\u5b88\u7f51\u7edc\u9053\u5fb7<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9075\u5b88robots.txt\u534f\u8bae<\/strong><\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u7f51\u7ad9\u5728\u6839\u76ee\u5f55\u4e0b\u90fd\u6709\u4e00\u4e2arobots.txt\u6587\u4ef6\uff0c\u89c4\u5b9a\u4e86\u54ea\u4e9b\u9875\u9762\u5141\u8bb8\u6216\u4e0d\u5141\u8bb8\u88ab\u722c\u866b\u6293\u53d6\u3002\u9075\u5b88robots.txt\u534f\u8bae\u662f\u7f51\u7edc\u722c\u866b\u5f00\u53d1\u8005\u7684\u57fa\u672c\u9053\u5fb7\u4e49\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com\/robots.txt&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u83b7\u53d6\u5e76\u89e3\u6790robots.txt\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u7f51\u7ad9\u7684\u722c\u866b\u6293\u53d6\u653f\u7b56\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9650\u5236\u8bf7\u6c42\u9891\u7387<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4e0d\u5bf9\u76ee\u6807\u7f51\u7ad9\u9020\u6210\u538b\u529b\uff0c\u5e94\u8be5\u5408\u7406\u63a7\u5236\u8bf7\u6c42\u9891\u7387\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5ef6\u65f6\u6216\u4f7f\u7528Scrapy\u7684\u4e0b\u8f7d\u5ef6\u8fdf\u529f\u80fd\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>time.sleep(2)  # \u7b49\u5f852\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5408\u7406\u7684\u8bf7\u6c42\u95f4\u9694\u6709\u52a9\u4e8e\u51cf\u8f7b\u5bf9\u670d\u52a1\u5668\u7684\u8d1f\u62c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5408\u6cd5\u5408\u89c4\u4f7f\u7528\u722c\u866b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u722c\u866b\u6280\u672f\u65f6\uff0c\u786e\u4fdd\u9075\u5b88\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u4e0d\u6293\u53d6\u654f\u611f\u6216\u672a\u7ecf\u6388\u6743\u7684\u6570\u636e\u3002\u540c\u65f6\uff0c\u660e\u786e\u6570\u636e\u7684\u4f7f\u7528\u76ee\u7684\uff0c\u907f\u514d\u4fb5\u72af\u9690\u79c1\u6216\u77e5\u8bc6\u4ea7\u6743\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f18\u5316\u722c\u866b\u6027\u80fd<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u6293\u53d6\u5927\u91cf\u6570\u636e\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u7f16\u7a0b\u6765\u63d0\u9ad8\u722c\u866b\u7684\u6548\u7387\u3002Python\u7684Threading\u5e93\u548cAsyncio\u5e93\u90fd\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u5e76\u53d1\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(response.status_code)<\/p>\n<p>thread = threading.Thread(target=fetch_url, args=(&#39;http:\/\/example.com&#39;,))<\/p>\n<p>thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u663e\u8457\u63d0\u9ad8\u722c\u866b\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u9ad8\u89e3\u6790\u6548\u7387<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u9ad8\u6548\u7684\u89e3\u6790\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u722c\u866b\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u5bf9\u4e8e\u5927\u578bHTML\u6587\u6863\uff0c\u4f7f\u7528lxml\u5e93\u8fdb\u884c\u89e3\u6790\u53ef\u80fd\u6bd4BeautifulSoup\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<p>tree = etree.HTML(response.text)<\/p>\n<p>links = tree.xpath(&#39;\/\/a\/@href&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>lxml\u5e93\u7684XPath\u529f\u80fd\u53ef\u4ee5\u5feb\u901f\u63d0\u53d6\u6570\u636e\uff0c\u6bd4\u4f20\u7edf\u7684\u67e5\u627e\u65b9\u6cd5\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5904\u7406\u53cd\u722c\u866b\u6280\u672f<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u4e9b\u7f51\u7ad9\u4f1a\u68c0\u6d4b\u8bf7\u6c42\u5934\u4e2d\u7684User-Agent\u5b57\u6bb5\uff0c\u4ee5\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u771f\u5b9e\u7528\u6237\u3002\u901a\u8fc7\u8bbe\u7f6e\u5408\u9002\u7684User-Agent\uff0c\u53ef\u4ee5\u6709\u6548\u7ed5\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u53cd\u722c\u866b\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">headers = {&#39;User-Agent&#39;: &#39;Mozilla\/5.0&#39;}<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbe\u7f6e\u5408\u9002\u7684\u8bf7\u6c42\u5934\u53ef\u4ee5\u5e2e\u52a9\u4f2a\u88c5\u6210\u6b63\u5e38\u7684\u6d4f\u89c8\u5668\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u4ee3\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u76ee\u6807\u7f51\u7ad9\u5bf9\u5355\u4e2aIP\u7684\u8bf7\u6c42\u9891\u7387\u6709\u9650\u5236\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406\u6765\u5206\u6563\u8bf7\u6c42\uff0c\u907f\u514d\u88ab\u5c01\u7981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">proxies = {&#39;http&#39;: &#39;http:\/\/10.10.1.10:3128&#39;}<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;, proxies=proxies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee3\u7406\u53ef\u4ee5\u6709\u6548\u5730\u7ed5\u8fc7IP\u9650\u5236\uff0c\u4f46\u9700\u6ce8\u610f\u9009\u62e9\u9ad8\u8d28\u91cf\u7684\u4ee3\u7406\u670d\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u9a8c\u8bc1\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u7ad9\u4f1a\u4f7f\u7528\u9a8c\u8bc1\u7801\u4f5c\u4e3a\u53cd\u722c\u866b\u624b\u6bb5\uff0c\u9700\u8981\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u6216\u4eba\u5de5\u5e72\u9884\u6765\u89e3\u51b3\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528Tesseract OCR\u7b49\u5f00\u6e90\u5de5\u5177\u8fdb\u884c\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytesseract import image_to_string<\/p>\n<p>from PIL import Image<\/p>\n<p>image = Image.open(&#39;captcha.png&#39;)<\/p>\n<p>text = image_to_string(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u9a8c\u8bc1\u7801\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6280\u672f\u8fdb\u884c\u8bc6\u522b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bf7\u6c42\u5931\u8d25<\/strong><\/p>\n<\/p>\n<p><p>\u8bf7\u6c42\u5931\u8d25\u901a\u5e38\u662f\u7531\u4e8e\u7f51\u7edc\u95ee\u9898\u3001\u7f51\u7ad9\u62d2\u7edd\u8bbf\u95ee\u6216\u8bf7\u6c42\u5934\u8bbe\u7f6e\u4e0d\u5f53\u9020\u6210\u7684\u3002\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u3001\u8c03\u6574\u8bf7\u6c42\u5934\u6216\u589e\u52a0\u91cd\u8bd5\u673a\u5236\u53ef\u4ee5\u89e3\u51b3\u5927\u591a\u6570\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u683c\u5f0f\u4e0d\u4e00\u81f4<\/strong><\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7f51\u7ad9\u7684\u6570\u636e\u683c\u5f0f\u53ef\u80fd\u4e0d\u4e00\u81f4\uff0c\u8fd9\u9700\u8981\u5728\u6570\u636e\u89e3\u6790\u548c\u5904\u7406\u9636\u6bb5\u8fdb\u884c\u9002\u5f53\u7684\u8c03\u6574\u548c\u6e05\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u722c\u866b\u88ab\u5c01\u7981<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u722c\u866b\u88ab\u76ee\u6807\u7f51\u7ad9\u5c01\u7981\uff0c\u9700\u8981\u68c0\u67e5\u5e76\u8c03\u6574\u8bf7\u6c42\u7b56\u7565\uff0c\u4f7f\u7528\u4ee3\u7406\u3001\u964d\u4f4e\u8bf7\u6c42\u9891\u7387\u6216\u66f4\u6539\u8bf7\u6c42\u5934\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u6293\u53d6Python\u7f51\u7edc\u722c\u866b\uff0c\u63d0\u53d6\u5e76\u5904\u7406\u6240\u9700\u7684\u6570\u636e\u3002\u540c\u65f6\uff0c\u59cb\u7ec8\u6ce8\u610f\u9075\u5b88\u7f51\u7edc\u9053\u5fb7\u548c\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u4ee5\u786e\u4fdd\u722c\u866b\u884c\u4e3a\u5408\u6cd5\u5408\u89c4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u7f51\u7edc\u722c\u866b\uff0c\u5982\u4f55\u5f00\u59cb\u4f7f\u7528\u5b83\uff1f<\/strong><br \/>Python\u7f51\u7edc\u722c\u866b\u662f\u4e00\u79cd\u901a\u8fc7\u7f16\u7a0b\u624b\u6bb5\u81ea\u52a8\u8bbf\u95ee\u5e76\u63d0\u53d6\u7f51\u9875\u6570\u636e\u7684\u6280\u672f\u3002\u8981\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u7f51\u7edc\u722c\u866b\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u57fa\u672c\u5e93\uff0c\u5982<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5904\u7406HTTP\u8bf7\u6c42\u548c\u89e3\u6790HTML\u6587\u6863\u3002\u53ef\u4ee5\u4ece\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\u5165\u624b\uff0c\u9010\u6b65\u6df1\u5165\u5b66\u4e60\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u7f51\u7ad9\u3002<\/p>\n<p><strong>\u5728\u6293\u53d6\u7f51\u7ad9\u6570\u636e\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u7edc\u722c\u866b\u65f6\uff0c\u9075\u5faa\u7f51\u7ad9\u7684<code>robots.txt<\/code>\u6587\u4ef6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8be5\u6587\u4ef6\u6307\u660e\u4e86\u54ea\u4e9b\u90e8\u5206\u53ef\u4ee5\u88ab\u722c\u53d6\uff0c\u54ea\u4e9b\u90e8\u5206\u88ab\u7981\u6b62\u3002\u6b64\u5916\uff0c\u907f\u514d\u5bf9\u76ee\u6807\u7f51\u7ad9\u9020\u6210\u8fc7\u5927\u8d1f\u62c5\u662f\u5173\u952e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u6765\u51cf\u8f7b\u670d\u52a1\u5668\u538b\u529b\u3002\u786e\u4fdd\u60a8\u6240\u6293\u53d6\u7684\u6570\u636e\u4e0d\u4f1a\u4fb5\u72af\u7248\u6743\u6216\u9690\u79c1\uff0c\u4ee5\u514d\u5f15\u53d1\u6cd5\u5f8b\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8Python\u7f51\u7edc\u722c\u866b\u7684\u6548\u7387\u548c\u7a33\u5b9a\u6027\uff1f<\/strong><br \/>\u63d0\u9ad8\u7f51\u7edc\u722c\u866b\u7684\u6548\u7387\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u8bf7\u6c42\u6765\u5b9e\u73b0\uff0c\u8fd9\u6837\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u663e\u8457\u7f29\u77ed\u6293\u53d6\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u4ee3\u7406\u6c60\u53ef\u4ee5\u5e2e\u52a9\u60a8\u907f\u514d\u88ab\u76ee\u6807\u7f51\u7ad9\u5c4f\u853d\u3002\u786e\u4fdd\u5904\u7406\u597d\u5f02\u5e38\u548c\u9519\u8bef\uff0c\u4f8b\u5982\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u6216\u6570\u636e\u89e3\u6790\u9519\u8bef\uff0c\u80fd\u591f\u589e\u5f3a\u722c\u866b\u7684\u7a33\u5b9a\u6027\uff0c\u786e\u4fdd\u5176\u5728\u957f\u65f6\u95f4\u8fd0\u884c\u65f6\u4e0d\u4f1a\u4e2d\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6293\u53d6Python\u7f51\u7edc\u722c\u866b\u7684\u5173\u952e\u5728\u4e8e\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u5904\u7406\u6570\u636e\u3001\u9075\u5b88\u7f51\u7edc\u9053\u5fb7\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u7f51\u7edc\u722c\u866b [&hellip;]","protected":false},"author":3,"featured_media":969676,"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\/969663"}],"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=969663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969663\/revisions"}],"predecessor-version":[{"id":969680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969663\/revisions\/969680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/969676"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=969663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=969663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=969663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}