简单的图片批量爬取

一、获取html页面内容

用requests获取,用beautifulsoup解析

import os
import requests
from fake_useragent import UserAgent
from bs4 import BeautifulSoup

ua = UserAgent(verify_ssl=False)
# print(ua.random)
user_agent=ua.random
#
url='https://www.yunbtv.com/vodtype/oumeiju.html'
headers={
    'User-Agent':user_agent
}
response=requests.get(url,headers=headers)
html_content=response.text
soup=BeautifulSoup(html_content,'lxml')

二、创建文件夹,用于存放图片

curpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
# 在当前路径外第二层文件下创建
#print(curpath)
imgs = os.path.join(curpath, 'imgs')
if not os.path.exists(imgs):  
    os.mkdir(imgs)        

三、提取图片并存入文件夹

img=soup.find_all('img',{'class':'lazy'})
#for i in img:
    #print(i['alt'])
    # print(i['data-original'])
for i in img:
    try:
        jpg_url = i['data-original']
        name = i['alt']
        r = requests.get(jpg_url)
        f = open(os.path.join(imgs, '%s.jpg'%name), 'wb')
        f.write(r.content)
        f.close()
    except:
        pass

结果:

简单的图片批量爬取

上一篇:原生js实现图片懒加载


下一篇:jquery简单的上传图片预览