Python練手項目0013

本項目採用的是https://github.com/Yixiaohan/show-me-the-code中所提供的練習項目,所有代碼均爲原創,轉載請註明,謝謝。

第 0013 題: 用 Python 寫一個爬圖片的程序,爬 這個鏈接裏的日本妹子圖片 :-)

其代碼如下

"""

Created on Tue Feb  7 15:32:39 2017


@author: sky
"""

import urllib
import re
import os


url = "http://tieba.baidu.com/p/2166231880"
def catch_pic(url,name):
    content = urllib.request.urlopen(url).read()
    reg = '<img pic_type="0" class="BDE_Image" src="(.*?)"'  
    r = re.compile(reg)
    picture_list = r.findall(content.decode('utf-8'))


    os.mkdir(name)
    os.chdir(os.path.join(os.getcwd(),name))
    for i in range(len(picture_list)):
        picture_name = str(i) + '.jpg'
        try:
            urllib.request.urlretrieve(picture_list[i],picture_name)
            print("success to download" + picture_list[i])
        except:
            print("fail to download" + picture_list[i])
        

if __name__ == '__main__':

    picture_list = catch_pic(url,'c')


詳細代碼和結果,可以參考https://github.com/g8015108/exercise-for-python

通過修改reg的正則項可以提取不同的網頁圖片


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章