javascript - 使用显示更多按钮抓取网页
问题描述
我想用“显示更多”按钮抓取谷歌学者页面。使用该平台的帮助解决我之前提出的问题,我编写了以下代码,以便单击“显示更多”按钮。但是,我仍然遇到问题。对于带有多个“显示更多”按钮的配置文件,只有第一个被点击。我不明白为什么会这样。我将不胜感激任何帮助。
from selenium import webdriver
import time
from bs4 import BeautifulSoup
import pandas as pd
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
chrome_path = r"C:\Users\ish05\Desktop\python\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://scholar.google.com/citations?user=cp-8uaAAAAAJ&hl=en")
time.sleep(3)
show_more = driver.find_elements_by_tag_name('button')
for x in range(len(show_more)):
if show_more[x].is_displayed():
driver.execute_script("arguments[0].click();", show_more[x])
time.sleep(3)
解决方案
它运行一个的原因是因为它在每一页上出现一个。
您需要使用无限循环,然后在页面上搜索,如果有,然后单击 else no more button break from the loop。
from selenium import webdriver
import time
chrome_path = r"C:\Users\ish05\Desktop\python\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://scholar.google.com/citations?user=cp-8uaAAAAAJ&hl=en")
time.sleep(3)
while True:
try:
show_more = driver.find_element_by_xpath("//button[.//span[text()='Show more'] and not(@disabled)]")
driver.execute_script("arguments[0].click();", show_more)
print("Show more button clicked")
time.sleep(2)
except:
print("No more Show more button")
break
您将在控制台上看到以下输出
Show more button clicked
Show more button clicked
Show more button clicked
Show more button clicked
Show more button clicked
No more Show more button
推荐阅读
- python - 将两个数据框列压缩到 dict 获取 TypeError: 'dict' object is not callable
- sql - SQL 平均天数差异
- testing - 每个端点的 PACT 合同测试
- r - Windows 10,R 版本 3.5.3,list.files 函数不适用于特定的重音文件
- css - 在 Django 中加载静态文件
- typescript - 来自接口实现的 Typescript 泛型推断
- python - Jenkins ModuleNotFoundError:没有名为“jenkinsapi.jenkins”的模块;'jenkinsapi' 不是一个包
- tkinter - TKinter 布局 - 新对象,新行
- python - 使用 psutil 查找所有前台应用程序
- php - 如何计算两个时间戳之间的差异,然后按月分组以用于 SQL 中的图表目的