Python如果String包含在href中

问题描述 投票:1回答:2

这是我的python代码。

r = requests.get("myurl")
data = r.text
soup = BeautifulSoup(data, "lxml")
texttmp = ""
for link in soup.find_all('a'):
    image = link.get("href")
    if ".jpg" in image:
        print(image)

当我尝试运行此代码时,我遇到了错误。我怎样才能解决这个问题?

TypeError                                 Traceback (most recent call last)
<ipython-input-35-618698d3a2d7> in <module>()
     11 for link in soup.find_all('a'):
     12     image = link.get("href")
---> 13     if ".jpg" in image:
     14         print(image)
     15 

TypeError: argument of type 'NoneType' is not iterable
python web-scraping
2个回答
3
投票

它告诉你的是,没有找到href字符串。因此,在查看None是否在图像标记中之前,您需要检查".jpg"

 if image and ".jpg" in image:

然而,这不是唯一发生的事情。你也试图从找到的链接节点get。你应该检查a是否具有href的属性(有些没有,请参阅Bootstrap示例!):

 for link in soup.find_all('a'):
   if link.has_attr('href'):
     #rest of code

看看this SO post和其他人喜欢它(我也应该先用google搜索。)


1
投票

除了代表其他资源的链接外,html锚标签<a ...>还可以作为文档中某个位置的命名标记,即所谓的名称标签<a name=whatever>,允许标记的位置成为使用片段的链接的目标。网址http://example.com/#whatever

这可能是您遇到的问题,因为名称标签不会有href来指示它们指向的资源。

您需要检查href是否返回None并跳过返回的标记(如果不是)。

祝好运。

© www.soinside.com 2019 - 2024. All rights reserved.