通过美丽汤获得第二个属性

时间:2018-08-30 13:19:39

标签: web-scraping beautifulsoup

我有这个html和一系列图像,可以使用这种结构:

export interface Collections extends Twitter.Collections, Coin.Collections {}
export type CollectionNames = keyof Collections;
export type CollectionType<K extends CollectionNames> = Collections[K];

我想获取image_link。我已经尝试过了,但是没有用。我不明白如何通过get()获取第二个属性。

<portrait a>
 <img class = "image" data = "random stuff" src = "image_link">
<portrait b>
 <img class = "image" data = "random stuff" src = "image_link">

谢谢

1 个答案:

答案 0 :(得分:0)

尝试这样:

from bs4 import BeautifulSoup

htmlcontent = """
<portrait a>
 <img class = "image" data = "random stuff" src = "image_link1">
<portrait b>
 <img class = "image" data = "random stuff" src = "image_link2">
"""
soup = BeautifulSoup(htmlcontent,"lxml")
for item in soup.find_all("img",class_="image"):
    print(item.get("src"))

输出:

image_link1
image_link2