【问题标题】:Using Python (Selenium) to Scrape IMDB (.click() is not working)使用 Python (Selenium) 抓取 IMDB(.click() 不起作用)
【发布时间】:2021-07-28 13:55:02
【问题描述】:

我正在尝试使用 tutorial 从 IMDB 中抓取特定电影的列表。

代码运行良好,期望 for click 获取 URL 然后保存在内容中​​。它不工作。问题是运行代码时chrome没有任何变化,如果有人能提供帮助,我真的很感激。

content = driver.find_element_by_class_name("tF2Cxc").click()
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import time

movie = 'Wolf Totem'
driver = webdriver.Chrome(executable_path=r"D:\chromedriver.exe")

#Go to Google
driver.get("https://www.google.com/")

#Enter the keyword
driver.find_element_by_name("q").send_keys(movie + " imdb")
time.sleep(1)

#Click the google search button
driver.find_element_by_name("btnK").send_keys(Keys.ENTER)
time.sleep(1)

【问题讨论】:

  • “不工作”是什么意思?它会给你任何错误回溯吗?

标签: python selenium selenium-webdriver scrape


【解决方案1】:

您使用了错误的定位器。
要在 Google 页面上打开搜索结果,您应该使用以下命令:

driver.find_element_by_xpath("//div[@class='yuRUbf']/a").click()

此定位器将匹配所有 10 个搜索结果,因此第一个匹配是第一个搜索结果。
此外,单击该元素不会为您提供任何内容,只需打开第一个搜索结果标题下方的第一个链接。

【讨论】:

  • 谢谢@Prophet。它工作得很好。打印内容时,不包含 URL。
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 2022-08-22
  • 2022-01-07
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多