繁体   English   中英

heroku 上的机器人 - 由于验证码无法废弃网站,即使我的电脑上一切正常

[英]Bot on heroku - unable to scrap sites because of captcha even though everything works on my pc

我在 heroku 上有一个简单的机器人,它与 discord 和废弃站点一起工作。 通常我使用reuqests模块来废弃网站,我得到页面源代码,仅此而已。 (注意:机器人不会发送垃圾邮件 ping 站点,每天/每周只发送一次,我正在 ping 的站点也是史诗游戏,但它不是唯一一个带有验证码的站点)


但后来我发现我的页面源代码中有验证码保护,所以我决定使用 chromedriver。 在 heroku 上设置 chromedriver 后,我仍然在网站上获得验证码保护。 在我的电脑上,即使没有以下任何选项,它也能正常工作,它从未要求验证码验证。

所以这就是我尝试的:(注意:我使用 未检测到的 chromedriver - selenium chromedriver 的优化版本)


1.在页面源代码中,它要求启用JavaScript ,所以我添加了 chromedriver 选项

import undetected_chromedriver as webdriver

opts = uc.ChromeOptions()
opts.add_argument("--enable-javascript")
driver = uc.Chrome(use_subprocess=True, options=opts)

driver.get(url)
print(driver.page_source)

仍然显示验证码验证,但现在没有 JavaScript 错误。


2.在做了一些研究之后,我发现 heroku IP 可能在某种阻止列表中,所以建议我将代理添加到 chromedriver 选项

import undetected_chromedriver as webdriver

opts = uc.ChromeOptions()
opts.add_argument("--enable-javascript")
opts.add_argument(f'--proxy-server=socks5://hostip:port')
driver = uc.Chrome(use_subprocess=True, options=opts)

driver.get(url)
print(driver.page_source)

3.我发现与第二个类似的选项似乎适用于其他选项,但站点仍然显示验证码

import undetected_chromedriver as webdriver
import os
import shutil
import tempfile

class ProxyExtension:
    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {"scripts": ["background.js"]},
        "minimum_chrome_version": "76.0.0"
    }
    """

    background_js = """
    var config = {
        mode: "fixed_servers",
        rules: {
            singleProxy: {
                scheme: "http",
                host: "%s",
                port: %d
            },
            bypassList: ["localhost"]
        }
    };

    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

    function callbackFn(details) {
        return {
            authCredentials: {
                username: "%s",
                password: "%s"
            }
        };
    }

    chrome.webRequest.onAuthRequired.addListener(
        callbackFn,
        { urls: ["<all_urls>"] },
        ['blocking']
    );
    """

    def __init__(self, host, port, user, password):
        self._dir = os.path.normpath(tempfile.mkdtemp())

        manifest_file = os.path.join(self._dir, "manifest.json")
        with open(manifest_file, mode="w") as f:
            f.write(self.manifest_json)

        background_js = self.background_js % (host, port, user, password)
        background_file = os.path.join(self._dir, "background.js")
        with open(background_file, mode="w") as f:
            f.write(background_js)

    @property
    def directory(self):
        return self._dir

    def __del__(self):
        shutil.rmtree(self._dir)


if __name__ == "__main__":
    proxy = ("hostip", port, "username", "pass")
    proxy_extension = ProxyExtension(*proxy)

    options = uc.ChromeOptions()
    options.add_argument("--enable-javascript")
    options.add_argument(f"--load-extension={proxy_extension.directory}")
    driver = uc.Chrome(use_subprocess=True, options=options)

我也尝试过添加 --headless 选项、将代理更改为 firefox、添加 nogpu 选项等选项。

我一直在尝试解决这个问题一个月,现在我希望有人知道我的问题的答案。

您可能会收到验证码,因为 Heroku 具有数据中心 ip 并且可能被标记或类似的东西。 您有几个选项可以尝试使用住宅代理并希望它没有被标记并且您没有获得验证码,或者您可以支付验证码解决方案,如2CaptchaCapmonster 不确定您获得的是哪种类型的验证码,但两者都支持 reCaptcha。 2Captcha Docs有很多很好的信息,用于在您解决验证码后提交验证码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM