如何解决列表理解由于未知原因返回“NoneType”TypeError
我正在尝试从我从网页中检索到的链接列表中获取链接地址的特定字符串。
from urllib.request import urlopen
from bs4 import BeautifulSoup
# Grab table links using url
url = "https://www.epa.gov/automotive-trends/download-automotive-trends-report#Full Report"
html = urlopen(url)
soup = BeautifulSoup(html,'html.parser')
links = []
for link in soup.findAll('a'):
links.append(link.get('href'))
auto_rep = [x for x in links if 'report-tables.xlsx' in x][0]
append 循环按预期工作,生成链接列表。但是,auto_rep 赋值会引发错误:
Traceback (most recent call last):
File "<ipython-input-3-77ab86ded43b>",line 19,in <module>
auto_rep = [x for x in links if 'report-tables.xlsx' in x][0]
File "<ipython-input-3-77ab86ded43b>",in <listcomp>
auto_rep = [x for x in links if 'report-tables.xlsx' in x][0]
TypeError: argument of type 'nonetype' is not iterable
我已经使用这种精确的列表理解格式在其他上下文中做同样的事情,所以我不确定这里的问题是什么。
解决方法
您的 links
列表中的某些链接是 None,在列表理解期间您要检查是否 'report-tables.xlsx' in x
,因为 x 可以是 None,in
检查会引发错误。>
解决方案是只将链接添加到链接列表中,如果它不是 None,或者,您可以使用这个 [x for x in links if x is not None and 'report-tables.xlsx' in x]
确保链接中没有发布 None 值。在 Python >= 3.8 中执行此操作的一种简单方法是使用赋值表达式:
links = []
for link in soup.findAll('a'):
if hrefs := link.get('href'):
links.append(hrefs)
对于以前的 python 版本,你可以这样做:
links = []
for link in soup.findAll('a'):
hrefs = link.get('href')
if hrefs:
links.append(hrefs)
,
stdin
将所有类型转换为字符串有效。
它可以迭代任何类型,因此您需要转换它们
它获取的某些链接没有 href,因此在将 href 附加到 links
之前,请先检查它是否存在。
links = []
for link in soup.findAll('a'):
if link.get('href'):
links.append(link.get('href'))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。