【问题标题】:how to find grids in table using splinter?如何使用 splinter 在表格中查找网格?
【发布时间】:2019-02-18 08:35:33
【问题描述】:

我想在表格中找到这样的元素:

我试过了

b.find_by_id('resourceTd_5897')

b.find_by_css('td#resourceTd_5897.resourceTd')

甚至

b.find_by_tag('td')

他们都引发no elements could be found with......异常。

如何在表格中找到网格?

【问题讨论】:

  • 这些元素是动态的,即 id 是随时间变化的还是静态的?如果先选择 id 为 resourceTable 的表会怎样?
  • 之前没用过splinter,但是如果表格是用JavaScript生成的,表格会在DOM加载完成后加载,所以你需要一个无头浏览器在抓取之前先渲染JavaScript。或者表格可能会加载到 iframe 中。如果两者都不是,请检查b 变量是否设置正确。
  • 不知道这些元素是不是动态的,怎么查。我试过b.find_by_id('resourceTable'),还是找不到。
  • @xinyueLiu 能否提供您的目标网页网址?

标签: python dom web-scraping splinter


【解决方案1】:

等待一段时间页面完全加载怎么样。

from splinter import Browser
b = Browser("chrome")
b.visit("https://splinter.readthedocs.io/en/latest/tutorial.html")

if b.is_element_present_by_xpath("//div[@id='create-a-browser-instance']/h2", wait_time=5):
    # wait until the element's appearing
    print(b.find_by_xpath("//div[@id='create-a-browser-instance']/h2"))

splinter documents上查看更多信息

希望对你有帮助..

【讨论】:

  • 感谢您的回复。我不知道如何为要选择的网格编写xpath。但我认为下面的代码是相等的但失败了。if b.is_element_present_by_id('resourceTd_5897', wait_time = 5): print(b.find_by_id('resourceTd_5897'))
  • @xinyueLiu,是的,它们是一样的,而您提供的网址超出我的能力范围的问题是本地网址....
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 2014-03-15
  • 2017-09-19
  • 1970-01-01
  • 2017-06-04
相关资源
最近更新 更多