{"id":936,"date":"2024-11-07T16:14:17","date_gmt":"2024-11-07T08:14:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/936\/"},"modified":"2024-11-07T16:14:17","modified_gmt":"2024-11-07T08:14:17","slug":"python-%e6%80%8e%e4%b9%88%e8%bf%87%e6%bb%a4%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/936\/","title":{"rendered":"python \u600e\u4e48\u8fc7\u6ee4\u722c\u866b"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\uff1a\u57fa\u4e8e user-agent\u3001\u57fa\u4e8e ip \u5730\u5740\u3001\u57fa\u4e8e\u8bf7\u6c42\u6a21\u5f0f\u3001\u4f7f\u7528\u53cd\u722c\u866b\u6846\u67b6\u3002\u5b9e\u65bd\u5c4f\u853d\u63aa\u65bd\uff1a\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\u3001\u4f7f\u7528 captcha\u3001\u4f7f\u7528\u901f\u7387\u9650\u5236\u3001\u4f7f\u7528 ip \u9ed1\u540d\u5355\u3002\n<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-971\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110603421718751.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110603421718751.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110603421718751-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110603421718751-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110603421718751-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"python \u600e\u4e48\u8fc7\u6ee4\u722c\u866b\u63d2\u56fe\" alt=\"python \u600e\u4e48\u8fc7\u6ee4\u722c\u866b\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u5c4f\u853d\u722c\u866b<\/h2>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>\u722c\u866b\u662f\u81ea\u52a8\u6293\u53d6\u548c\u5904\u7406\u7f51\u9875\u5185\u5bb9\u7684\u7a0b\u5e8f\uff0c\u5bf9\u4e8e\u7f51\u7ad9\u7684\u5b89\u5168\u548c\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Python \u8fc7\u6ee4\u722c\u866b\u3002<\/p>\n<p><strong>\u65b9\u6cd5<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\uff1a<\/p>\n<p><strong>1. \u57fa\u4e8e User-Agent<\/strong><br \/>User-Agent \u662f\u6d4f\u89c8\u5668\u6216\u722c\u866b\u8fde\u63a5\u5230\u670d\u52a1\u5668\u65f6\u53d1\u9001\u7684\u6807\u8bc6\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8bf7\u6c42\u7684 User-Agent \u6765\u8bc6\u522b\u722c\u866b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>if request.user_agent.startswith(\"Googlebot\"):\n    return \"\u7981\u6b62\u722c\u866b\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u57fa\u4e8e IP \u5730\u5740<\/strong><br \/>\u67d0\u4e9b\u722c\u866b\u4f7f\u7528\u7279\u5b9a\u7684 IP \u5730\u5740\u8303\u56f4\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u8bf7\u6c42\u7684 IP \u5730\u5740\u4e0e\u5df2\u77e5\u722c\u866b IP \u5730\u5740\u5217\u8868\u8fdb\u884c\u6bd4\u8f83\u6765\u8bc6\u522b\u722c\u866b\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<p><strong>3. \u57fa\u4e8e\u8bf7\u6c42\u6a21\u5f0f<\/strong><br \/>\u722c\u866b\u901a\u5e38\u8868\u73b0\u51fa\u7279\u5b9a\u7684\u8bf7\u6c42\u6a21\u5f0f\uff0c\u4f8b\u5982\u9ad8\u9891\u7387\u7684\u8bf7\u6c42\u6216\u5f02\u5e38\u7684 HTTP \u5934\u3002\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u8bf7\u6c42\u6a21\u5f0f\u6765\u8bc6\u522b\u722c\u866b\u3002<\/p>\n<p><strong>4. \u4f7f\u7528\u53cd\u722c\u866b\u6846\u67b6<\/strong><br \/>\u6709\u8bb8\u591a\u53cd\u722c\u866b\u6846\u67b6\uff0c\u5982 Scrapy-Anti-Crawler \u548c Crawling Control Framework\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\u3002<\/p>\n<p><strong>\u5b9e\u65bd<\/strong><br \/>\u4e00\u65e6\u8bc6\u522b\u4e86\u722c\u866b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u5c4f\u853d\uff1a<\/p>\n<p><strong>1. \u8fd4\u56de\u9519\u8bef\u4ee3\u7801<\/strong><br \/>\u5411\u722c\u866b\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\uff0c\u4f8b\u5982 403\uff08\u7981\u6b62\u8bbf\u95ee\uff09\u6216 404\uff08\u672a\u627e\u5230\uff09\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 Captcha \u6216 ReCAPTCHA<\/strong><br \/>\u8981\u6c42\u7528\u6237\u5b8c\u6210 Captcha \u6216 ReCAPTCHA \u9a8c\u8bc1\uff0c\u4ee5\u533a\u5206\u4eba\u7c7b\u548c\u722c\u866b\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u901f\u7387\u9650\u5236<\/strong><br \/>\u5bf9\u6bcf\u4e2a IP \u5730\u5740\u6216 User-Agent \u7684\u8bf7\u6c42\u6570\u91cf\u65bd\u52a0\u901f\u7387\u9650\u5236\u3002\u8d85\u8fc7\u9650\u5236\u7684\u8bf7\u6c42\u5c06\u88ab\u963b\u6b62\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 IP \u9ed1\u540d\u5355<\/strong><br \/>\u5c06\u5df2\u77e5\u7684\u722c\u866b IP \u5730\u5740\u6dfb\u52a0\u5230\u9ed1\u540d\u5355\u4e2d\uff0c\u4ee5\u963b\u6b62\u5176\u8bbf\u95ee\u7f51\u7ad9\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><br \/>\u901a\u8fc7\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\uff0c\u4fdd\u62a4\u7f51\u7ad9\u7684\u5b89\u5168\u548c\u6027\u80fd\u3002\u8bf7\u6ce8\u610f\uff0c\u5c4f\u853d\u722c\u866b\u53ef\u80fd\u9700\u8981\u6301\u7eed\u7684\u7ef4\u62a4\u548c\u66f4\u65b0\uff0c\u4ee5\u5e94\u5bf9\u4e0d\u65ad\u53d8\u5316\u7684\u722c\u866b\u6280\u672f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fpython \u600e\u4e48\u8fc7\u6ee4\u722c\u866b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\uff1a\u57fa\u4e8e user-agent\u3001\u57fa\u4e8e ip \u5730\u5740\u3001\u57fa\u4e8e\u8bf7\u6c42\u6a21\u5f0f\u3001\u4f7f\u7528\u53cd\u722c\u866b\u6846\u67b6\u3002\u5b9e\u65bd\u5c4f\u853d\u63aa\u65bd\uff1a\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\u3001\u4f7f\u7528 captcha\u3001\u4f7f\u7528\u901f\u7387\u9650\u5236\u3001\u4f7f\u7528 ip \u9ed1\u540d\u5355\u3002 \u5982\u4f55\u5c4f\u853d\u722c\u866b \u7b80\u4ecb\u722c\u866b\u662f\u81ea\u52a8\u6293\u53d6\u548c\u5904\u7406\u7f51\u9875\u5185\u5bb9\u7684\u7a0b\u5e8f\uff0c\u5bf9\u4e8e\u7f51\u7ad9\u7684\u5b89\u5168\u548c\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Python \u8fc7\u6ee4\u722c\u866b\u3002 \u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\uff1a 1. \u57fa\u4e8e User-AgentUser-Agent \u662f\u6d4f\u89c8\u5668\u6216\u722c\u866b\u8fde\u63a5\u5230\u670d\u52a1\u5668\u65f6\u53d1\u9001\u7684\u6807\u8bc6\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8bf7\u6c42\u7684 User-Agent \u6765\u8bc6\u522b\u722c\u866b\u3002\u4f8b\u5982\uff1a if request.user_agent.startswith(&#8220;Googlebot&#8221;): return &#8220;\u7981\u6b62\u722c\u866b&#8221; \u767b\u5f55\u540e\u590d\u5236 2. \u57fa\u4e8e IP \u5730\u5740\u67d0\u4e9b\u722c\u866b\u4f7f\u7528\u7279\u5b9a\u7684 IP \u5730\u5740\u8303\u56f4\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u8bf7\u6c42\u7684 IP \u5730\u5740\u4e0e\u5df2\u77e5\u722c\u866b IP \u5730\u5740\u5217\u8868\u8fdb\u884c\u6bd4\u8f83\u6765\u8bc6\u522b\u722c\u866b\u3002 \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b 3. \u57fa\u4e8e\u8bf7\u6c42\u6a21\u5f0f\u722c\u866b\u901a\u5e38\u8868\u73b0\u51fa\u7279\u5b9a\u7684\u8bf7\u6c42\u6a21\u5f0f\uff0c\u4f8b\u5982\u9ad8\u9891\u7387\u7684\u8bf7\u6c42\u6216\u5f02\u5e38\u7684 HTTP \u5934\u3002\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u8bf7\u6c42\u6a21\u5f0f\u6765\u8bc6\u522b\u722c\u866b\u3002 4. \u4f7f\u7528\u53cd\u722c\u866b\u6846\u67b6\u6709\u8bb8\u591a\u53cd\u722c\u866b\u6846\u67b6\uff0c\u5982 Scrapy-Anti-Crawler \u548c Crawling Control Framework\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u5c4f\u853d\u722c\u866b\u3002 \u5b9e\u65bd\u4e00\u65e6\u8bc6\u522b\u4e86\u722c\u866b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u5c4f\u853d\uff1a 1. \u8fd4\u56de\u9519\u8bef\u4ee3\u7801\u5411\u722c\u866b\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\uff0c\u4f8b\u5982 403\uff08\u7981\u6b62\u8bbf\u95ee\uff09\u6216 404\uff08\u672a\u627e\u5230\uff09\u3002 2. \u4f7f\u7528 Captcha \u6216 ReCAPTCHA\u8981\u6c42\u7528\u6237\u5b8c\u6210 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-936","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=936"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}