python 图片

import requests
from lxml import etree
from hashlib import md5
import re
import os
import redis

r = redis.StrictRedis(host='localhost', port=6379, db=2, decode_responses=True)
headers = {
    'Cookie': 'Hm_lvt_c8263f264e5db13b29b03baeb1840f60=1632291839,1632373348; Hm_lpvt_c8263f264e5db13b29b03baeb1840f60=1632373697',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
}
os.chdir(r"E:\home\webContainer\ntmssResource")
urlRes = requests.get("https://www.3gbizhi.com/meinv/", headers=headers)
html = etree.HTML(urlRes.text)
href_list = html.xpath('//div[@class="cl r"]//ul[@class="cl"]/li/a/@href')
for href in href_list:
    parttern_href = re.compile(r'https://www.3gbizhi.com/meinv/(.*?).html', flags=re.DOTALL)
    linkShort = re.search(parttern_href, href)
    short = linkShort.group(1)
    if not os.path.exists(short):
        os.makedirs(short)
    for i in range(2, 3):
        url = f'https://www.3gbizhi.com/meinv/' + short + '_' + str(i) + '.html'
        response = requests.get(url, headers=headers)
        html = etree.HTML(response.text)
        href_list = html.xpath('//div[@class="contlistw mtw"]//ul[@class="cl"]/li/a/@href')
        title_list = html.xpath('//div[@class="contlistw mtw"]//ul[@class="cl"]/li/a/@title')
        for href, title in zip(href_list, title_list):
            res = requests.get(href, headers=headers)
            html_data = etree.HTML(res.text)
            img_url_list = html_data.xpath('//div[@class="picimglist pos"]/ul/li/a/img/@src')
            for img_url in img_url_list:
                img_url = ''.join(img_url.split('thumb_200_0_'))
                result = requests.get(img_url, headers=headers).content
                file_path = '{}/{}.{}'.format(short, md5(result).hexdigest(), 'jpg')
                path = file_path
                url_local = "http://192.168.31.155:8889/ntmssResource/" + path
                r.sadd("img_list", url_local)
                print(f'正在下载  {title} {url_local}!!!!')
                with open(file_path, 'wb')as f:
                    f.write(result)

 

posted @ 2021-11-20 14:55  Bonnie_ξ  阅读(63)  评论(0编辑  收藏  举报