Python 爬取百度图片的高清原图
生活随笔
收集整理的這篇文章主要介紹了
Python 爬取百度图片的高清原图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# coding=utf-8
"""
爬取百度圖片的高清原圖
Author : MirrorMan
Created : 2017-11-10
"""
import re
import urllib
import os
import requestsdef get_onepage_urls(onepageurl):if not onepageurl:print('執行結束')return [], ''try:html = requests.get(onepageurl).textexcept Exception as e:print(e)pic_urls = []fanye_url = ''return pic_urls, fanye_urlpic_urls = re.findall('"objURL":"(.*?)",', html, re.S)html = requests.get(onepageurl)html.encoding = 'utf-8'content = html.textfanye_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_urldef 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)continueif __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 += 1print('第%s頁' % fanye_count)if fanye_url == '' and onepage_urls == []:breakall_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
總結
以上是生活随笔為你收集整理的Python 爬取百度图片的高清原图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【手机电子杂志制作】名编辑电子杂志大师教
- 下一篇: python高级函数,将函数作为变量、返