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文件时,我都会得到这些数据
原始表如下所示:
在处理生成的数据时,需要在添加前从要转换为整数的字符串中删除
,
。。比如:因此,事情变得有点简单,您的新t_数据将如下所示:
我想你现在可以很容易地理解文字了。另一件事是csv文件,它没有头。。也许将标题也写在上面会很有趣
你的
t_data
只是一个列列表-你只有一行-阅读writerows-它需要一个行列表,每一行都是一个列列表使用
最好还是不要替换数据并将其添加到页面中,而不将其归因于源。给它添加一个链接可以让人们有更多的方法来阅读它
如果文本文件中偶尔引用的内容给您添加了注释,您可能还需要使用标题和其他分隔符。它必须立即引用包含
','
的所有字符串:要获得:
相关问题 更多 >
编程相关推荐