我正在使用 beautifulsoup 抓取数据,但我没有获取所有数据

使用 python scraping 提取所有数据时出现问题

from bs4 import BeautifulSoup 
import requests 

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'}) 
for row in table.findAll('tr', attrs = {'class':['odd','even','total_row']}): 
    print(row.text)


月关宝盒
浏览 83回答 2
2回答

哆啦的时光机

from bs4 import BeautifulSoup import requests , csvdef 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())

噜噜哒

import requestsimport pandas as pdr = requests.get("https://www.worldometers.info/coronavirus/")df = pd.read_html(r.content)print(df)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5