0
点赞
收藏
分享

微信扫一扫

使用python简单的爬取图片

互联网码农 2023-06-23 阅读 30

import re

from bs4 import  BeautifulSoup
import requests

headers={'User-agent':'Mozilla/5.0(Linux:Android 6.0;Nexus 5 Build/MRA58M)''ApplewebKit/537.36(KHTML,like Gecko)''Chrome/104.0.5112.81'}
url=''  ###填写需要爬取的图片url
res=requests.get(url,headers=headers)

soup=BeautifulSoup(res.text,'html.parser')
img_link=[]
for i in soup.find_all("img"):
     if i.get('src')!=None:
#####构建爬取图片的url地址
      Picture='https:'+i.get('src')  
      responseimge = requests.get(Picture)
      file_name = Picture.split('/')[-1]
      with open('%s' % (file_name), 'wb', ) as f:
          f.write(responseimge.content)
     else:
         pass

举报

相关推荐

0 条评论