gpt4 book ai didi

python-3.x - Beautifulsoup 过滤器 "find_all"结果,仅限于通过 Regex 的 .jpeg 文件

转载 作者:行者123 更新时间:2023-12-02 00:20:47 25 4
gpt4 key购买 nike

我想从论坛上获取一些图片。 find_all 结果给了我最想要的东西,即 jpeg 文件。然而,它也给了我一些我不想要的 gif 文件。另一个问题是 gif 文件是附件,不是有效链接,在我保存文件时会造成麻烦。

soup_imgs = soup.find(name='div', attrs={'class':'t_msgfont'}).find_all('img', alt="")
for i in soup_imgs:
src = i['src']
print(src)

我试图在我的 find_all 选择搜索中避免 gif 文件,但没用,jpeg 和 gif 文件都在同一部分。那我应该怎么做才能过滤我的结果呢?请帮帮我吧,老大。我对编码非常业余。玩Python只是我的一个爱好。

最佳答案

你可以通过正则表达式过滤它。请引用下面的例子。希望这对你有帮助。

import re
from bs4 import BeautifulSoup

data='''<html>
<body>

<h2>List of images</h2>

<div class="t_msgfont">
<img src="img_chania.jpeg" alt="" width="460" height="345">
<img src="wrongname.gif" alt="">
<img src="img_girl.jpeg" alt="" width="500" height="600">
</div>
</body>
</html>'''

soup=BeautifulSoup(data, "html.parser")
soup_imgs = soup.find('div', attrs={'class':'t_msgfont'}).find_all('img', alt="" ,src=re.compile(".jpeg"))
for i in soup_imgs:
src = i['src']
print(src)

关于python-3.x - Beautifulsoup 过滤器 "find_all"结果,仅限于通过 Regex 的 .jpeg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55447885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com