编程爱好者之家

python删除网页中含有lazy.png字符串的img标签并返回删除后的字符串

2024-07-10 10:57:41 12

编程爱好者之家为大家带来python删除网页中含有lazy.png字符串的img标签并返回删除后的字符串


原始网页代码部分例子如下

<div>
<noscript><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3072"  src="http://www.test.com/test.jpg" alt="20240709172357407" width="720" height="537" /></noscript>
<img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3072 j-lazy"  src="http://www.test.com/images/lazy.png" data-original="http://www.test.com/test.jpg" alt="20240709172357407" width="720" height="537" />

<img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3072 j-lazy"  src="http://www.test.com/images/lazy.png" data-original="http://www.test.com/test2.jpg" alt="20240709172357407" width="720" height="537" />
</div>


实现代码如下:

from bs4 import BeautifulSoup
resp = requests.get(url)  #url为网页地址
soup = BeautifulSoup(resp.content, "html.parser")
imgs = soup.find_all("img", src=re.compile(r".*lazy\.png"))
for img in imgs:
    img.decompose()


同类文章