TA的每日心情 | 无聊 4 天前 |
---|
签到天数: 530 天 连续签到: 2 天 [LV.9]测试副司令
|
1测试积点
导师让爬美团数据,自己编写了selenium爬取程序尝试爬取,但一到打开店铺处便弹出网络错误,貌似被检测为webdriver拒绝访问,代码如下,求带神指导
-
- ```python
- from selenium import webdriver
- import time
- from selenium.webdriver import ChromeOptions
- import time
- option = ChromeOptions()
- #224反屏蔽,webdriver属性设置为空 224
- # 设置headless模式,这种方式下无启动界面,能够加速程序的运行
- # chrome_options.add_argument("--headless")
- # 禁用gpu防止渲染图片
- # option.add_argument('disable-gpu')
- # # 设置不加载图片
- # option.add_argument('blink-settings=imagesEnabled=false')
-
-
- option.add_experimental_option("excludeSwitches",["enable-automation"])
- option.add_experimental_option("useAutomationExtension", False)
- bro = webdriver.Chrome(executable_path="./chromedriver.exe",options=option)
- # bro.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
- # "source": """
- # Object.defineProperty(navigator, 'webdriver', {
- # get: () => undefined
- # })
- # """
- # })
-
- bro.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
- "source": """
- Object.defineProperty(navigator, 'webdriver', {
- get: () => undefined
- })
- """
- })
-
- #最大化窗口
- bro.maximize_window()
- #登陆美团
- bro.get("https://passport.meituan.com/account/unitivelogin")
- bro.find_element_by_xpath('//*[@id="J-mobile-link"]').click()
- bro.find_element_by_xpath('//*[@id="user-agreement-wrap-text-circle"]/i').click()
- bro.find_element_by_xpath('//*[@id="login-mobile"]').send_keys('18602763878')
- bro.find_element_by_xpath('//*[@id="J-verify-btn"]').click()
- #接收验证码
- bro.find_element_by_xpath('//*[@id="login-verify-code"]').send_keys(input('请输入验证码:'))
- bro.find_element_by_xpath('//*[@id="J-mobile-form"]/div[6]/input[3]').click()
- #点击城市武汉
- cookies = bro.get_cookies()
- cookies_dict = {cookie['name']:cookie['value'] for cookie in cookies}
- # print(cookies_dict)
- # print(bro.window_handles)
- # #点击美食
- time.sleep(2)
- bro.find_element_by_xpath('//a[@href="//wh.meituan.com"]').click()
- time.sleep(2)
- bro.find_element_by_xpath('//*[@id="react"]/div/div/div[1]/div[1]/div/div[2]/ul/li[1]/span/span/a').click()
-
复制代码
|
|