【问题标题】:web scraping with Python and BeautifulSoup errror TypeError: can only concatenate str (not "NoneType") to str使用 Python 和 BeautifulSoup 进行网页抓取错误 TypeError: can only concatenate str (not "NoneType") to str
【发布时间】:2020-06-24 15:10:33
【问题描述】:
anchors = soup.find_all('a')`all_links = set()`for link in anchors:
if (link.get('href') != '#'):`linkText = "https://www.newegg.com/" + `link.get('href')``all_links.add(link)`print(linkText)

`#我的最后三行代码应该如何改正?`````

【问题讨论】:

    标签: python-3.x beautifulsoup


    【解决方案1】:

    link.get('href') 函数提供默认值:link.get('href', '')。默认是None,所以如果链接没有'href'属性,你会得到错误。

    anchors = soup.find_all('a')
    all_links = set()
    
    for link in anchors:
        if link.get('href', '') != '#':
            linkText = "https://www.newegg.com/" + link.get('href', '')
            all_links.add(link)
            print(linkText)
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2021-08-31
    • 2020-11-21
    • 2020-08-29
    • 2022-11-01
    • 1970-01-01
    • 2020-09-25
    • 2021-08-18
    • 1970-01-01
    • 2019-02-02
    相关资源
    最近更新 更多