2 回答
TA贡献1779条经验 获得超6个赞
from bs4 import BeautifulSoup
import requests , csv
def SaveAsCsv(list_of_rows):
try:
print('\nSaving CSV Result')
with open('output.csv', 'a', newline='', encoding='utf-8') as outfile:
writer = csv.writer(outfile)
writer.writerow(list_of_rows)
print("rsults saved successully")
except PermissionError:
print("Please make sure output.csv is closed \n")
url = "https://www.worldometers.info/coronavirus/"
req_data = requests.get(url)
soup = BeautifulSoup(req_data.text, 'html.parser')
table = soup.find('table', attrs={'id': 'main_table_countries_today'})
header = [col_name.text.rstrip('\n').strip() for col_name in table.select('thead th')]
SaveAsCsv(header)
for row in table.select('tbody tr'):
tds = [td.get_text().rstrip('\n').strip() for td in row.select('td')]
SaveAsCsv(tds)
# print(row.text.rstrip('\n').strip())
TA贡献1784条经验 获得超7个赞
import requests
import pandas as pd
r = requests.get("https://www.worldometers.info/coronavirus/")
df = pd.read_html(r.content)
print(df)
- 2 回答
- 0 关注
- 82 浏览
添加回答
举报