使用Selenium Wire绕过检测并抓取Shopee且unDetected_chromedriver不起作用

问题描述 投票:0回答:3

我正在尝试制作一个shopee scraper。它与 undected_chromedriver 一起工作正常,但我想从网络选项卡获取 API 响应。所以我使用了 seleniumwire 和 unDetected_chromedriver,但网站能够检测到它是自动化并抛出一个登录页面。

这是我的代码不起作用:

import seleniumwire.undetected_chromedriver as uc 
import time
driver = uc.Chrome()
driver.get('https://shopee.co.id/search?keyword=mobile')
time.sleep(10)
driver.close()

这是我正在使用的:

import undetected_chromedriver as uc
import time
driver = uc.Chrome()
driver.get('https://shopee.co.id/search?keyword=mobile')
time.sleep(10)
driver.close()
python selenium-webdriver
3个回答
0
投票

以下代码打印页面中的所有标头/API 响应。它使用 SeleniumBase 来做到这一点。 (

pip install seleniumbase
).

将代码复制到文件中,然后使用

python
pytest
:

运行该文件
from pprint import pformat
from seleniumbase import BaseCase

if __name__ == "__main__":
    from pytest import main
    main([__file__, "--uc", "--uc-cdp", "--incognito", "-s"])

class CDPTests(BaseCase):
    def test_display_cdp_events(self):
        if not self.undetectable or not self.uc_cdp_events:
            self.get_new_driver(undetectable=True, uc_cdp_events=True)
        self.driver.add_cdp_listener("*", lambda data: print(pformat(data)))
        self.open("https://shopee.co.id/search?keyword=mobile")
        self.sleep(5)

由于它使用

driver.add_cdp_listener
,因此您不需要
selenium-wire
,它与
undetected-chromedriver
配合得不好。将会有很多输出,因此您可能需要过滤响应以获取特定详细信息。


0
投票

试试这个 SeleniumBase 的代码示例 您使用 SeleniumBase 共享的代码包含显示所有 Chrome DevTools 协议 (CDP) 事件的功能,这有助于调试,但本身无法解决验证码问题。与普通 Selenium 相比,SeleniumBase 提供了更简单的语法和一些附加功能,例如更容易设置不可检测的选项和更好的测试管理。这是您的代码的细分:

from pprint import pformat
from seleniumbase import BaseCase

if __name__ == "__main__":
    from pytest import main
    main([__file__, "--uc", "--uc-cdp", "--incognito", "-s"])

class CDPTests(BaseCase):
    def test_display_cdp_events(self):
        if not self.undetectable or not self.uc_cdp_events:
            self.get_new_driver(undetectable=True, uc_cdp_events=True)
        self.driver.add_cdp_listener("*", lambda data: print(pformat(data)))
        self.open("https://shopee.co.id/search?keyword=mobile")
        self.sleep(5)

不可检测:尝试避免被网站检测。 uc_cdp_events:使用 Chrome DevTools 协议事件来更深入地了解浏览器正在执行的操作。 --uc 和 --uc-cdp 标志:这些标志用于使用 SeleniumBase 启动 pytest,以通过 CDP 事件监控以更难以检测的模式运行。 建议 合规性:始终确保您的活动符合网站的服务条款。 道德使用:考虑绕过验证码等安全措施的道德影响。 替代方法:如果您经常面临验证码或机器人检测,请考虑该服务是否为您所需的数据提供合法的 API,或者访问网站寻找潜在的合作机会。


-1
投票

它实际上并没有检测到任何东西,只是无论如何都需要登录才能浏览网站。您要么必须登录(教程:https://www.browserstack.com/guide/login-automation-using-selenium-webdriver),要么只使用纯 html 抓取,不带 selenium(尽快获取页面源代码) ,在它抛出登录横幅之前)如果它满足您的需求。

可能有另一种解决方案,即通过更改CSS使横幅不可见,但我不认为我可以推荐它。

© www.soinside.com 2019 - 2024. All rights reserved.