【发布时间】:2020-05-03 08:22:25
【问题描述】:
我正在尝试从一个跟踪冠状病毒病例的网站上抓取数据。 网址是“https://www.coronatracker.com/”
我要抓取的表格是这样的: Corona record table
如果我们看一下它的 html 元素,它有一个包含 thead 和 tbody 的 table 元素 我正在尝试阅读整个表格,但我的尝试只阅读了标题。 我也想看表格的内容。
这是我编写的代码,希望我能读懂表格:
import requests
from bs4 import BeautifulSoup
url = "https://www.coronatracker.com/"
html_page = requests.get(url)
soup = BeautifulSoup(html_page.text, 'html.parser')
#pointing to div that is parent to table
data = soup.find('div' , {'class':'w-full block md:hidden mt-4 mb-8'})
#pointing to table
tables = data.find_all('table' , {'class':'table-auto w-full'})
#printing out the headings
for table in tables:
print(table.text)
#printing out the contents
body = table.find('tbody')
for data in body.find_all('tr'):
print(data)
问题在于阅读表格的内容,标题被完美地阅读了。
【问题讨论】:
-
你有什么错误吗?
标签: python html web-scraping