gpt4 book ai didi

python - 如何从 BeautifulSoup 下载图片?

转载 作者:太空狗 更新时间:2023-10-30 00:54:33 27 4
gpt4 key购买 nike

图片http://i.imgur.com/OigSBjF.png

导入请求
从 bs4 导入 BeautifulSoup



r = requests.get("xxxxxxxxx")
soup = BeautifulSoup(r.content)

for link in links:
if "http" in link.get('src'):
print link.get('src')

我得到打印的 URL,但不知道如何使用它。

最佳答案

您需要下载并写入磁盘:

import requests
from os.path import basename

r = requests.get("xxx")
soup = BeautifulSoup(r.content)

for link in links:
if "http" in link.get('src'):
lnk = link.get('src')
with open(basename(lnk), "wb") as f:
f.write(requests.get(lnk).content)

您还可以使用选择 来过滤您的标签,以仅获取带有 http 链接的标签:

for link in soup.select("img[src^=http]"):
lnk = link["src"]
with open(basename(lnk)," wb") as f:
f.write(requests.get(lnk).content)

关于python - 如何从 BeautifulSoup 下载图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37158246/

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