import bs4
html = """<div class="someClass">
<a href="href">
<img alt="some" src="some"/>
</a>
</div>"""
soup = bs4.BeautifulSoup(html, "html.parser")
# this will return src attrib from img tag that is inside 'a' tag
soup.a.img['src']
>>> 'some'
# if you have more then one 'a' tag
for a in soup.find_all('a'):
if a.img:
print(a.img['src'])
>>> 'some'
链接没有属性
src
必须以实际的img
标记为目标。可以使用
BeautifulSoup
提取html img
标记的src
属性。在我的示例中,htmlText
包含img
标记本身,但这也可以与urllib2
一起用于URL。对于URL
对于带有img标签的文本
相关问题 更多 >
编程相关推荐