【发布时间】:2021-12-20 05:49:00
【问题描述】:
我正在尝试使用 Python 从网站上抓取表格,但由于某种原因,我所有已知的方法都失败了。有一张桌子在 https://www.nbc4i.com/news/state-news/535-new-cases-of-covid-19-reported-in-ohio-schools-in-past-week/ 45 页。我尝试使用:requests、requests-html(渲染它)、BeautifulSoup 和 selenium 来抓取它。这是我的代码之一,我不会在这里复制我尝试过的所有代码,方法相似,只是使用不同的 Python 库:
from requests_html import HTMLSession
from bs4 import BeautifulSoup
session = HTMLSession()
page = session.get('https://www.nbc4i.com/news/state-news/535-new-cases-of-covid-19-reported-in-ohio-schools-in-past-week/')
page.html.render(timeout=120)
soup = BeautifulSoup(page.content, 'lxml') #also tried with page.text and 'html.parser' and all permutations
table = soup.find_all(id='table')
我的表变量在这里是一个空列表,它不应该是。我尝试使用 selenium 在表格中查找任何其他 web 元素,我也尝试按类查找 xpath,但所有这些都未能找到表格或其任何部分。我用这些方法刮了很多类似的网站,在这之前我从来没有遇到过问题。 请问有什么想法吗?
【问题讨论】:
-
表格在 iframe 中。必须切换到 iframe,然后才能查询“表格”内容。
-
是的,我现在可以看到了,谢谢,伙计。这是我通过网络抓取学习html并且我缺乏一些基础知识的时候......
标签: python selenium web-scraping beautifulsoup python-requests-html