繁体   English   中英

Python 用字符串替换列表中的空列表以避免 IndexError: list index out of range

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

我编写了这段代码,它可以很好地从网站列表中抓取 H1 标签。 有一些特定的网站没有 H1,因此返回一个空列表并给出 IndexError: list index out of range,并停止脚本。

    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]

我尝试使用 if new_flagged_list == ['']: 更改空行,但仍然出现错误。 反正我不明白为什么

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

忽略列表索引错误的列表中的空列表。 为什么它不能保留一个空列表?

如何使用任何字符串更改列表中的空列表以避免错误?

谢谢!

您可以使用

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

在上面的代码中,您正在检查列表是否为空

我不明白你想做什么

if new_flagged_list == ['']:
            ['x']  

您在这里没有为变量分配任何东西。 你也可以试试:

if titles:
    appended = new_flagged_list.append(titles)

这样当标题不为空时,您只有 append 。

这些行:

        if new_flagged_list == ['']:
            ['x'] 

除了创建并立即销毁仅包含“x”的列表之外,不要做任何事情。 我猜你想要:

new_flagged_list = ['x']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM