When I try to index in the statement, it says index out of range. I am trying to scrape stuff from the website.
import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.set.or.th/set/factsheet.do?symbol=TRUE&ssoPageId=3&language=en&country=US')
soup = BeautifulSoup(page.text, 'html.parser')
list_stuff = list()
for x in soup.findAll('table',{'class':'factsheet'}):
for tr in x.findAll('tr'):
stuff=[td for td in tr.stripped_strings]
if stuff[0] == 'Beta':
list_stuff.append(stuff[1])
The code returns an error saying list index out of range
and points to the stuff[0]
line in the for loop
Add a step to check if the tuple is empty or not.
if not not stuff: #check if stuff is not empty
if stuff[0] == 'Beta':
list_stuff.append(stuff[1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.