python抓取網(wǎng)頁圖片示例(python爬蟲)
#-*- encoding: utf-8 -*-
'''
Created on 2014-4-24
@author: Leon Wong
'''
import urllib2
import urllib
import re
import time
import os
import uuid
#獲取二級頁面url
def findUrl2(html):
re1 = r'http://tuchong.com/\d+/\d+/|http://\w+(?<!photos).tuchong.com/\d+/'
url2list = re.findall(re1,html)
url2lstfltr = list(set(url2list))
url2lstfltr.sort(key=url2list.index)
#print url2lstfltr
return url2lstfltr
#獲取html文本
def getHtml(url):
html = urllib2.urlopen(url).read().decode('utf-8')#解碼為utf-8
return html
#下載圖片到本地
def download(html_page , pageNo):
#定義文件夾的名字
x = time.localtime(time.time())
foldername = str(x.__getattribute__("tm_year"))+"-"+str(x.__getattribute__("tm_mon"))+"-"+str(x.__getattribute__("tm_mday"))
re2=r'http://photos.tuchong.com/.+/f/.+\.jpg'
imglist=re.findall(re2,html_page)
print imglist
download_img=None
for imgurl in imglist:
picpath = 'D:\\TuChong\\%s\\%s' % (foldername,str(pageNo))
filename = str(uuid.uuid1())
if not os.path.exists(picpath):
os.makedirs(picpath)
target = picpath+"\\%s.jpg" % filename
print "The photos location is:"+target
download_img = urllib.urlretrieve(imgurl, target)#將圖片下載到指定路徑中
time.sleep(1)
print(imgurl)
return download_img
# def callback(blocknum, blocksize, totalsize):
# '''回調(diào)函數(shù)
# @blocknum: 已經(jīng)下載的數(shù)據(jù)塊
# @blocksize: 數(shù)據(jù)塊的大小
# @totalsize: 遠程文件的大小
# '''
# print str(blocknum),str(blocksize),str(totalsize)
# if blocknum * blocksize >= totalsize:
# print '下載完成'
def quitit():
print "Bye!"
exit(0)
if __name__ == '__main__':
print ''' *****************************************
** Welcome to Spider for TUCHONG **
** Created on 2014-4-24 **
** @author: Leon Wong **
*****************************************'''
pageNo = raw_input("Input the page number you want to scratch (1-100),please input 'quit' if you want to quit>")
while not pageNo.isdigit() or int(pageNo) > 100 :
if pageNo == 'quit':quitit()
print "Param is invalid , please try again."
pageNo = raw_input("Input the page number you want to scratch >")
#針對圖蟲人像模塊來爬取
html = getHtml("http://tuchong.com/tags/%E4%BA%BA%E5%83%8F/?page="+str(pageNo))
detllst = findUrl2(html)
for detail in detllst:
html2 = getHtml(detail)
download(html2,pageNo)
print "Finished."
相關(guān)文章
使用python的pexpect模塊,實現(xiàn)遠程免密登錄的示例
今天小編就為大家分享一篇使用python的pexpect模塊,實現(xiàn)遠程免密登錄的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
python 用opencv實現(xiàn)圖像修復(fù)和圖像金字塔
這篇文章主要介紹了python 如何用opencv實現(xiàn)圖像修復(fù)和圖像金字塔,幫助大家更好的理解和使用python處理圖片,感興趣的朋友可以了解下2020-11-11
利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))
這篇文章主要給大家介紹了關(guān)于利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2018-03-03
Python復(fù)制Excel表格中指定數(shù)據(jù)若干次的方法
本文介紹基于Python語言,讀取Excel表格文件數(shù)據(jù),并基于其中某一列數(shù)據(jù)的值,將這一數(shù)據(jù)處于指定范圍的那一行加以復(fù)制,并將所得結(jié)果保存為新的Excel表格文件的方法,需要的朋友可以參考下2024-02-02
pyecharts繪制各種數(shù)據(jù)可視化圖表案例附效果+代碼
這篇文章主要介紹了pyecharts繪制各種數(shù)據(jù)可視化圖表案例并附效果和代碼,文章圍繞主題展開詳細的內(nèi)容介紹,感興趣的小伙伴可以參考一下2022-06-06

