返回顶部

python批量下载配图网的图片

import requests
import re
from bs4 import BeautifulSoup
import random
# 生成随机数
# for i in range(5):
#     print(random.randrange(100000000))
headers = {
    'content-type': 'application/json;charset=UTF-8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}


# 构建获取图片函数
def get_url(get_url_list):
    response = requests.get(get_url_list, headers=headers)
    response.encoding = 'utf-8'
    html = response.text
    soup = BeautifulSoup(html, 'html.parser')
    img = soup.select('img')
    re_img_url_list = re.findall('https.*?.jpg', str(img))
    # print(re_img_list)
    return re_img_url_list


# 构建下载函数
def download_img(url, file_name):
    res = requests.get(url)
    img = open('file/' + file_name, 'wb')
    img.write(res.content)
    img.close()


# """
for j in range(10):
    urls = get_url('https://www.keaitupian.cn/meinv/list_4_' + str(j + 1) + '.html')
    print('正在下载第' + str(j + 1) + '页图片...')
    for i in urls:
        # 图片名称
        name = i.split('/')[-1]
        download_img(i, name)
    print('' + str(j + 1) + '页图片下载完成')
print('下载完成!')
# """

下载图片:

 

posted @ 2022-10-14 14:57  gz_zyg  阅读(19)  评论(0编辑  收藏  举报