{"id":1153540,"date":"2025-01-13T17:40:05","date_gmt":"2025-01-13T09:40:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153540.html"},"modified":"2025-01-13T17:40:08","modified_gmt":"2025-01-13T09:40:08","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aeip%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153540.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6eip\u4ee3\u7406"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183254\/6f7c6ddf-6a00-432d-9184-beb085d13056.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6eip\u4ee3\u7406\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8bbe\u7f6eIP\u4ee3\u7406\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684HTTP\u5e93\u3001\u7b2c\u4e09\u65b9HTTP\u5e93\u5982Requests\u3001\u4ee5\u53ca\u66f4\u9ad8\u7ea7\u7684\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5e93\u5982Selenium\u7b49\u3002<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Requests\u5e93\u3001\u4f7f\u7528urllib\u5e93\u3001\u4f7f\u7528Selenium\u5e93<\/strong>\u3002\u5176\u4e2d\u4f7f\u7528Requests\u5e93\u662f\u6700\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Requests\u5e93<\/strong>\uff1aRequests\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u53d1\u9001HTTP\u8bf7\u6c42\u65f6\u8bbe\u7f6eIP\u4ee3\u7406\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528Requests\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;http:\/\/example.com&quot;, proxies=proxies)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Requests\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4ee3\u7406\u4fe1\u606f\u7684\u5b57\u5178<code>proxies<\/code>\uff0c\u5b57\u5178\u7684\u952e\u662f\u534f\u8bae\u7c7b\u578b\uff08http\u6216https\uff09\uff0c\u503c\u662f\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5c06\u8be5\u4ee3\u7406\u5b57\u5178\u4f20\u9012\u7ed9Requests\u5e93\u7684<code>get<\/code>\u65b9\u6cd5\u3002\u6700\u540e\uff0c\u6253\u5370\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Requests\u5e93<\/h3>\n<\/p>\n<p><p>Requests\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684HTTP\u5e93\u4e4b\u4e00\uff0c\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u901a\u8fc7\u4f7f\u7528Requests\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Requests\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Requests\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528Requests\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;http:\/\/example.com&quot;, proxies=proxies)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4ee3\u7406\u4fe1\u606f\u7684\u5b57\u5178<code>proxies<\/code>\uff0c\u5b57\u5178\u7684\u952e\u662f\u534f\u8bae\u7c7b\u578b\uff08http\u6216https\uff09\uff0c\u503c\u662f\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5c06\u8be5\u4ee3\u7406\u5b57\u5178\u4f20\u9012\u7ed9Requests\u5e93\u7684<code>get<\/code>\u65b9\u6cd5\u3002\u6700\u540e\uff0c\u6253\u5370\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u6709\u4e9b\u4ee3\u7406\u670d\u52a1\u5668\u9700\u8981\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ee3\u7406URL\u4e2d\u5305\u542b\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8bbe\u7f6e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;http:\/\/example.com&quot;, proxies=proxies)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u4ee3\u7406URL\u4e2d\u6dfb\u52a0\u4e86\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u8fd9\u6837\uff0cRequests\u5e93\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4f1a\u4f7f\u7528\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528urllib\u5e93<\/h3>\n<\/p>\n<p><p>urllib\u5e93\u662fPython\u5185\u7f6e\u7684HTTP\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5904\u7406URL\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u7b49\u529f\u80fd\u3002\u867d\u7136urllib\u5e93\u7684\u4f7f\u7528\u76f8\u5bf9\u590d\u6742\u4e00\u4e9b\uff0c\u4f46\u5b83\u540c\u6837\u652f\u6301\u8bbe\u7f6eIP\u4ee3\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528urllib\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxy = urllib.request.ProxyHandler({<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>})<\/p>\n<p>opener = urllib.request.build_opener(proxy)<\/p>\n<p>urllib.request.install_opener(opener)<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = urllib.request.urlopen(&quot;http:\/\/example.com&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>html = response.read().decode(&quot;utf-8&quot;)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a<code>ProxyHandler<\/code>\u5bf9\u8c61\u5e76\u4f20\u9012\u4ee3\u7406\u4fe1\u606f\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>build_opener<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a<code>OpenerDirector<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7<code>install_opener<\/code>\u65b9\u6cd5\u5c06\u5176\u5b89\u88c5\u4e3a\u5168\u5c40\u9ed8\u8ba4\u7684<code>Opener<\/code>\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>urlopen<\/code>\u65b9\u6cd5\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u8bfb\u53d6\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8eRequests\u5e93\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u4ee3\u7406URL\u4e2d\u5305\u542b\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<h2><strong>\u8bbe\u7f6e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406<\/strong><\/h2>\n<p>proxy = urllib.request.ProxyHandler({<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>})<\/p>\n<p>opener = urllib.request.build_opener(proxy)<\/p>\n<p>urllib.request.install_opener(opener)<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = urllib.request.urlopen(&quot;http:\/\/example.com&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>html = response.read().decode(&quot;utf-8&quot;)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u4ee3\u7406URL\u4e2d\u6dfb\u52a0\u4e86\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u8fd9\u6837\uff0curllib\u5e93\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4f1a\u4f7f\u7528\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Selenium\u5e93<\/h3>\n<\/p>\n<p><p>Selenium\u5e93\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fdb\u884c\u7f51\u9875\u6d4b\u8bd5\u3001\u722c\u866b\u7b49\u4efb\u52a1\u3002\u901a\u8fc7Selenium\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u8fdb\u884c\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Selenium\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Selenium\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Selenium\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u4e0b\u8f7d\u5bf9\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528Chrome\u6d4f\u89c8\u5668\uff0c\u9700\u8981\u4e0b\u8f7dChromeDriver\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528Selenium\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff08\u4ee5Chrome\u6d4f\u89c8\u5668\u4e3a\u4f8b\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.chrome.options import Options<\/p>\n<h2><strong>\u8bbe\u7f6eChrome\u9009\u9879<\/strong><\/h2>\n<p>chrome_options = Options()<\/p>\n<p>chrome_options.add_argument(&quot;--proxy-server=http:\/\/your_proxy_ip:your_proxy_port&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>driver = webdriver.Chrome(options=chrome_options)<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&quot;http:\/\/example.com&quot;)<\/p>\n<h2><strong>\u6253\u5370\u7f51\u9875\u6807\u9898<\/strong><\/h2>\n<p>print(driver.title)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a<code>Options<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7<code>add_argument<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u5e26\u6709\u9009\u9879\u7684<code>webdriver.Chrome<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6d4f\u89c8\u5668\u5bf9\u8c61\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>get<\/code>\u65b9\u6cd5\u6253\u5f00\u7f51\u9875\uff0c\u5e76\u6253\u5370\u7f51\u9875\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bbe\u7f6eIP\u4ee3\u7406\uff08\u4ee5Chrome\u6d4f\u89c8\u5668\u4e3a\u4f8b\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.chrome.options import Options<\/p>\n<h2><strong>\u8bbe\u7f6eChrome\u9009\u9879<\/strong><\/h2>\n<p>chrome_options = Options()<\/p>\n<p>chrome_options.add_argument(&quot;--proxy-server=http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>driver = webdriver.Chrome(options=chrome_options)<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&quot;http:\/\/example.com&quot;)<\/p>\n<h2><strong>\u6253\u5370\u7f51\u9875\u6807\u9898<\/strong><\/h2>\n<p>print(driver.title)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u4ee3\u7406URL\u4e2d\u6dfb\u52a0\u4e86\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u8fd9\u6837\uff0cSelenium\u5e93\u5728\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u64cd\u4f5c\u65f6\u4f1a\u4f7f\u7528\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Scrapy\u5e93<\/h3>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684Python\u722c\u866b\u6846\u67b6\uff0c\u4e13\u4e3a\u5927\u89c4\u6a21\u722c\u53d6\u7f51\u9875\u6570\u636e\u800c\u8bbe\u8ba1\u3002Scrapy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u722c\u53d6\u7ba1\u7406\u3001\u6570\u636e\u5904\u7406\u3001\u8bf7\u6c42\u8c03\u5ea6\u7b49\u3002\u901a\u8fc7Scrapy\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u8fdb\u884c\u722c\u53d6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Scrapy\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Scrapy\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Scrapy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scrapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaScrapy\u9879\u76ee<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aScrapy\u9879\u76ee\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efaScrapy\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myproject<\/code>\u7684Scrapy\u9879\u76ee\u76ee\u5f55\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u5728Scrapy\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u8bbe\u7f6eIP\u4ee3\u7406\u3002\u5728Scrapy\u9879\u76ee\u7684<code>settings.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u4ee3\u7406\u4e2d\u95f4\u4ef6<\/p>\n<p>DOWNLOADER_MIDDLEWARES = {<\/p>\n<p>    &#39;scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware&#39;: 1,<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>PROXY = &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u4ee3\u7406\u4e2d\u95f4\u4ef6<\/strong><\/h2>\n<p>class ProxyMiddleware(object):<\/p>\n<p>    def process_request(self, request, spider):<\/p>\n<p>        request.meta[&#39;proxy&#39;] = PROXY<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u5728<code>middlewares.py<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u4ee3\u7406\u4e2d\u95f4\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from myproject.settings import PROXY<\/p>\n<p>class ProxyMiddleware(object):<\/p>\n<p>    def process_request(self, request, spider):<\/p>\n<p>        request.meta[&#39;proxy&#39;] = PROXY<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728<code>settings.py<\/code>\u6587\u4ef6\u4e2d\u914d\u7f6e\u4ee3\u7406\u4e2d\u95f4\u4ef6\u548c\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u63a5\u7740\uff0c\u5728<code>middlewares.py<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u81ea\u5b9a\u4e49\u4ee3\u7406\u4e2d\u95f4\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u8bbe\u7f6e\u8bf7\u6c42\u7684\u4ee3\u7406\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u8fd0\u884cScrapy\u722c\u866b<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u4e0a\u8ff0\u914d\u7f6e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884cScrapy\u722c\u866b\u6765\u9a8c\u8bc1\u4ee3\u7406\u8bbe\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fd0\u884cScrapy\u722c\u866b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy crawl myspider<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>myspider<\/code>\u662f\u722c\u866b\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ohttp\u5e93<\/h3>\n<\/p>\n<p><p>aiohttp\u662f\u4e00\u4e2a\u5f02\u6b65HTTP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u5e93\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u9ad8\u6548\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528aiohttp\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u53d1\u9001\u5f02\u6b65HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5aiohttp\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528aiohttp\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5aiohttp\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install aiohttp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528aiohttp\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch(url, proxy):<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        async with session.get(url, proxy=proxy) as response:<\/p>\n<p>            return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    url = &quot;http:\/\/example.com&quot;<\/p>\n<p>    proxy = &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;<\/p>\n<p>    html = await fetch(url, proxy)<\/p>\n<p>    print(html)<\/p>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570<code>fetch<\/code>\uff0c\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u5728<code>main<\/code>\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86URL\u548c\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\uff0c\u5e76\u8c03\u7528<code>fetch<\/code>\u51fd\u6570\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002\u6700\u540e\uff0c\u4f7f\u7528\u4e8b\u4ef6\u5faa\u73af\u8fd0\u884c<code>main<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bbe\u7f6eIP\u4ee3\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch(url, proxy, auth):<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        async with session.get(url, proxy=proxy, proxy_auth=auth) as response:<\/p>\n<p>            return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    url = &quot;http:\/\/example.com&quot;<\/p>\n<p>    proxy = &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;<\/p>\n<p>    auth = aiohttp.BasicAuth(&quot;username&quot;, &quot;password&quot;)<\/p>\n<p>    html = await fetch(url, proxy, auth)<\/p>\n<p>    print(html)<\/p>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a<code>BasicAuth<\/code>\u5bf9\u8c61\uff0c\u7528\u4e8e\u8fdb\u884c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002\u63a5\u7740\uff0c\u5728<code>fetch<\/code>\u51fd\u6570\u4e2d\uff0c\u5c06\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u4f20\u9012\u7ed9<code>session.get<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528httpx\u5e93<\/h3>\n<\/p>\n<p><p>httpx\u662f\u4e00\u4e2a\u73b0\u4ee3\u7684\u3001\u5f02\u6b65\u7684HTTP\u5ba2\u6237\u7aef\u5e93\uff0c\u652f\u6301\u540c\u6b65\u548c\u5f02\u6b65\u8bf7\u6c42\u3002\u901a\u8fc7\u4f7f\u7528httpx\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5httpx\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528httpx\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5httpx\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install httpx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528httpx\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import httpx<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &quot;http:\/\/&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https:\/\/&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = httpx.get(&quot;http:\/\/example.com&quot;, proxies=proxies)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4ee3\u7406\u4fe1\u606f\u7684\u5b57\u5178<code>proxies<\/code>\uff0c\u5b57\u5178\u7684\u952e\u662f\u534f\u8bae\u7c7b\u578b\uff08http\u6216https\uff09\uff0c\u503c\u662f\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u63a5\u7740\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5c06\u8be5\u4ee3\u7406\u5b57\u5178\u4f20\u9012\u7ed9httpx\u5e93\u7684<code>get<\/code>\u65b9\u6cd5\u3002\u6700\u540e\uff0c\u6253\u5370\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bbe\u7f6eIP\u4ee3\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import httpx<\/p>\n<h2><strong>\u8bbe\u7f6e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &quot;http:\/\/&quot;: &quot;http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https:\/\/&quot;: &quot;http:\/\/username:password@your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = httpx.get(&quot;http:\/\/example.com&quot;, proxies=proxies)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u4ee3\u7406URL\u4e2d\u6dfb\u52a0\u4e86\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u8fd9\u6837\uff0chttpx\u5e93\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4f1a\u4f7f\u7528\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528PyCurl\u5e93<\/h3>\n<\/p>\n<p><p>PyCurl\u662flibcurl\u7684Python\u63a5\u53e3\uff0clibcurl\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684HTTP\u5e93\uff0c\u652f\u6301\u591a\u79cd\u534f\u8bae\u548c\u9ad8\u7ea7\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528PyCurl\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5PyCurl\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528PyCurl\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5PyCurl\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pycurl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528PyCurl\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pycurl<\/p>\n<p>from io import BytesIO<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxy = &quot;your_proxy_ip:your_proxy_port&quot;<\/p>\n<h2><strong>\u521b\u5efaCurl\u5bf9\u8c61<\/strong><\/h2>\n<p>c = pycurl.Curl()<\/p>\n<h2><strong>\u8bbe\u7f6eURL<\/strong><\/h2>\n<p>c.setopt(c.URL, &quot;http:\/\/example.com&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>c.setopt(c.PROXY, proxy)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aBytesIO\u5bf9\u8c61\u7528\u4e8e\u5b58\u50a8\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>buffer = BytesIO()<\/p>\n<h2><strong>\u8bbe\u7f6e\u54cd\u5e94\u5185\u5bb9\u7684\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>c.setopt(c.WRITEDATA, buffer)<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>c.perform()<\/p>\n<h2><strong>\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>response = buffer.getvalue().decode(&quot;utf-8&quot;)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response)<\/p>\n<h2><strong>\u5173\u95edCurl\u5bf9\u8c61<\/strong><\/h2>\n<p>c.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8bbe\u7f6e\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u63a5\u7740\uff0c\u521b\u5efa\u4e00\u4e2a<code>Curl<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7<code>setopt<\/code>\u65b9\u6cd5\u8bbe\u7f6eURL\u548c\u4ee3\u7406\u670d\u52a1\u5668\u3002\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a<code>BytesIO<\/code>\u5bf9\u8c61\u7528\u4e8e\u5b58\u50a8\u54cd\u5e94\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u54cd\u5e94\u5185\u5bb9\u7684\u5199\u5165\u5bf9\u8c61\u3002\u6700\u540e\uff0c\u53d1\u9001\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bbe\u7f6eIP\u4ee3\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pycurl<\/p>\n<p>from io import BytesIO<\/p>\n<h2><strong>\u8bbe\u7f6e\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4ee3\u7406<\/strong><\/h2>\n<p>proxy = &quot;your_proxy_ip:your_proxy_port&quot;<\/p>\n<p>proxy_userpwd = &quot;username:password&quot;<\/p>\n<h2><strong>\u521b\u5efaCurl\u5bf9\u8c61<\/strong><\/h2>\n<p>c = pycurl.Curl()<\/p>\n<h2><strong>\u8bbe\u7f6eURL<\/strong><\/h2>\n<p>c.setopt(c.URL, &quot;http:\/\/example.com&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>c.setopt(c.PROXY, proxy)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1<\/strong><\/h2>\n<p>c.setopt(c.PROXYUSERPWD, proxy_userpwd)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aBytesIO\u5bf9\u8c61\u7528\u4e8e\u5b58\u50a8\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>buffer = BytesIO()<\/p>\n<h2><strong>\u8bbe\u7f6e\u54cd\u5e94\u5185\u5bb9\u7684\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>c.setopt(c.WRITEDATA, buffer)<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>c.perform()<\/p>\n<h2><strong>\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>response = buffer.getvalue().decode(&quot;utf-8&quot;)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response)<\/p>\n<h2><strong>\u5173\u95edCurl\u5bf9\u8c61<\/strong><\/h2>\n<p>c.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>setopt<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4ee3\u7406\u670d\u52a1\u5668\u548c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u3002\u8fd9\u6837\uff0cPyCurl\u5e93\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4f1a\u4f7f\u7528\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u4ee3\u7406\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528requests-html\u5e93<\/h3>\n<\/p>\n<p><p>requests-html\u662f\u4e00\u4e2a\u57fa\u4e8eRequests\u5e93\u548cPyQuery\u5e93\u7684HTML\u89e3\u6790\u5e93\uff0c\u9002\u7528\u4e8e\u8fdb\u884c\u7f51\u9875\u6293\u53d6\u548c\u89e3\u6790\u3002\u901a\u8fc7\u4f7f\u7528requests-html\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6eIP\u4ee3\u7406\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5requests-html\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528requests-html\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5requests-html\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests-html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6eIP\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528requests-html\u5e93\u8bbe\u7f6eIP\u4ee3\u7406\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from requests_html import HTMLSession<\/p>\n<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/your_proxy_ip:your_proxy_port&quot;,<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaHTMLSession\u5bf9\u8c61<\/strong><\/h2>\n<p>session = HTMLSession()<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = session.get(&quot;http:\/\/example.com&quot;, proxies=proxies)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4ee3\u7406\u4fe1\u606f\u7684\u5b57\u5178<code>proxies<\/code>\uff0c\u5b57\u5178\u7684\u952e\u662f\u534f\u8bae\u7c7b\u578b\uff08http<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6eHTTP\u4ee3\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bbe\u7f6eHTTP\u4ee3\u7406\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u8bf7\u6c42\u4e2d\u6dfb\u52a0<code>proxies<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u4ee3\u7406\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import requests\n\nproxies = {\n    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:port&quot;,\n    &quot;https&quot;: &quot;https:\/\/your_proxy_ip:port&quot;\n}\n\nresponse = requests.get(&quot;http:\/\/example.com&quot;, proxies=proxies)\nprint(response.text)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>your_proxy_ip<\/code>\u548c<code>port<\/code>\u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u4ee3\u7406\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bbe\u7f6eSOCKS\u4ee3\u7406\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528SOCKS\u4ee3\u7406\uff0c\u60a8\u9700\u8981\u5b89\u88c5<code>PySocks<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install PySocks<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u8bbe\u7f6e\u65b9\u6cd5\u4e0eHTTP\u4ee3\u7406\u7c7b\u4f3c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import requests\n\nproxies = {\n    &quot;http&quot;: &quot;socks5:\/\/your_proxy_ip:port&quot;,\n    &quot;https&quot;: &quot;socks5:\/\/your_proxy_ip:port&quot;\n}\n\nresponse = requests.get(&quot;http:\/\/example.com&quot;, proxies=proxies)\nprint(response.text)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u4f7f\u7528<code>&quot;socks5:\/\/&quot;<\/code>\u524d\u7f00\u6765\u6307\u5b9aSOCKS5\u4ee3\u7406\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9a8c\u8bc1\u4ee3\u7406\u662f\u5426\u5de5\u4f5c\u6b63\u5e38\uff1f<\/strong><br \/>\u4e3a\u4e86\u9a8c\u8bc1\u4ee3\u7406\u662f\u5426\u5de5\u4f5c\uff0c\u53ef\u4ee5\u8bbf\u95ee\u4e00\u4e2a\u663e\u793a\u60a8\u7684IP\u5730\u5740\u7684\u7f51\u7ad9\uff0c\u4f8b\u5982<code>http:\/\/httpbin.org\/ip<\/code>\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7406\u53d1\u9001\u8bf7\u6c42\u5e76\u68c0\u67e5\u8fd4\u56de\u7684IP\u5730\u5740\u662f\u5426\u4e0e\u60a8\u7684\u4ee3\u7406IP\u5339\u914d\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import requests\n\nproxies = {\n    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:port&quot;,\n    &quot;https&quot;: &quot;https:\/\/your_proxy_ip:port&quot;\n}\n\nresponse = requests.get(&quot;http:\/\/httpbin.org\/ip&quot;, proxies=proxies)\nprint(response.json())\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u60a8\u901a\u8fc7\u4ee3\u7406\u8bbf\u95ee\u7684IP\u5730\u5740\uff0c\u786e\u4fdd\u4ee3\u7406\u8bbe\u7f6e\u6b63\u786e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6eIP\u4ee3\u7406\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684HTTP\u5e93\u3001\u7b2c\u4e09\u65b9HTTP\u5e93\u5982Requests\u3001\u4ee5\u53ca\u66f4\u9ad8 [&hellip;]","protected":false},"author":3,"featured_media":1153547,"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\/1153540"}],"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=1153540"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153540\/revisions"}],"predecessor-version":[{"id":1153550,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153540\/revisions\/1153550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}