利用Python和goagent代理爬取1024帖子所有图片
2015-08-16 22:02
681 查看
#1.0版本功能 保存单个帖子全部图片 #作者:Rocycle
<span style="font-family: Arial, Helvetica, sans-serif;">#E-mail:cycle1337@gmail.com</span>
import urllib.request import re import os def download(html_url): #打开页面返回源代码 img_html = urllib.request.urlopen(html_url).read().decode('gb2312','ignore') #创建文件夹 folder_name = re.findall('<h4>(.+)</h4>',img_html)[0].replace('|','&').replace('/','&').replace('\\','&').replace('?','?').replace(':',':') if not os.path.exists(folder_name): os.mkdir(folder_name) print('创建文件夹:',folder_name) os.chdir(folder_name) #把页面地址信息写入txt with open('页面地址.txt','w') as j: j.write(html_url) j.close() print('已保存页面地址到文本') #获取图片url列表 img_url_list = re.findall("<input type='image' src='(http[^']+\.(?:jpg|jpeg|png|gif))",img_html) #print(img_url_list) #储存图片 img_name = 0 for each in img_url_list: img_name += 1 if not os.path.exists('%d' % img_name + '.jpg'): try: img = urllib.request.urlopen(each).read() print('正在保存第',img_name,'张图片:',each) with open('%d' % img_name + '.jpg','wb') as f: f.write(img) except: print('图片地址失效!') else: print('图片已存在') print('帖子:' + folder_name + '#图片下载完毕#' + '\n') def main(): #创建and进入子文件夹:“社会主义核心价值观” if not os.path.exists('社会主义核心价值观'): os.mkdir('社会主义核心价值观') os.chdir('社会主义核心价值观') #安装代理opener proxies = urllib.request.ProxyHandler({'http':'127.0.0.1:8087','https':'127.0.0.1:8087'}) #此处为代理地址和端口号,需要根据自己的情况自行设置 opener = urllib.request.build_opener(proxies) urllib.request.install_opener(opener) #运行函数 html_url = input('请输入帖子地址:') download(html_url) if __name__ == '__main__': main()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- 爬虫笔记
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- 零基础学python-4.1 python对象的简介和标准类型