Bootstrap

爬取今日头条热榜(selenium)

导包什么的不做赘述,直接上代码

s = Service(executable_path=r'D:\chromedriver.exe')
browser = webdriver.Chrome(service=s)

browser.get('https://www.toutiao.com')


# 验证(自己的电脑无验证)
# 等待两秒数据加载
time.sleep(2)
# 垂直滚动(每次滚动500px)
# for i in range(0,10):
#    browser.execute_script('document.documentElement.scrollTop = '+ str(i*500))


element_re = browser.find_elements(By.XPATH, '//*[@id="root"]/div/div[5]/div[2]/div[2]/div/div/div/ol/li')
for element in element_re:
    for tis in element.find_elements(By.TAG_NAME, 'a'):
        print(tis.text)
    for lis in element.find_elements(By.TAG_NAME, 'a'):
        print(lis.get_attribute('href'))
else:
    element_hot = browser.find_element(By.CLASS_NAME, 'refresh').click()

只爬取一页的数据 

代码编写于2023.8.13 并可正常运行

;