【问题标题】: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)