Python IndexError после вывода - PullRequest
0 голосов
/ 15 марта 2020
import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.worldometers.info/coronavirus/')
c = r.content
soup = BeautifulSoup (c, 'html.parser')

indexs = 0
table1 = soup.find("table")
table2 = table1.find('tbody')
table3 = table2.find_all('td')
for index,i in enumerate(table3):
    print(f'{index} - {table3[indexs].text} - Total Cased - {table3[indexs+1].text}')
    indexs+=9

Привет, ребята, у меня есть проблема, этот код делает его работу Потрясающе выводить то, что он должен, но в конце, когда он выводит последний, говорящий об ошибке вроде этого

Exception has occurred: IndexError
list index out of range
  File "C:\Users\SGamr\Desktop\Python\worldmetters.py", line 14, in <module>
    print(f'{index} - {table3[indexs].text} - Total Cased - {table3[indexs+1].text}')

помогите мне, спасибо < 3

1 Ответ

0 голосов
/ 15 марта 2020
import pandas as pd

df = pd.read_html("https://www.worldometers.info/coronavirus/")[0]

df.to_csv("result.csv", index=False)

Вывод: Check-online

...