Python實現從訂閱源下載圖片的方法

來源:互聯網
上載者:User
本文執行個體講述了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程式設計有所協助。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.