【发布时间】:2020-04-23 06:27:50
【问题描述】:
首先,我必须将其解压缩并保存在 CSV 文件中。这是我的代码
import requests
from bs4 import BeautifulSoup
import csv
result = requests.get("https://www.health.govt.nz/our-work/diseases-and-conditions/covid-19-novel-coronavirus/covid-19-current-situation/covid-19-current-cases")
src = result.content
soup = BeautifulSoup(src,'lxml')
cov19_table = soup.find("table", attrs={"class": "table-style-two"})
cov19_table_data = cov19_table.find_all("tbody")
headings = []
# gives me headings
for th in cov19_table_data[0].find_all("th"): #3rows
headings.append(th.text.replace('\n',' ').strip())
#print(headings)
t_data = []
for td in cov19_table_data[0].find_all("td"):
t_data.append(td.text.strip())
print(t_data)
with open('data.csv', 'w', newline="") as new_file:
csv_writer = csv.writer(new_file)
csv_writer.writerows(t_data)
每当我打开 data.csv 文件时,我都会得到这些数据
原始表格如下所示:
【问题讨论】:
-
简单地提供一个指向页面的链接,以便清楚数据的来源如何?
-
只是为了让事情更清楚一点:你显示代码,你显示你得到的,你显示原始表格格式......但最后,你的问题是什么?您希望什么作为所需的输出格式以及您面临的问题是什么? How to Ask
标签: python web-scraping