1. 程式人生 > >python爬取百度圖片---釋出exe小計 編碼是個大坑

python爬取百度圖片---釋出exe小計 編碼是個大坑

#*--coding:utf-8--*
import requests
import sitecustomize
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
type=sys.getfilesystemencoding()

x=0
def getManyPages(keyword,pages,pathdir):
    params=[]
    for i in range(0,30*pages,30):
        params.append({
                      'tn': 'resultjson_com'
, 'ipn': 'rj', 'ct': 201326592, 'is': '', 'fp': 'result', 'queryWord': keyword, 'cl': 2, 'lm': -1, 'ie': 'utf-8', 'oe'
: 'utf-8', 'adpicid': '', 'st': -1, 'z': '', 'ic': 0, 'word': keyword, 's': '', 'se': '', 'tab': '', 'width'
: '', 'height': '', 'face': 0, 'istype': 2, 'qc': '', 'nc': 1, 'fr': '', 'pn': i, 'rn': 30,#步長 'gsm': '1e', '1517211097507': '' }) url = 'https://image.baidu.com/search/acjson' urls = [] for i in params: l=[] try: l=requests.get(url, params=i).json().get('data') except: continue finally: if len(l) is not 0: urls.append(l) try: getImg(urls,pathdir) # 引數2:指定儲存的路徑 urls.pop() except: pass return urls def getImg(dataList, localPath): #print('開始下載'.decode('utf-8').encode(type)) print('開始下載') global x; if not os.path.exists(localPath): # 新建資料夾 os.mkdir(localPath) for list in dataList: for i in list: if i.get('thumbURL') != None: #print('正在下載:%s'.decode('utf-8').encode(type) % i.get('thumbURL').decode('utf-8').encode(type)) print('正在下載:%s' % i.get('thumbURL')) ir = requests.get(i.get('thumbURL')) open((localPath + '%d.jpg' % x), 'wb').write(ir.content) x += 1 else: #print('圖片連結不存在'.decode('utf-8').encode(type)) print('圖片連結不存在') if __name__ == '__main__': li=[] pages=[] #exe: # pathdir=raw_input('請輸入儲存路徑(英文 半形):'.decode('utf-8').encode(type)) # count=input("請輸入查詢的類別數量:".decode('utf-8').encode(type)) # for xxx in range(0,count): # li.append(raw_input('請輸入要查詢的關鍵字:'.decode('utf-8').encode(type))) # pages.append(input('請輸入下載的總頁數:'.decode('utf-8').encode(type))) # for yyy in range(0,count): # getManyPages(li[yyy].decode(type).encode('utf-8'),int(pages[yyy]),(pathdir+str(yyy)+'/')) # 引數1:關鍵字,引數2:要下載的頁數 #pycharm: pathdir=raw_input('請輸入儲存路徑(英文 半形):') count=input("請輸入查詢的類別數量:") for xxx in range(0,count): li.append(raw_input('請輸入要查詢的關鍵字:')) pages.append(input('請輸入下載的總頁數:')) for yyy in range(0,count): getManyPages(li[yyy],int(pages[yyy]),(pathdir+str(yyy)+'/')) # 引數1:關鍵字,引數2:要下載的頁數
現附上原始碼!

接著上一篇文章那個爬蟲!

公司來了一個同事,和我做著差不多的工作,所以就想把那個py打包成為exe!!坑之路從此開始!

由於windows應用的編碼是MBCS!!超級大坑  說白了其實就是編碼轉換一下 但是過程真的很心酸!