Python批量爬取唯美类图片

Posted on 2019-05-10 17:54  努力成长静待花开  阅读(295)  评论(0编辑  收藏  举报

效果图:

  

实现代码:

# -*- coding: UTF-8 -*-
import requests
import re

urls = ['http://www.ik123.com/q/tuku/weimei/list_11_{}.html'.format(str(i)) for i in range(1, 201)]
headers={
    'Referer': 'http://www.ik123.com/q/tuku/weimei/list_11_1.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
}

def list_info(url):
    res = requests.get(url)
    res.encoding = 'GB2312'
    srcs = re.findall("<img style='width: 190px; height: 130px' src='(.*?)' alt='.*?'/>", res.text, re.S)
    titles = re.findall("<img style='width: 190px; height: 130px' src='.*?' alt='[<b>]?(.*?)[</b>]?'/>", res.text, re.S)

    for src, title in zip(srcs, titles):
        try:
            info = {
                'src': src,
                'title': title
            }
            print(info)
            pic=requests.get(info['src'],headers=headers)
            f=open('pic\\'+str(info['title'])+'.jpg', 'wb')
            f.write(pic.content)
            f.close()
        except IndexError:
            pass

if __name__ == '__main__':
    for url in urls:
        list_info(url)