Python批量爬取唯美类图片

效果图:

  

实现代码:

# -*- 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)

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10845830.html