是否只打印扩展名为.jpg结尾的链接



我有多个以.png和.jpg扩展名结尾的链接。

我想打印并保存在仅以.jpg扩展名结尾的txt文件链接中。

我尝试了这个代码,但只保存了第一个结果:

for item in soup.find_all('img'):
hotel_image = (item['src'])
print(hotel_image)
file1 = open("myfile.txt", "w")
file1.writelines(hotel_image)
file1.close()  # to change file access modes

链接示例:

https://cf.bstatic.com/images/hotel/max300/288.jpg

https://cf.bstatic.com/static/img/flags/12/eg.png

https://cf.bstatic.com/images/hotel/max300.jpg

https://cf.bstatic.com/static/img/review.png

我想要什么:

https://cf.bstatic.com/images/hotel/max300.jpg

https://cf.bstatic.com/images/hotel/max300/288.jpg

有什么需要帮忙的吗?

确保hotel_image是字符串,否则将其转换为字符串并使用endswith函数。

试试这个:

with open("myfile.txt", "w") as fp:
for item in soup.find_all('img'):
hotel_image = (item['src'])
if hotel_image.endswith('.jpg'):
fp.writelines(hotel_image)

尝试在"find_all";方法,如:

soup.find_all('img', alt="" ,src=re.compile(".jpg"))

最新更新