1 回答

TA贡献1841条经验 获得超3个赞
如果img标签具有特定的样式值,您可以使用您尝试的内容,只需删除多余的空格:
from bs4 import BeautifulSoup
html='''
<a href='link'>
<img src='address' style='max-width:222px;max-height:222px' title='owntitle'>
</a>
<a href='link'>
<img src='address1' style='max-width:222px;max-height:222px' title='owntitle1'>
</a>
<a href='link'>
<img src='address2' style='max-width:222px;max-height:222px' title='owntitle2'>
</a>
'''
srcs=[]
titles=[]
soup=BeautifulSoup(html,'html.parser')
for img in soup.select('img["style=max-width:222px;max-height:222px"]'):
srcs.append(img['src'])
titles.append(img['title'])
print(srcs)
print(titles)
否则,您可以从a标签开始,然后img像这样:
for a in soup.select('a'):
srcs.append(a.select_one('img')['src'])
titles.append(a.select_one('img')['title'])
print(srcs)
print(titles)
添加回答
举报