【问题标题】:Selenium + chromedriver is not able to load HTML in only some pages, but not otherSelenium + chromedriver 无法仅在某些页面中加载 HTML,但不能在其他页面中加载
【发布时间】:2020-02-09 11:27:44
【问题描述】:

这是在我问过here的同一个项目中

但是,这次我遇到了另一个问题。基本上,我试图在More information 切换链接下获取UpdatedPublished 两个字段(为此切换选择的HTML 是"//a[@class='toggle_info_btn']"

在一页https://thehive.itch.io/promnesia 中,我能够检索到这两个字段。但是在另一个页面https://dmullinsgames.itch.io/paper-jekyll 中,即使两者具有相同的 HTML,我也不能。

这是我的代码(正如 Yosuva A 在上一个问题中所建议的那样):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome('chromedriver')  # Optional argument, if not specified will search path.
driver.implicitly_wait(15)

driver.get("https://dmullinsgames.itch.io/paper-jekyll");
driver.find_element(By.XPATH,"//a[@class='toggle_info_btn']").click()
time.sleep(2)
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//div[@class='game_info_panel_widget']/table//tr//td"))) #Wait for specific element 

table_rows= driver.find_elements(By.XPATH,"//div[@class='game_info_panel_widget']/table//tr//td")

for rows in table_rows:
    print(rows.text)

driver.quit()

运行此程序时,我看到 chromedriver 打开一个带有该页面的 Chrome 窗口,但我没有看到 UpdatedPublished 两个字段。 以下是 chromedriver 在打开 Chrome 实例时看到的内容:

实际上是这样的: 请告诉我这是什么问题...

【问题讨论】:

  • 第二页没有updatedpublished字段。那么你想如何获得它们呢?
  • 嗨@D.Weltrowski,是的...我在浏览器中打开它,我可以在More information 下看到它们(但您需要单击切换)。确认一下,游戏是paper-jekyll 可以再查一下吗?更新截图
  • 所以这里的答案很简单。在您登录之前,您不会在第二个网站上看到更新和发布。这意味着您需要在下载数据之前使用 selenium 登录。
  • 但这并不能解释为什么chromedriver可以在第一个链接https://thehive.itch.io/promnesia中看到这两个字段,而无需登录
  • 显然第一个不需要登录即可查看这些字段。这就是全部。自从我作为客人看到了第一个领域。

标签: python python-3.x selenium scrapy selenium-chromedriver


【解决方案1】:

正如 D.Weltrowski 在评论中回答的那样,页面中的某些字段只有在登录时才可见。此外,同一字段可以在一个页面上可见但在另一个页面上不可见。因此,解决方案是在抓取之前让 Scrapy 登录,它将能够抓取这些数据。认证爬取信息here

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多