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: 遠(yuǎn)程文件的大小
# '''
# 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."
- 零基礎(chǔ)寫python爬蟲之爬蟲編寫全記錄
- Python爬蟲模擬登錄帶驗(yàn)證碼網(wǎng)站
- 使用Python編寫簡單網(wǎng)絡(luò)爬蟲抓取視頻下載資源
- 三個python爬蟲項(xiàng)目實(shí)例代碼
- 33個Python爬蟲項(xiàng)目實(shí)戰(zhàn)(推薦)
- python制作爬蟲并將抓取結(jié)果保存到excel中
- 10個python爬蟲入門基礎(chǔ)代碼實(shí)例 + 1個簡單的python爬蟲完整實(shí)例
- 如何用六步教會你使用python爬蟲爬取數(shù)據(jù)
- 最新Python爬蟲從入門到精通:靜態(tài)爬取+JS逆向+反爬繞過實(shí)戰(zhàn)教程
相關(guān)文章
開源軟件包和環(huán)境管理系統(tǒng)Anaconda的安裝使用
Anaconda是一個用于科學(xué)計(jì)算的Python發(fā)行版,支持 Linux, Mac, Windows系統(tǒng),提供了包管理與環(huán)境管理的功能,可以很方便地解決多版本python并存、切換以及各種第三方包安裝問題。2017-09-09
使用tensorflow實(shí)現(xiàn)AlexNet
這篇文章主要為大家詳細(xì)介紹了使用tensorflow實(shí)現(xiàn)AlexNet,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Python用HBuilder創(chuàng)建交流社區(qū)APP
這篇文章主要講解Python使用HBuilder創(chuàng)建交流社區(qū)APP,使用HBuilder做一個簡單的社區(qū)瀏覽界面,下面文章附有詳細(xì)的代碼,需要的朋友可以參考一下2021-11-11
python實(shí)現(xiàn)上傳文件到linux指定目錄的方法
這篇文章主要介紹了python實(shí)現(xiàn)上傳文件到linux指定目錄的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
基于Flask實(shí)現(xiàn)文件上傳七牛云中并下載
文件上傳是Web應(yīng)用中常見的功能之一,而七牛云則提供了強(qiáng)大的云存儲服務(wù),本文我們將學(xué)習(xí)如何在Flask應(yīng)用中實(shí)現(xiàn)文件上傳,并將上傳的文件保存到七牛云,感興趣的可以學(xué)習(xí)一下2023-10-10
python中根據(jù)字符串調(diào)用函數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵ython中根據(jù)字符串調(diào)用函數(shù)的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧2016-06-06
Matplotlib自定義坐標(biāo)軸刻度的實(shí)現(xiàn)示例
這篇文章主要介紹了Matplotlib自定義坐標(biāo)軸刻度的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

