1. 程式人生 > >Python 爬取百度圖片的高清原圖

Python 爬取百度圖片的高清原圖

# coding=utf-8
"""
爬取百度圖片的高清原圖
Author          : MirrorMan
Created         : 2017-11-10
"""
import re
import urllib
import os
import requests


def get_onepage_urls(onepageurl):
    if not onepageurl:
        print('執行結束')
        return [], ''
    try:
        html = requests.get(onepageurl).text
    except Exception as e:
        print(e)
        pic_urls = []
        fanye_url = ''
        return pic_urls, fanye_url
    pic_urls = re.findall('"objURL":"(.*?)",', html, re.S)
    html = requests.get(onepageurl)
    html.encoding = 'utf-8'
    content = html.text
    fanye_urls = re.findall(re.compile(r'<a href="(.*)" class="n">下一頁</a>'), content, flags=0)
    fanye_url = 'http://image.baidu.com' + fanye_urls[0] if fanye_urls else ''
    return pic_urls, fanye_url

def down_pic(pic_urls, localPath):
    if not os.path.exists(localPath):  # 新建資料夾
        os.mkdir(localPath)
    """給出圖片連結列表, 下載圖片"""
    for i, pic_url in enumerate(pic_urls):
        try:
            pic = requests.get(pic_url, timeout=15)
            string = str(i + 1) + '.jpg'
            with open(localPath + '%d.jpg' % i, 'wb')as f:
                f.write(pic.content)
                print('成功下載第%s張圖片: %s' % (str(i + 1), str(pic_url)))
        except Exception as e:
            print('下載第%s張圖片時失敗: %s' % (str(i + 1), str(pic_url)))
            print(e)
            continue

if __name__ == '__main__':
    keyword = '鳴人'  # 關鍵詞, 改為你想輸入的詞即可, 相當於在百度圖片裡搜尋一樣
    url_init_first = r'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1497491098685_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1497491098685%5E00_1519X735&word='
    url_init = url_init_first + urllib.parse.quote(keyword, safe='/')
    all_pic_urls = []
    onepage_urls, fanye_url = get_onepage_urls(url_init)
    all_pic_urls.extend(onepage_urls)

    fanye_count = 1  # 圖片所在頁數,下載完後調整這裡就行
    while 1:
        onepage_urls, fanye_url = get_onepage_urls(fanye_url)
        fanye_count += 1
        print('第%s頁' % fanye_count)
        if fanye_url == '' and onepage_urls == []:
            break
        all_pic_urls.extend(onepage_urls)

    down_pic(list(set(all_pic_urls)), r'C:\Users\41174\AppData\Local\Temp\change.py\shrinkImage\\')  # 儲存位置也可以修改

參考:https://blog.csdn.net/xiligey1/article/details/73321152