我想从网站中提取数据,然后将其显示在我的网页上

2024-09-29 16:28:59 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我必须提取它并将其保存在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文件时,我都会得到这些数据

This is how it saving

原始表如下所示: This is what I want to extract


Tags: 文件csvimportdatatableallfindrequests
2条回答

在处理生成的数据时,需要在添加前从要转换为整数的字符串中删除,。。比如:

value = td.text.strip().replace(',', '')
t_data.append(int(value))

因此,事情变得有点简单,您的新t_数据将如下所示:

[1112, -1, 339, 1, 1451, 0, 8, -3, 1065, 29, 16, 2]

我想你现在可以很容易地理解文字了。另一件事是csv文件,它没有头。。也许将标题也写在上面会很有趣

你的t_data只是一个列列表-你只有一行-阅读writerows-它需要一个行列表,每一行都是一个列列表

使用

with open('data.csv', 'w', newline="") as new_file:
  csv_writer = csv.writer(new_file)
  csv_writer.writerows( [t_data] )  # fix here

最好还是不要替换数据并将其添加到页面中,而不将其归因于源。给它添加一个链接可以让人们有更多的方法来阅读它

如果文本文件中偶尔引用的内容给您添加了注释,您可能还需要使用标题和其他分隔符。它必须立即引用包含','的所有字符串:

with open('data.csv', 'w', newline="") as new_file:
    csv_writer = csv.writer(new_file, delimiter='|')
    csv_writer.writerow(headings)
    csv_writer.writerows([t_data])

要获得:

Number of confirmed cases in New Zealand|Number of probable cases|Number of confirmed and probable cases|Number of cases currently in hospital|Number of recovered cases|Number of deaths
1,112|-1|339|1|1,451|0|8|-3|1,065|29|16|2

相关问题 更多 >

    热门问题