Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

Python replacing empty list in a list with a string to avoid IndexError: list index out of range

I wrote this code and it works to fine to scrape H1 tags from a list of websites. There are some particular websites that don't have an H1 so an empty list is returned and it gives IndexError: list index out of range, and stops the script.

    list_flagged = df['Websites'].to_list()

    new_flagged_list = []

    for site in list_flagged:                                                                
        quote_page = requests.get(site, headers=random_header)
        soup = BeautifulSoup(quote_page.text, 'html.parser')
        h1tag = soup.find_all('h1')
        titles = [(h1.get_text()).strip() for h1 in h1tag] 
        appended = new_flagged_list.append(titles)
        print('appended')
        if new_flagged_list == ['']:
            ['x']    
        new = [x[0] for x in new_flagged_list]

I tried with if new_flagged_list == ['']: to change an empty row but still the error appears. I don't understand anyway why

    new = [x[0] for x in new_flagged_list]

ignores an empty list in a list with list index error. Why it cannot keep an empty list?

How can I change the empty list in a list with whatever string to avoid the error?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use

if not new_flagged_list:
       print("do task here")

In the above code you are checking if list is empty or not


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...