图片提取代码错误,应该如何改写,将图片批量提取保存

图片提取代码错误,需要把图片批量提取并保存,该如何改写?

>>> import requests
>>> from bs4 import BeautifulSoup
>>> from PIL import Image
>>> r=requests.get("https://www.bilibili.com/read/cv16332851?from=search&spm_id_from=333.337.0.0")
>>> soup=BeautifulSoup(r.text)
>>> a=soup.find_all('img')
>>> soup.find_all('img')
>>> import re
>>> soup.find_all('img',{'src':re.compile('jpg')})

你题目的解答代码如下:

import requests
from bs4 import BeautifulSoup
r=requests.get("https://www.bilibili.com/read/cv16332851?from=search&spm_id_from=333.337.0.0%22")
soup=BeautifulSoup(r.text)
path = r'e:/xxx/'
a=soup.find_all('img')
for img in a:
    url = "https:"+img['data-src']
    print(url)
    fn = url.split('/')[-1]
    with open(path+fn,'wb') as f:
        r=requests.get(url)
        f.write(r.content)

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632