Python实现从订阅源下载图片的方法
作者:saintatgod 时间:2021-08-14 19:19:37
本文实例讲述了Python实现从订阅源下载图片的方法。分享给大家供大家参考。具体如下:
这段代码是基于python 3.4实现的,和python2.X 比起来有了好多差别啊。
这是一个练习,数据源来自网易订阅。代码如下:
__author__ = 'Saint'
import os
import urllib.request
import json
from html.parser import HTMLParser
# 从获取的网页内容筛选图片的内容
class MyHtmlParser(HTMLParser):
links = []
def handle_starttag(self, tag, attrs):
if tag == "img":
if len(attrs) == 0:
pass
else:
for name, value in attrs:
if name == "src":
self.links.append(value)
class Down(object):
# 总的目录
img_path = "E:/saint"
# 下载目录
dir = ''
# 采集源地址
collect_links = ["http://dy.163.com/v2/media/articlelist/T1374483113516-1", "http://dy.163.com/v2/media/articlelist/T1420776257254-1", "http://dy.163.com/v2/media/articlelist/T1376641060407-1"]
img_links = "http://dy.163.com/v2/article"
def handleCollect(self):
for collect_link in self.collect_links:
notice = "开始从[" + collect_link + "]采集图片"
print(notice)
# 建立下载的目录
dir_name = collect_link.split("/")[-1]
self.isDirExists(dir_name)
dict = self.getListFromSubscribe(collect_link)
if dict == False:
print("数据采集失败,是否继续(y/n)")
op = input();
if op == "y":
os.system("cls")
pass
elif op == "n":
print("停止采集")
break
else:
os.system("cls")
print("非法输入")
break
else:
for page in dict:
page_uri = self.img_links + "/" + page["tid"] + "/" + page["docid"]
self.getImgFromUri(page_uri)
print("是否继续(y/n)")
new_op = input();
if new_op == "n":
os.system("cls")
print("采集完毕")
break
print("OK")
# 从订阅源获取目录
def getListFromSubscribe(self, uri):
res = urllib.request.urlopen(uri)
if res.code < 200 or res.code > 300:
os.system("clear")
return False
else:
result = res.read().decode("gbk") # 3.4版本的read()返回的是byte类型,需要decode()处理,选项是网页编码
dict = json.loads(result)
if dict['code'] != 1:
print(dict['msg'])
return False
else:
return dict['data']
# 获取本期订阅的网页,并从网页中提取出来需要的图片
def getImgFromUri(self, uri):
html_code = urllib.request.urlopen(uri).read().decode("gbk")
hp = MyHtmlParser()
hp.feed(html_code)
hp.close()
for link in hp.links: # hp.links 是图片的下载地址的列表
self.writeToDisk(link)
# 检查文件目录是否存在,如果不存在,则创建目录
def isDirExists(self, dir_name):
self.dir = self.img_path + dir_name
isExists = os.path.exists(self.dir)
if not isExists:
os.makedirs(self.dir)
return True
else:
return True
# 下载文件,并且写入磁盘
def writeToDisk(self, url):
os.chdir(self.dir)
file = urllib.request.urlopen(url).read()
file_name = url.split("/")[-1]
open(file_name, "wb").write(file)
return True
if __name__ == "__main__":
down = Down()
down.handleCollect()
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,下载,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Dreamweaver MX 2004表格设计
2008-02-03 11:36:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311363330s.jpg)
让IE8支持eWebEditor在线编辑器
2010-02-28 10:36:00
使用PHP获取当前url路径的函数以及服务器变量
2023-10-31 02:31:34
详解Bagging算法的原理及Python实现
2021-06-10 00:20:41
![](https://img.aspxhome.com/file/2023/9/77909_0s.jpg)
PHP htmlspecialchars()函数用法与实例讲解
2023-06-02 16:48:38
![](https://img.aspxhome.com/file/2023/2/55382_0s.png)
页面重构中的组件制作要点
2009-10-25 13:06:00
![](https://img.aspxhome.com/file/UploadPic/200910/25/medium-14s.png)
浅谈python中真正关闭socket的方法
2023-11-02 15:41:56
Python tkinter布局与按钮间距设置方式
2023-09-28 23:43:27
《写给大家看的设计书》阅读笔记之对齐原则
2009-07-09 16:32:00
![](https://img.aspxhome.com/file/UploadPic/20097/9/dq-25s.jpg)
CSS缩写可以有效的缩小CSS文件的体积
2009-01-13 18:06:00
解决MySQL数据库中与优化器有关的问题
2008-12-17 16:18:00
Oracle密码文件的使用和维护第1/3页
2010-07-30 12:43:00
python机器学习理论与实战(五)支持向量机
2021-11-27 11:36:30
![](https://img.aspxhome.com/file/2023/2/75942_0s.jpg)
Python 程序报错崩溃后如何倒回到崩溃的位置(推荐)
2021-01-08 16:14:34
![](https://img.aspxhome.com/file/2023/1/83991_0s.jpg)
深入了解SQL Server 2008 商业智能平台
2009-01-15 13:03:00
网页绿色系配色应用实例
2008-08-26 11:51:00
![](https://img.aspxhome.com/file/UploadPic/20088/26/2008826115322660s.gif)
JavaScript 编程引入命名空间
2007-10-11 19:00:00
python实现微信自动回复机器人功能
2023-12-30 01:01:40
![](https://img.aspxhome.com/file/2023/8/64718_0s.jpg)
Python模块zipfile原理及使用方法详解
2022-03-24 10:33:37
ASP读取Exif信息无组件实现过程
2009-02-09 12:52:00