简单python爬虫淘宝图片+界面编程+打包成exe

来源:互联网 发布:得无异乎的异 编辑:程序博客网 时间:2024/05/20 08:22

淘宝图片爬虫exe下载:http://download.csdn.net/detail/luoshixian099/9788470

源码解析

新建GetImg.py文件:

第一步:编写爬虫代码

[python] view plain copy
  1. import re  
  2. import urllib.request  
  3. def getHtml(url='') :  
  4.     page = urllib.request.urlopen(url)  
  5.     html = page.read().decode('GBK'#解析网页源代码  
  6.     return html  
  7.   
  8.   
  9. #print(html)  
  10.   
  11. def getImg(html):  
  12.     #https://cbu01.alicdn.com/img/ibank/2016/693/443/3382344396_1109687136.jpg  
  13.     #reg = r'https://.*\.?alicdn\.com[^"]+\.jpg'  
  14.     reg = r'//[\w]*\.?alicdn\.com[^"\']+\.jpg'#正则表达式,搜索匹配字段  
  15.     imgre = re.compile(reg)  
  16.     imglist = re.findall(imgre,html)  
  17.     print(len(imglist))  
  18.     x = 1  
  19.     for imgurl in imglist:  
  20.         print(imgurl)  
  21.         imgurl = 'https:' + imgurl  
  22.         urllib.request.urlretrieve(imgurl,'save\\%s.jpg' %x) #按顺序保存在save文件夹  
  23.         x+=1  
  24.   
  25. def DownLoadImg(url):  
  26.     html = getHtml(url)  
  27.     getImg(html)  
通过调用DownLoadImg()函数,即可下载所有.jpg格式的图片
例如:DownLoadImg('https://detail.1688.com/offer/523861658213.htmlspm=b26110380.8015204.xshy005.37.q3aAzE')


第二步:编写界面

[python] view plain copy
  1. class Application(Frame):  
  2.          def __init__(self, master=None):  
  3.                   Frame.__init__(self, master)  
  4.                   self.pack(fill=X,padx=100)  
  5.                   self.createWidgets()  
  6.          def createWidgets(self):  
  7.                   self.nameInput = Entry(self)  
  8.                   self.nameInput.pack(fill=X,pady=10)  
  9.                   self.nameInput.pack(fill=X,padx=5)  
  10.                   self.alertButton = Button(self, text=(' Please Input Website '), command=self.FUN)  
  11.                   self.alertButton.pack(fill=X,padx=30)  
  12.                   self.alertButton.pack(fill=X,pady=10)  
  13.          def FUN(self):  
  14.                   name = self.nameInput.get() or 'blank'  
  15.                   if name == 'blank':  
  16.                            messagebox.showinfo('Message''Website is invalid!')  
  17.                   else:  
  18.                            DownLoadImg(name)  
  19.                            messagebox.showinfo('Message''Success: see \"save\" folder')  
  20. app = Application()  
  21. app.master.title('DownLoad IMG From Web : [LSX]')  
  22. app.mainloop()  


第三步:打包成exe

下载py2exe,选择对应python版本,仅支持python3.3以上版本,3.5版本用起来有点问题,所以本文采用python3.4。

两种方法把python程序打包成exe

1.在cmd命令窗口下,进入GetImg.py文件夹

运行

[cpp] view plain copy
  1. build_exe GetImg.py  

生成的exe文件被保存在dist文件中。

2.新建一个convert2exe.py

[python] view plain copy
  1. from distutils.core import setup    
  2. import py2exe    
  3. setup(windows=[{"script""GetImg.py"}])   
运行

[python] view plain copy
  1. python convert2exe.py py2exe  

总结:

1.没有搞清楚解析html源码格式,'GBK','utf-8',对于'utf-8'格式的源码解析总是出现错误

2.对于py2exe软件,不能转换中文字符。

3.只能解析到部分图片,可能是正则表达式有问题。


参考:

http://blog.csdn.net/wangxiaobupt/article/details/37936849

http://www.cnblogs.com/fnng/p/3576154.html

http://blog.csdn.net/liuxu0703/article/details/54428405

原创粉丝点击