python3编写爬虫从百度图库中爬取图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27668313/article/details/80567571
# -*- coding:utf-8 -*-
# 从百度图库中下载图片
import urllib
import requests
import re

def getHtml(url):
    html = requests.get(url).text
    urls = re.findall('"objURL":"(.*?)"',html, re.S)
    return urls

def downloadImg(urls):
    x = 1
    for url in urls:
        img = requests.get(url, allow_redirects=False)
        with open(str(x)+'.jpg', 'wb') as f:
            f.write(img.content)
            print('正在下载第%s张图片' % x)
        x+=1
        if x>20:                     #设置爬取图片的张数
            break
    return None

# 李沁的图片
html = getHtml('http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=李沁')
downloadImg(html)

猜你喜欢

转载自blog.csdn.net/qq_27668313/article/details/80567571