【发布时间】:2020-05-10 14:35:09
【问题描述】:
我的 HTML 页面包含带有 2 个“src”属性的图像标签,我想使用 BS 提取第一个“src”而不是第二个“src”。
例如:
当我如下使用BS时:
from bs4 import BeautifulSoup
html_doc = <img class="lazy" src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=1.00xw:0.669xh;0,0.190xh&resize=980:*" src="https://www.mdf.qa/media/catalog/product/cache/1/image/800x800/9df78eab33525d08d6e5fb8d27136e95/b/l/black_2.jpg"/>
soup = BeautifulSoup(html_doc, 'html.parser')
bs_images = soup.find_all('img')
for bs_image in bs_images:
attrs = bs_image.attrs
image_path = attrs['src']
我得到的路径是第二个 src "https://www.mdf.qa/media/catalog/product/cache/1/image/800x800/9df78eab33525d08d6e5fb8d27136e95/b/l/black_2.jpg" 但我需要第一个 src - https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=1.00xw:0.669xh;0,0.190xh&resize=980:* 。
【问题讨论】:
标签: python beautifulsoup html5-img