{"id":11,"date":"2023-05-25T13:51:25","date_gmt":"2023-05-25T05:51:25","guid":{"rendered":"https:\/\/www.cloudbypass.com\/tutorial\/?p=11"},"modified":"2024-04-30T20:01:15","modified_gmt":"2024-04-30T12:01:15","slug":"python-selenium%e5%a6%82%e4%bd%95%e7%bb%95%e8%bf%87cloudflare%e6%8a%93%e5%8f%96%e7%bd%91%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/www.cloudbypass.com\/tutorial\/11.html","title":{"rendered":"Python Selenium\u5982\u4f55\u7ed5\u8fc7cloudflare\u6293\u53d6\u7f51\u9875"},"content":{"rendered":"\n<p>\u548c\u8bb8\u591a\u7f51\u7ad9\u4e00\u6837\uff0cCloudflare\u4e5f\u4f1a\u5bf9\u8bbf\u95ee\u8fdb\u884c\u68c0\u6d4b\uff0c\u770b\u662f\u5426\u7531Selenium bot\u53d1\u8d77\u3002\u8fd9\u79cd\u68c0\u6d4b\u4e3b\u8981\u5173\u6ce8\u662f\u5426\u5b58\u5728\u7279\u6709\u7684js\u53d8\u91cf\uff0c\u5982\u5305\u542b&#8221;selenium&#8221;\u3001&#8221;webdriver&#8221;\u7684\u53d8\u91cf\uff0c\u6216\u8005\u5305\u542b&#8221;$cdc_&#8221;\u3001&#8221;$wdc_&#8221;\u7684\u6587\u4ef6\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u9a71\u52a8\u7a0b\u5e8f\u7684\u68c0\u6d4b\u673a\u5236\u53ef\u80fd\u4e0d\u540c\uff0c\u4ee5\u4e0b\u7684\u89e3\u51b3\u65b9\u6848\u4e3b\u8981\u9488\u5bf9chromedriver\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1\u3001\u4f7f\u7528Undetected-chromedriver \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u4fbf\u5229\u7684\u5305\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\u3002\u7136\u540e\u50cf\u4e0b\u9762\u8fd9\u6837\u521d\u59cb\u5316\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4e4b\u540e\u7684\u64cd\u4f5c\u5c31\u50cf\u5e38\u89c4\u7684Selenium\u4f7f\u7528\u4e00\u6837\u3002<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code\nimport undetected_chromedriver as uc\ndriver = uc.Chrome()\ndriver.get('https:\/\/nowsecure.nl')\n\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2\u3001\u76f4\u63a5\u4fee\u6539chromedriver\u53ef\u6267\u884c\u6587\u4ef6 \u4f60\u53ef\u4ee5\u5c06key\u53d8\u91cf\u66f4\u6539\u4e3a\u4efb\u610f\u4e0d\u5305\u542b&#8221;cdc&#8221;\u7684\u5b57\u7b26\u3002<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">javascriptCopy code\n\/**\n * Returns the global object cache for the page.\n * @param {Document=} opt_doc The document whose cache to retrieve. Defaults to\n *     the current document.\n * @return {!Cache} The page's object cache.\n *\/\nfunction getPageCache(opt_doc, opt_w3c) {\n  var doc = opt_doc || document;\n  var w3c = opt_w3c || false;\n  \/\/ |key| is a long random string, unlikely to conflict with anything else.\n  var key = '$cdc_asdjflasutopfhvcZLmcfl_';\n  if (w3c) {\n    if (!(key in doc))\n      doc[key] = new CacheWithUUID();\n    return doc[key];\n  } else {\n    if (!(key in doc))\n      doc[key] = new Cache();\n    return doc[key];\n  }\n}\n\n<\/pre>\n\n\n\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5728\u672c\u8d28\u4e0a\u6ca1\u6709\u592a\u5927\u7684\u533a\u522b\u3002\u5b9e\u9645\u4e0a\uff0cundetected-chromedriver\u5728\u542f\u52a8chromedriver\u65f6\u4f1a\u5e94\u7528\u4e00\u4e2a\u8865\u4e01\uff0c\u5b8c\u6210\u4e86\u4fee\u6539key\u7684\u6b65\u9aa4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code\ndef patch_exe(self):\n    \"\"\"\n    Patches the ChromeDriver binary\n    :return: False on failure, binary name on success\n    \"\"\"\n    logger.info(\"patching driver executable %s\" % self.executable_path)\n \n    linect = 0\n    replacement = self.gen_random_cdc() #\u8fd9\u91cc\u4fee\u6539\u4e86cdc\u7684\u540d\u79f0\n    with io.open(self.executable_path, \"r+b\") as fh:\n        for line in iter(lambda: fh.readline(), b\"\"):\n            if b\"cdc_\" in line:\n                fh.seek(-len(line), 1)\n                newline = re.sub(b\"cdc_.{22}\", replacement, line)\n                fh.write(newline)\n                linect += 1 <\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3\u3001\u4f7f\u7528\u7a7f\u4e91API\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed5\u8fc7Cloudflare\u7684\u673a\u5668\u4eba\u9a8c\u8bc1\uff0c\u5373\u4f7f\u60a8\u9700\u8981\u53d1\u900110\u4e07\u4e2a\u8bf7\u6c42\uff0c\u4e5f\u4e0d\u5fc5\u62c5\u5fc3\u88ab\u8bc6\u522b\u4e3a\u6293\u53d6\u8005\u3002<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u7a7f\u4e91API\u5373\u53ef\u7a81\u7834\u6240\u6709\u53cdAnti-bot\u673a\u5668\u4eba\u68c0\u67e5\uff0c\u8f7b\u677e<a href=\"https:\/\/www.cloudbypass.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u7ed5\u8fc7Cloudflare<\/a>\u3001CAPTCHA\u9a8c\u8bc1\uff0cWAF\uff0cCC\u9632\u62a4\uff0c\u5e76\u63d0\u4f9b\u4e86HTTP API\u548cProxy\uff0c\u5305\u62ec\u63a5\u53e3\u5730\u5740\u3001\u8bf7\u6c42\u53c2\u6570\u3001\u8fd4\u56de\u5904\u7406\uff1b\u4ee5\u53ca\u8bbe\u7f6eReferer\uff0c\u6d4f\u89c8\u5668UA\u548cheadless\u72b6\u6001\u7b49\u5404\u6d4f\u89c8\u5668\u6307\u7eb9\u8bbe\u5907\u7279\u5f81\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/console.cloudbypass.com\/#\/document\" target=\"_blank\" rel=\"noreferrer noopener\">API\u6587\u6863 &#8211; \u7a7f\u4e91API &amp; \u7528\u6237\u63a7\u5236\u53f0 (cloudbypass.com)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u548c\u8bb8\u591a\u7f51\u7ad9\u4e00\u6837\uff0cCloudflare\u4e5f\u4f1a\u5bf9\u8bbf\u95ee\u8fdb\u884c\u68c0\u6d4b\uff0c\u770b\u662f\u5426\u7531Selenium bot\u53d1\u8d77\u3002\u8fd9\u79cd\u68c0\u6d4b\u4e3b\u8981\u5173\u6ce8&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"https:\/\/www.cloudbypass.com\/tutorial\/11.html\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;Python Selenium\u5982\u4f55\u7ed5\u8fc7cloudflare\u6293\u53d6\u7f51\u9875&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":41,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36,43,32],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chatgpt-cloudflare-verification","category-chatgpt-bypass-cloudflare","category-cloudflare-5-second-shield"],"_links":{"self":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":12,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts\/11\/revisions\/12"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/media\/41"}],"wp:attachment":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}