{"id":1091,"date":"2021-11-26T03:27:05","date_gmt":"2021-11-25T19:27:05","guid":{"rendered":"https:\/\/byy3.com\/?p=1091"},"modified":"2021-11-27T07:09:15","modified_gmt":"2021-11-26T23:09:15","slug":"%e8%87%aa%e5%b7%b1%e6%96%b0%e5%bb%ba%e4%bb%a3%e7%90%86%e6%b1%a0%e6%8b%bc%e9%87%87%e7%94%a8python%e8%b0%83%e7%94%a8%e8%ae%bf%e9%97%ae%e7%bd%91%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/byy3.com\/?p=1091","title":{"rendered":"\u81ea\u5df1\u65b0\u5efa\u4ee3\u7406\u6c60\u62fc\u91c7\u7528python\u8c03\u7528\u8bbf\u95ee\u7f51\u9875"},"content":{"rendered":"<p>\u9879\u76ee\u5730\u5740https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool<\/p>\n<p>\u91c7\u7528googleshell\u5b89\u88c5\u6ce8\u610f1\uff0c\u65e0\u6cd5\u4f7f\u7528\u5916\u7f51\u7aef\u53e3\u8bbf\u95ee<\/p>\n<p>\u5b89\u88c5pip3 install -r r<span class=\"pl-md\">equirements.txt \u5176\u4e2d \u7248\u672clxml\u6709\u51b2\u7a81\uff0c\u91c7\u7528pip3 uninstall lxml\u7136\u540e\u518d\u5b89\u88c5\u6307\u5b9a\u7248\u672cpip3 install lxml==4.6.4 \u6216\u80053.5<\/span><\/p>\n<p>\u5b89\u88c5\u597d\u7528\u9700\u8981\u5b89\u88c5apt-get install redis\u670d\u52a1\u5668 \u7136\u540e\u8fd0\u884c\u670d\u52a1\u5668<strong>redis-server<\/strong><\/p>\n<p>\u53ef\u4ee5\u770b\u5230 Redis\u3001Getter\u3001Server\u3001Tester \u90fd\u5df2\u7ecf\u542f\u52a8\u6210\u529f\u3002<\/p>\n<p>\u7136\u540e\u518d\u5f00\u4e00\u4e2aterminal\u518d\u8fd0\u884csudo python3 run.py \u6216\u8005\u8f6c\u6362\u8d85\u7ea7\u7528\u6237su\u7136\u540eroot\u7528\u6237\u4e0b\u5728python3 run.py<\/p>\n<p>\u6ce8\u610f\u4f7f\u7528screen -S proxy\u521b\u5efa\u540e\u53f0\u6267\u884c\u4efb\u52a1\uff01<\/p>\n<p>\u8fd9\u65f6\u5019\u8bbf\u95ee\u00a0<a href=\"https:\/\/byy3.com\/go\/?url=http:\/\/localhost:5555\/random\" rel=\"nofollow\">http:\/\/localhost:5555\/random<\/a>\u00a0\u5373\u53ef\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u53ef\u7528\u4ee3\u7406\u3002<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<h2 dir=\"auto\">\u4f7f\u7528<\/h2>\n<p>\u722c\u53d6\u6570\u636e\u5305\u5728\/AutoBuy\/ProxyPool\/proxypool\/crawlers\/public\/<\/p>\n<p>http:\/\/www.66ip.cn\/{page}.html<\/p>\n<p>&nbsp;<\/p>\n<p dir=\"auto\">\u6210\u529f\u8fd0\u884c\u4e4b\u540e\u53ef\u4ee5\u901a\u8fc7\u00a0<a href=\"https:\/\/byy3.com\/go\/?url=http:\/\/localhost:5555\/random\" rel=\"nofollow\">http:\/\/localhost:5555\/random<\/a>\u00a0\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u53ef\u7528\u4ee3\u7406\u3002<\/p>\n<p dir=\"auto\">\u53ef\u4ee5\u7528\u7a0b\u5e8f\u5bf9\u63a5\u5b9e\u73b0\uff0c\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u83b7\u53d6\u4ee3\u7406\u5e76\u722c\u53d6\u7f51\u9875\u7684\u8fc7\u7a0b\uff1a<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-k\">import<\/span> <span class=\"pl-s1\">requests<\/span>\r\n\r\n<span class=\"pl-s1\">proxypool_url<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s\">'http:\/\/127.0.0.1:5555\/random'<\/span>\r\n<span class=\"pl-s1\">target_url<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s\">'http:\/\/httpbin.org\/get'<\/span>\r\n\r\n<span class=\"pl-k\">def<\/span> <span class=\"pl-en\">get_random_proxy<\/span>():\r\n <span class=\"pl-s\">\"\"\"<\/span>\r\n<span class=\"pl-s\"> get random proxy from proxypool<\/span>\r\n<span class=\"pl-s\"> :return: proxy<\/span>\r\n<span class=\"pl-s\"> \"\"\"<\/span>\r\n <span class=\"pl-k\">return<\/span> <span class=\"pl-s1\">requests<\/span>.<span class=\"pl-en\">get<\/span>(<span class=\"pl-s1\">proxypool_url<\/span>).<span class=\"pl-s1\">text<\/span>.<span class=\"pl-en\">strip<\/span>()\r\n\r\n<span class=\"pl-k\">def<\/span> <span class=\"pl-en\">crawl<\/span>(<span class=\"pl-s1\">url<\/span>, <span class=\"pl-s1\">proxy<\/span>):\r\n <span class=\"pl-s\">\"\"\"<\/span>\r\n<span class=\"pl-s\"> use proxy to crawl page<\/span>\r\n<span class=\"pl-s\"> :param url: page url<\/span>\r\n<span class=\"pl-s\"> :param proxy: proxy, such as 8.8.8.8:8888<\/span>\r\n<span class=\"pl-s\"> :return: html<\/span>\r\n<span class=\"pl-s\"> \"\"\"<\/span>\r\n <span class=\"pl-s1\">proxies<\/span> <span class=\"pl-c1\">=<\/span> {<span class=\"pl-s\">'http'<\/span>: <span class=\"pl-s\">'http:\/\/'<\/span> <span class=\"pl-c1\">+<\/span> <span class=\"pl-s1\">proxy<\/span>}\r\n <span class=\"pl-k\">return<\/span> <span class=\"pl-s1\">requests<\/span>.<span class=\"pl-en\">get<\/span>(<span class=\"pl-s1\">url<\/span>, <span class=\"pl-s1\">proxies<\/span><span class=\"pl-c1\">=<\/span><span class=\"pl-s1\">proxies<\/span>).<span class=\"pl-s1\">text<\/span>\r\n\r\n\r\n<span class=\"pl-k\">def<\/span> <span class=\"pl-en\">main<\/span>():\r\n <span class=\"pl-s\">\"\"\"<\/span>\r\n<span class=\"pl-s\"> main method, entry point<\/span>\r\n<span class=\"pl-s\"> :return: none<\/span>\r\n<span class=\"pl-s\"> \"\"\"<\/span>\r\n <span class=\"pl-s1\">proxy<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-en\">get_random_proxy<\/span>()\r\n <span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">'get random proxy'<\/span>, <span class=\"pl-s1\">proxy<\/span>)\r\n <span class=\"pl-s1\">html<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-en\">crawl<\/span>(<span class=\"pl-s1\">target_url<\/span>, <span class=\"pl-s1\">proxy<\/span>)\r\n <span class=\"pl-en\">print<\/span>(<span class=\"pl-s1\">html<\/span>)\r\n\r\n<span class=\"pl-k\">if<\/span> <span class=\"pl-s1\">__name__<\/span> <span class=\"pl-c1\">==<\/span> <span class=\"pl-s\">'__main__'<\/span>:\r\n <span class=\"pl-en\">main<\/span>()<\/pre>\n<\/div>\n<p dir=\"auto\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>get random proxy 116.196.115.209:8080\r\n{\r\n \"args\": {}, \r\n \"headers\": {\r\n \"Accept\": \"*\/*\", \r\n \"Accept-Encoding\": \"gzip, deflate\", \r\n \"Host\": \"httpbin.org\", \r\n \"User-Agent\": \"python-requests\/2.22.0\", \r\n \"X-Amzn-Trace-Id\": \"Root=1-5e4d7140-662d9053c0a2e513c7278364\"\r\n }, \r\n \"origin\": \"116.196.115.209\", \r\n \"url\": \"https:\/\/httpbin.org\/get\"\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p dir=\"auto\">\u53ef\u4ee5\u770b\u5230\u6210\u529f\u83b7\u53d6\u4e86\u4ee3\u7406\uff0c\u5e76\u8bf7\u6c42 httpbin.org \u9a8c\u8bc1\u4e86\u4ee3\u7406\u7684\u53ef\u7528\u6027\u3002<\/p>\n<h2 dir=\"auto\"><a id=\"user-content-\u53ef\u914d\u7f6e\u9879\" class=\"anchor\" href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool#%E5%8F%AF%E9%85%8D%E7%BD%AE%E9%A1%B9\" aria-hidden=\"true\" rel=\"nofollow\" ><\/a>\u53ef\u914d\u7f6e\u9879<\/h2>\n<p dir=\"auto\">\u4ee3\u7406\u6c60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u914d\u7f6e\u4e00\u4e9b\u53c2\u6570\u3002<\/p>\n<h3 dir=\"auto\"><a id=\"user-content-\u5f00\u5173\" class=\"anchor\" href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool#%E5%BC%80%E5%85%B3\" aria-hidden=\"true\" rel=\"nofollow\" ><\/a>\u5f00\u5173<\/h3>\n<ul dir=\"auto\">\n<li>ENABLE_TESTER\uff1a\u5141\u8bb8 Tester \u542f\u52a8\uff0c\u9ed8\u8ba4 true<\/li>\n<li>ENABLE_GETTER\uff1a\u5141\u8bb8 Getter \u542f\u52a8\uff0c\u9ed8\u8ba4 true<\/li>\n<li>ENABLE_SERVER\uff1a\u8fd0\u884c Server \u542f\u52a8\uff0c\u9ed8\u8ba4 true<\/li>\n<\/ul>\n<h3 dir=\"auto\"><a id=\"user-content-\u73af\u5883\" class=\"anchor\" href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool#%E7%8E%AF%E5%A2%83\" aria-hidden=\"true\" rel=\"nofollow\" ><\/a>\u73af\u5883<\/h3>\n<ul dir=\"auto\">\n<li>APP_ENV\uff1a\u8fd0\u884c\u73af\u5883\uff0c\u53ef\u4ee5\u8bbe\u7f6e dev\u3001test\u3001prod\uff0c\u5373\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\u73af\u5883\uff0c\u9ed8\u8ba4 dev<\/li>\n<li>APP_DEBUG\uff1a\u8c03\u8bd5\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8bbe\u7f6e true \u6216 false\uff0c\u9ed8\u8ba4 true<\/li>\n<\/ul>\n<h3 dir=\"auto\"><a id=\"user-content-redis-\u8fde\u63a5\" class=\"anchor\" href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool#redis-%E8%BF%9E%E6%8E%A5\" aria-hidden=\"true\" rel=\"nofollow\" ><\/a>Redis \u8fde\u63a5<\/h3>\n<ul dir=\"auto\">\n<li>REDIS_HOST\uff1aRedis \u7684 Host<\/li>\n<li>REDIS_PORT\uff1aRedis \u7684\u7aef\u53e3<\/li>\n<li>REDIS_PASSWORD\uff1aRedis \u7684\u5bc6\u7801<\/li>\n<li>REDIS_DB\uff1aRedis \u7684\u6570\u636e\u5e93\u7d22\u5f15\uff0c\u5982 0\u30011<\/li>\n<li>REDIS_CONNECTION_STRING\uff1aRedis \u8fde\u63a5\u5b57\u7b26\u4e32<\/li>\n<li>REDIS_KEY\uff1aRedis \u50a8\u5b58\u4ee3\u7406\u4f7f\u7528\u5b57\u5178\u7684\u540d\u79f0<\/li>\n<\/ul>\n<h3 dir=\"auto\"><a id=\"user-content-\u5904\u7406\u5668\" class=\"anchor\" href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool#%E5%A4%84%E7%90%86%E5%99%A8\" aria-hidden=\"true\" rel=\"nofollow\" ><\/a>\u5904\u7406\u5668<\/h3>\n<ul dir=\"auto\">\n<li>CYCLE_TESTER\uff1aTester \u8fd0\u884c\u5468\u671f\uff0c\u5373\u95f4\u9694\u591a\u4e45\u8fd0\u884c\u4e00\u6b21\u6d4b\u8bd5\uff0c\u9ed8\u8ba4 20 \u79d2<\/li>\n<li>CYCLE_GETTER\uff1aGetter \u8fd0\u884c\u5468\u671f\uff0c\u5373\u95f4\u9694\u591a\u4e45\u8fd0\u884c\u4e00\u6b21\u4ee3\u7406\u83b7\u53d6\uff0c\u9ed8\u8ba4 100 \u79d2<\/li>\n<li>TEST_URL\uff1a\u6d4b\u8bd5 URL\uff0c\u9ed8\u8ba4\u767e\u5ea6<\/li>\n<li>TEST_TIMEOUT\uff1a\u6d4b\u8bd5\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4 10 \u79d2<\/li>\n<li>TEST_BATCH\uff1a\u6279\u91cf\u6d4b\u8bd5\u6570\u91cf\uff0c\u9ed8\u8ba4 20 \u4e2a\u4ee3\u7406<\/li>\n<li>TEST_VALID_STATUS\uff1a\u6d4b\u8bd5\u6709\u6548\u7684\u72b6\u6001\u5417<\/li>\n<li>API_HOST\uff1a\u4ee3\u7406 Server \u8fd0\u884c Host\uff0c\u9ed8\u8ba4 0.0.0.0<\/li>\n<li>API_PORT\uff1a\u4ee3\u7406 Server \u8fd0\u884c\u7aef\u53e3\uff0c\u9ed8\u8ba4 5555<\/li>\n<li>API_THREADED\uff1a\u4ee3\u7406 Server \u662f\u5426\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u9ed8\u8ba4 true<\/li>\n<\/ul>\n<h3 dir=\"auto\"><a id=\"user-content-\u65e5\u5fd7\" class=\"anchor\" href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/ProxyPool#%E6%97%A5%E5%BF%97\" aria-hidden=\"true\" rel=\"nofollow\" ><\/a>\u65e5\u5fd7<\/h3>\n<ul dir=\"auto\">\n<li>LOG_DIR\uff1a\u65e5\u5fd7\u76f8\u5bf9\u8def\u5f84<\/li>\n<li>LOG_RUNTIME_FILE\uff1a\u8fd0\u884c\u65e5\u5fd7\u6587\u4ef6\u540d\u79f0<\/li>\n<li>LOG_ERROR_FILE\uff1a\u9519\u8bef\u65e5\u5fd7\u6587\u4ef6\u540d\u79f0<\/li>\n<\/ul>\n<p dir=\"auto\">\u4ee5\u4e0a\u5185\u5bb9\u5747\u53ef\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u914d\u7f6e\uff0c\u5373\u5728\u8fd0\u884c\u524d\u8bbe\u7f6e\u5bf9\u5e94\u73af\u5883\u53d8\u91cf\u503c\u5373\u53ef\uff0c\u5982\u66f4\u6539\u6d4b\u8bd5\u5730\u5740\u548c Redis \u952e\u540d\uff1a<\/p>\n<div class=\"highlight highlight-source-shell position-relative overflow-auto\">\n<pre><span class=\"pl-k\">export<\/span> TEST_URL=http:\/\/weibo.cn\r\n<span class=\"pl-k\">export<\/span> REDIS_KEY=proxies:weibo<\/pre>\n<div class=\"zeroclipboard-container position-absolute right-0 top-0\"><\/div>\n<\/div>\n<p dir=\"auto\">\u5373\u53ef\u6784\u5efa\u4e00\u4e2a\u4e13\u5c5e\u4e8e\u5fae\u535a\u7684\u4ee3\u7406\u6c60\uff0c\u6709\u6548\u7684\u4ee3\u7406\u90fd\u662f\u53ef\u4ee5\u722c\u53d6\u5fae\u535a\u7684\u3002<\/p>\n<hr \/>\n<p dir=\"auto\">\u5b89\u88c5redis\u9519\u8bef \u518dubuntu20\u4e2d \u5b89\u88c5 apt-get install redis \u7136\u540e\u542f\u52a8redis-server \u53d1\u73b0\u9519\u8bef \u5c31\u662f\u6ca1\u9519\u8bef<\/p>\n<p dir=\"auto\">\u67e5\u770b\u7248\u672credis-server -V<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u5730\u5740https:\/\/github.com\/lvwuwei\/AutoBuy\/tree\/master\/Prox [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,20,1],"tags":[180,123,181,671],"class_list":["post-1091","post","type-post","status-publish","format-standard","hentry","category-linux","category-python","category-net-security","tag-ip"],"_links":{"self":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1091","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=1091"}],"version-history":[{"count":0,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1091\/revisions"}],"wp:attachment":[{"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}