加油努力zmh 2022-03-09 19:47 采纳率: 77.8%
浏览 384
已结题

美团selenium爬虫貌似被检测为webdriver不知如何解决

导师让爬美团数据,自己编写了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()

```

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 5月11日
      • 已采纳回答 5月3日
      • 创建了问题 3月9日

      悬赏问题

      • ¥15 图片上传成功,但界面显示不出来,是跨域问题?
      • ¥15 ANSYS APDL循环结果输出
      • ¥15 ArcGIS处理MODIS 09数据,计算EVI 像元值大小问题
      • ¥15 提个关于sql的问题
      • ¥15 Python库一直装不好
      • ¥30 在linux上调用海康SDK没有进入函数内
      • ¥15 FreeRTOS有任务卡死
      • ¥15 vue网页地址中的#问题
      • ¥20 一个js里的函数的retur值想返回另一个js的变量值,应该怎么写?
      • ¥15 登陆器jar2exe过期了怎么样重新授权