Python getting stuck in exception flow

Viewed 71

Very new to Python and currently stumped at the moment. What keeps happening is the first several rows work correctly, print, and write to the file just fine, but once it gets to row 11, it throws a "list index out of range" error, and does that for the remaining rows as well.

Cant figure out for the life of me how once it fails at row 11, to start over on row 12 and run the try again (should be successful on remaining rows)

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = "https://coinmarketcap.com/"

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")
rows = page_soup.find("tbody").findAll("tr")

filename = "coins.csv"
f = open(filename, "w")

headers = "Rank, Name, Price, Circulating Supply\n"

f.write(headers)

for row in rows:

    try:
        rank_number = row.findAll('td')[1].text
        coin_name = row.findAll('td')[2].text
        coin_price = row.findAll('td')[3].text
        supply = row.findAll('td')[8].text

        print(rank_number + "," + coin_name + "," + coin_price.replace(",","") + "," + supply.replace(",","") + "\n")
        f.write(rank_number + "," + coin_name + "," + coin_price.replace(",","") + "," + supply.replace(",","") + "\n")

    except Exception as e:
        print(e)
        continue

f.close()

Any help would be greatly appreciated!

Thanks!

0 Answers
Related