﻿{"id":1057,"date":"2021-04-16T05:25:38","date_gmt":"2021-04-15T21:25:38","guid":{"rendered":"https:\/\/byy3.com\/?p=1057"},"modified":"2021-04-16T06:01:36","modified_gmt":"2021-04-15T22:01:36","slug":"python%e4%bd%bf%e7%94%a8ip%e4%bb%a3%e7%90%86%e6%8a%93%e5%8f%96%e7%bd%91%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/byy3.com\/?p=1057","title":{"rendered":"python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875"},"content":{"rendered":"<p>python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875<br \/>\npython\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875\uff0c\u5728\u6293\u53d6\u4e00\u4e2a\u7f51\u7ad9\u7684\u4fe1\u606f\u65f6\uff0c\u5982\u679c\u6211\u4eec\u8fdb\u884c\u9891\u7e41\u7684\u8bbf\u95ee\uff0c\u5c31\u5f88\u6709\u53ef\u80fd\u88ab\u7f51\u7ad9\u68c0\u6d4b\u5230\u800c\u88ab\u5c4f\u853d\uff0c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528ip\u4ee3\u7406 \u3002\u5728\u6211\u4eec\u63a5\u5165\u56e0\u7279\u7f51\u8fdb\u884c\u4e0a\u7f51\u65f6\uff0c\u6211\u4eec\u7684\u7535\u8111\u90fd\u4f1a\u88ab\u5206\u914d\u4e00\u4e2a\u5168\u7403\u552f\u4e00\u5730ip\u5730\u5740\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u800c\u5f53\u6211\u4eec\u9891\u7e41\u8bbf\u95ee\u4e00\u4e2a\u7f51\u7ad9\u65f6\uff0c\u7f51\u7ad9\u4e5f\u6b63\u662f\u56e0\u4e3a\u53d1\u73b0\u540c\u4e00\u4e2aip\u5730\u5740\u8bbf\u95ee\u591a\u6b21\u800c\u8fdb\u884c\u5c4f\u853d\u7684\uff0c\u6240\u4ee5\u8fd9\u65f6\u5019\u5982\u679c\u6211\u4eec\u4f7f\u7528\u591a\u4e2aip\u5730\u5740\u8fdb\u884c\u968f\u673a\u5730\u8f6e\u6d41\u8bbf\u95ee\uff0c\u8fd9\u6837\u88ab\u7f51\u7ad9\u68c0\u6d4b\u7684\u6982\u7387\u5c31\u5f88\u5c0f\u4e86\uff0c\u8fd9\u65f6\u5019\u5982\u679c\u6211\u4eec\u518d\u4f7f\u7528\u591a\u4e2a\u4e0d\u540c\u7684headers\uff0c\u8fd9\u65f6\u5019\u5c31\u6709\u591a\u4e2aip+\u4e3b\u673a\u7684\u7ec4\u5408\uff0c\u8bbf\u95ee\u65f6\u88ab\u53d1\u73b0\u7684\u6982\u7387\u53c8\u8fdb\u4e00\u6b65\u51cf\u5c0f\u4e86\u3002<\/p>\n<p>python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875<\/p>\n<pre><code><\/code><\/pre>\n<p><a href=\"https:\/\/byy3.com\/go\/?url=https:\/\/www.wanbianip.com\/\" target=\"_self\" rel=\"noopener\" rel=\"nofollow\" ><img loading=\"lazy\" decoding=\"async\" data-original=\"https:\/\/www.wanbianip.com\/ueditor\/php\/upload\/image\/20200106\/1578298197187690.png\" src=\"https:\/\/byy3.com\/wp-content\/themes\/MNews%20V2.4\/images\/post-loading.gif\" width=\"425\" height=\"242\" border=\"0\" vspace=\"0\" title=\"python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875\u63d2\u56fe\" alt=\"python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875\u63d2\u56fe\" \/><\/a><\/p>\n<pre><code><\/code><\/pre>\n<p>\u5173\u4e8e\u4ee3\u7801\u4e2dip\u4ee3\u7406\u7684\u4f7f\u7528\uff0c\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e0b\uff1a<\/p>\n<pre><code>\r\n\r\n\u6b65\u9aa4\uff1a\r\n\r\n1\u3001urllib2\u5e93\u4e2d\u7684ProxyHandler\u7c7b\uff0c\u901a\u8fc7\u6b64\u7c7b\u53ef\u4ee5\u4f7f\u7528ip\u4ee3\u7406\u8bbf\u95ee\u7f51\u9875\r\n\r\nproxy_support=urllib2.ProxyHandler({})\uff0c\u5176\u4e2d\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u5178{\u2018\u7c7b\u578b':'\u4ee3\u7406ip:\u7aef\u53e3\u53f7'}\r\n\r\n2\u3001\u5b9a\u5236\u3001\u521b\u5efa\u4e00\u4e2aopener\r\n\r\nopener=urllib2.build_opener(proxy_support)\r\n\r\n3\u3001\uff081\uff09\u5b89\u88c5opener\r\n\r\nurlib2.install_opener(opener)\r\n\r\n\uff082\uff09\u8c03\u7528\u9ed8\u8ba4\u7684opener\r\n\r\nopener.open(url)\r\n\r\n\u5bf9\u4e8e\u6ca1\u6709\u8bbe\u7f6e\u53cd\u722c\u866b\u673a\u5236\u7684\u7f51\u7ad9\uff0c\u6211\u4eec\u53ea\u9700\u8981\u76f4\u63a5\u50cf\u4e0a\u9762\u5f15\u5165ProxyHandler\u7c7b\u8fdb\u884c\u5904\u7406\uff0c\u4e0b\u9762\u4ee5\u8bbf\u95eecsdn\u4e3b\u9875\u4e3a\u4f8b\uff1a\r\n\r\n[python] view plain copy\r\n\r\nimport urllib\r\n\r\nurl=\"http:\/\/www.csdn.net\/\"\r\n\r\nfor i in range(0,10000):\r\n\r\nhtml=urllib.urlopen(url)\r\n\r\nprint html.info()\r\n\r\nprint i\r\n\r\n\u5f53\u4f7f\u7528\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u5f53\u5faa\u73af\u523020\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e0b\u9762\u7684\u9519\u8bef\r\n\r\nTraceback (most recent call last):\r\n\r\nFile \"C:\/Users\/lenovo\/PycharmProjects\/untitled1\/jt2\/__init__.py\", line 19, in \r\n\r\nhtml=urllib.urlopen(url)\r\n\r\nFile \"C:\\Python27\\lib\\urllib.py\", line 87, in urlopen\r\n\r\nreturn opener.open(url)\r\n\r\nFile \"C:\\Python27\\lib\\urllib.py\", line 213, in open\r\n\r\nreturn getattr(self, name)(url)\r\n\r\nFile \"C:\\Python27\\lib\\urllib.py\", line 350, in open_http\r\n\r\nh.endheaders(data)\r\n\r\nFile \"C:\\Python27\\lib\\httplib.py\", line 997, in endheaders\r\n\r\nself._send_output(message_body)\r\n\r\nFile \"C:\\Python27\\lib\\httplib.py\", line 850, in _send_output\r\n\r\nself.send(msg)\r\n\r\nFile \"C:\\Python27\\lib\\httplib.py\", line 812, in send\r\n\r\nself.connect()\r\n\r\nFile \"C:\\Python27\\lib\\httplib.py\", line 793, in connect\r\n\r\nself.timeout, self.source_address)\r\n\r\nFile \"C:\\Python27\\lib\\socket.py\", line 571, in create_connection\r\n\r\nraise err\r\n\r\nIOError: [Errno socket error] [Errno 10060]\r\n\r\n\u8fd9\u5c31\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u8ba1\u7b97\u673a\u7684\u5355\u4e00ip\u8fdb\u884c\u9891\u7e41\u8bbf\u95ee\u800c\u88ab\u68c0\u6d4b\u51fa\u6765\u7684\u3002\r\n\r\n\u4e0b\u9762\u662f\u4f7f\u7528\u4e86ip\u4ee3\u7406\u7684\u4ee3\u7801\uff1a\r\n\r\n[python] view plain copy<\/code><\/pre>\n<div>\n<div>import\u00a0urllib.request<\/div>\n<div>import\u00a0random<\/div>\n<div>def\u00a0getHtml(url,proxies):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0random_proxy\u00a0=\u00a0random.choice(proxies)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0proxy_support\u00a0=\u00a0urllib.request.ProxyHandler({\"http\":random_proxy})<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0opener\u00a0=\u00a0urllib.request.build_opener(proxy_support)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0urllib.request.install_opener(opener)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0html\u00a0=\u00a0urllib.request.urlopen(url)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0return\u00a0html<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0url\u00a0=\u00a0\"https:\/\/byy3.com\/\"<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0proxies\u00a0=\u00a0[\"178.32.129.31:3128\",\"165.225.77.44:80\",\"105.112.8.53:3128\",\"165.225.77.42:80\",\"144.217.254.175:3128\",\"134.209.69.46:8080\"]<\/div>\n<div>for\u00a0i\u00a0in\u00a0range(0,10000):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0try:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0html=getHtml(url,proxies)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print\u00a0(html.info())\u00a0\u00a0\u00a0\u00a0#\u6253\u5370\u7f51\u9875\u7684\u5934\u90e8\u4fe1\u606f\uff0c\u53ea\u662f\u4e3a\u4e86\u5c55\u793a\u8bbf\u95ee\u5230\u4e86\u7f51\u9875\uff0c\u53ef\u4ee5\u81ea\u5df1\u4fee\u6539\u6210\u60f3\u663e\u793a\u7684\u5185\u5bb9<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print\u00a0(i)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0except:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print\u00a0(\"\u51fa\u73b0\u6545\u969c\")<\/div>\n<\/div>\n<pre><code>\r\n\r\n\u8fd9\u4e2a\u4ee3\u7801\u6211\u6d4b\u8bd5\u662f\u57281096\u6b21\u65f6\u88ab\u68c0\u6d4b\u5230\u4e86\uff0c\u8981\u77e5\u9053\u6211\u7684\u5217\u8868\u4e2d\u53ea\u67096\u4e2aip\uff0c\u5982\u679c\u6211\u4eec\u589e\u52a0ip\u7684\u4e2a\u6570\uff0c\u90a3\u4e48\u88ab\u53d1\u73b0\u7684\u6982\u7387\u662f\u4e0d\u662f\u53c8\u4f1a\u66f4\u4f4e\u4e86\u3002\u5bf9\u4e8e\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7684ip\u4ee3\u7406\uff0c\u6709\u53ef\u80fd\u5728\u8fc7\u4e86\u4e00\u6bb5\u65f6\u95f4\u540e\u4fbf\u4e0d\u80fd\u7528\u4e86\uff0c\u8fd9\u4e2a\u9700\u8981\u81ea\u5df1\u5230\u7f51\u4e0a\u641c\u7d22\u6700\u65b0\u7684ip\u4ee3\u7406\uff0c\u8fdb\u884c\u66ff\u6362\u3002\u8fd8\u6709\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u662f\u4e3a\u4e86\u4f7f\u7a0b\u5e8f\u80fd\u591f\u5904\u7406ip\u4ee3\u7801\u8bbf\u95ee\u65f6\u51fa\u73b0\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u6709\u4e9bip\u4ee3\u7406\u5728\u8bbf\u95ee\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u6545\u969c\u7684\uff0c\u8fd9\u6837\u505a\u4e86\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u3002\r\n\r\n\u5bf9\u4e8e\u6709\u53cd\u722c\u866b\u673a\u5236\u7684\u7f51\u9875\uff0c\u4e0b\u9762\u8fd8\u662f\u4ee5\u8bbf\u95eecsdn\u4e2d\u7684\u535a\u5ba2\u4e3a\u4f8b\uff1a\r\n\r\n[python] view plain copy\r\n\r\n#coding:utf-8\r\n\r\nimport urllib2\r\n\r\nimport random\r\n\r\ndef get_html(url,headers,proxies):\r\n\r\nrandom_userAget = random.choice(headers)\r\n\r\nrandom_proxy = random.choice(proxies)\r\n\r\n#\u4e0b\u9762\u662f\u6a21\u62df\u6d4f\u89c8\u5668\u8fdb\u884c\u8bbf\u95ee\r\n\r\nreq = urllib2.Request(url)\r\n\r\nreq.add_header(\"User-Agent\", random_userAget)\r\n\r\nreq.add_header(\"GET\", url)\r\n\r\nreq.add_header(\"Host\", \"blog.csdn.net\")\r\n\r\nreq.add_header(\"Referer\", \"http:\/\/blog.csdn.net\/?&amp;page=6\")\r\n\r\n#\u4e0b\u9762\u662f\u4f7f\u7528ip\u4ee3\u7406\u8fdb\u884c\u8bbf\u95ee\r\n\r\nproxy_support = urllib2.ProxyHandler({\"http\":random_proxy})\r\n\r\nopener = urllib2.build_opener(proxy_support)\r\n\r\nurllib2.install_opener(opener)\r\n\r\nhtml = urllib2.urlopen(req)\r\n\r\nreturn html\r\n\r\nurl = \"http:\/\/blog.csdn.net\/?&amp;page=3\"\r\n\r\n\"\"\"\r\n\r\n\u4f7f\u7528\u591a\u4e2a\u4e3b\u673a\u4e2d\u7684user_agent\u4fe1\u606f\u7ec4\u6210\u4e00\u4e2a\u5217\u8868\uff0c\u5f53\u7136\u8fd9\u91cc\u9762\u7684user_agent\u90fd\u662f\u6b8b\u7f3a\u7684\uff0c\u5927\u5bb6\u4f7f\u7528\u65f6\u53ef\u4ee5\u81ea\u5df1\u627e\r\n\r\n\u8eab\u8fb9\u7684\u5c0f\u4f19\u4f34\u501f\u5466\r\n\r\n\"\"\"\r\n\r\nuser_agents = [\r\n\r\n\"Mozilla\/5.0 (Windows NT 6.3; WOW64) AppleWe\u3002\u3002\u3002hrome\/45.0.2454.101 Safari\/537.36\",\r\n\r\n\"Mozilla \/ 5.0(Windows NT 6.1) AppleWebKit \/ 537.\u3002\u3002\u3002\u3002likeGecko) Chrome \/ 45.0.2454.101Safari\/ 537.36\",\r\n\r\n\"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\u3002\u3002\u3002\u3002\u3002Gecko) Chrome\/50.0.2661.102 Safari\/537.36\",\r\n\r\n\"Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.3\u3002\u3002\u3002\u3002ML, like Gecko) Chrome\/49.0.2623.112 Safari\/537.36\",\r\n\r\n\"User-Agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) \u3002\u3002\u3002WebKit\/537.36 (KHTML, like Gecko) Chrome\/46.0.2486.0 Safari\/537.36 Edge\/13.10586\",\r\n\r\n\"User-Agent: Mozilla\/5.0 (Windows NT 10.0) AppleWebKi\u3002\u3002\u3002\u3002\u300236 (KHTML, like Gecko) Chrome\/46.0.2486.0 Safari\/537.36 Edge\/13.10586\",\r\n\r\n\"Mozilla\/5.0 (Windows NT 10.0; WOW64) Apple\u3002\u3002\u3002\u3002\u3002KHTML, like Gecko) Chrome\/50.0.2661.94 Safari\/537.36\"\r\n\r\n]\r\n\r\n#\u7f51\u4e0a\u7684ip\u6709\u53ef\u80fd\u662f\u4e0d\u80fd\u7528\u7684\uff0c\u9700\u8981\u591a\u505a\u5c1d\u8bd5\r\n\r\nmyproxies=[\"220.189.249.80:80\",\"124.248.32.43:80\"]\r\n\r\nhtml = get_html(url,user_agents,myproxies)\r\n\r\nprint html.read()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875 python\u4f7f\u7528ip\u4ee3\u7406\u6293\u53d6\u7f51\u9875\uff0c\u5728\u6293\u53d6\u4e00\u4e2a\u7f51\u7ad9\u7684\u4fe1\u606f\u65f6\uff0c\u5982\u679c\u6211\u4eec\u8fdb\u884c\u9891\u7e41\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[616,619,402,51,178],"class_list":["post-1057","post","type-post","status-publish","format-standard","hentry","category-python","tag-proxy","tag-python-proxy","tag-python-scrapy","tag-python"],"_links":{"self":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1057"}],"version-history":[{"count":0,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1057\/revisions"}],"wp:attachment":[{"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}