会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 556楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 558楼

image.png

Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 559楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 560楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 562楼

老师,反检测只能适用第一个选项卡的页面吗?我的程序是从虎牙直播的主页开始的,点击英雄联盟分类后,跳转到直播页面(在一个新的选项卡中打开的),然后,在这里检测到window.navigator.webdriver为true。这样里面的数据就爬取不到了。所以怎么解决这个问题呢?

from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common import action_chains
from selenium.webdriver.common.action_chains import ActionChains
import time


url = 'https://www.huya.com/'
option = webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ['enable-automation','enable-logging'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => false
    })
  """
})
driver.get(url)
time.sleep(1)
# 点击分类中的英雄联盟
# driver.find_element_by_id('hy-nav-category').click()
fenlei = driver.find_element_by_id('hy-nav-category')  # 分类tag
shubiao = ActionChains(driver)
shubiao.move_to_element(fenlei).perform() # 鼠标移动到分类上
time.sleep(2)
lol = driver.find_element_by_xpath("//div[@class='nav-expand-list nav-expand-game']/dl[1]/dd[1]")  # 英雄联盟tag
shubiao.click(lol).perform()
time.sleep(2) # 等待页面渲染
zhubo = driver.find_elements_by_xpath("//ul[@id='js-live-list']/li/span/span[@class='avatar fl']/i")
print(zhubo)
print(len(zhubo))
print(type(zhubo))
for i in zhubo:
    print(i)
    print(type(i))
    print(i.text())

Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 564楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 565楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 566楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 567楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 569楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 570楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637