【问题标题】:'NoneType' object has no attribute 'text' in web crawling with beautifulsoup'NoneType' 对象在使用 beautifulsoup 进行网络爬行时没有属性'text'
【发布时间】:2021-06-07 22:22:57
【问题描述】:

我在尝试抓取网络数据时出错,错误消息显示

标签信息:“NoneType”对象没有“文本”属性

网站在这里 https://www.target.com/p/nong-shim-noodle-bowl-soup-spicy-kimchi-flavor-3-03oz/-/A-15137591#lnk=sametab

        try:
            label_info = soup.find('div', {'class': 'h-bg-white h-margin-a-default'})
            if debug: print('Label info:',label_info.text)
            if debug: print('')
        except Exception as e:
            label_info = ''
            if debug: print("Label info:", e)
            pass

谁能指导我为什么这不起作用?

【问题讨论】:

    标签: python beautifulsoup web-crawler


    【解决方案1】:

    您可以将 CSS 类指定为字符串列表,而不是一个字符串:

    import requests
    from bs4 import BeautifulSoup
    
    url = 'https://www.target.com/p/nong-shim-noodle-bowl-soup-spicy-kimchi-flavor-3-03oz/-/A-15137591#lnk=sametab'
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    
    print(soup.find('div', {'class': ['h-bg-white', 'h-margin-a-default']}).get_text(strip=True, separator='\n'))
    

    打印:

    Highlights
    Spicy kimchi flavor
    Fresh noodle texture and robust flavor
    Ready in only 3 minutes— just add hot water
    Convenient on-the-go meal
    

    【讨论】:

    • 该死!你是我的救星,关键是我需要包含多个类作为 ['a', 'b', 'c'] !!!!非常感谢@Andrej
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    相关资源
    最近更新 更多