亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python抓取網(wǎng)頁(yè)圖片示例(python爬蟲(chóng))

 更新時(shí)間:2014年04月27日 10:45:33   作者:  
這篇文章主要介紹了python抓取網(wǎng)頁(yè)圖片示例(python爬蟲(chóng)),需要的朋友可以參考下

復(fù)制代碼 代碼如下:

#-*- encoding: utf-8 -*-
'''
Created on 2014-4-24

@author: Leon Wong
'''

import urllib2
import urllib
import re
import time
import os
import uuid

#獲取二級(jí)頁(yè)面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 >")

    #針對(duì)圖蟲(chóng)人像模塊來(lái)爬取
    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)文章

  • Selenium結(jié)合BeautifulSoup4編寫(xiě)簡(jiǎn)單的python爬蟲(chóng)

    Selenium結(jié)合BeautifulSoup4編寫(xiě)簡(jiǎn)單的python爬蟲(chóng)

    這篇文章主要介紹了Selenium結(jié)合BeautifulSoup4編寫(xiě)簡(jiǎn)單的python爬蟲(chóng),幫助大家更好的理解和學(xué)習(xí)python 爬蟲(chóng)的相關(guān)知識(shí),感興趣的朋友可以了解下
    2020-11-11
  • Python讀取和寫(xiě)入txt、Excel文件和JSON文件的方法

    Python讀取和寫(xiě)入txt、Excel文件和JSON文件的方法

    Python 提供了多種方法來(lái)讀取和寫(xiě)入不同類型的文件,包括文本文件(txt)、Excel 文件和 JSON 文件,本文給大家介紹了一些常用的方法和示例代碼,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • python中flatten()參數(shù)示例詳解

    python中flatten()參數(shù)示例詳解

    flatten是numpy.ndarray.flatten的一個(gè)函數(shù),即返回一個(gè)一維數(shù)組,這篇文章主要給大家介紹了關(guān)于python中flatten()參數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Python機(jī)器學(xué)習(xí)庫(kù)sklearn(scikit-learn)的基礎(chǔ)知識(shí)和高級(jí)用法

    Python機(jī)器學(xué)習(xí)庫(kù)sklearn(scikit-learn)的基礎(chǔ)知識(shí)和高級(jí)用法

    Scikit-Learn是 Python 最流行的機(jī)器學(xué)習(xí)庫(kù)之一,它提供了各種工具來(lái)實(shí)現(xiàn)、評(píng)估和探索各種學(xué)習(xí)算法,用于,各種機(jī)器學(xué)習(xí)任務(wù),在本教程中,我們將介紹 Scikit-Learn 的基礎(chǔ)知識(shí)和一些高級(jí)用法,并提供一些實(shí)例代碼來(lái)幫助我們更好地理解
    2023-07-07
  • 淺談python中頻繁的print到底能浪費(fèi)多長(zhǎng)時(shí)間

    淺談python中頻繁的print到底能浪費(fèi)多長(zhǎng)時(shí)間

    今天小編就為大家分享一篇淺談python中頻繁的print到底能浪費(fèi)多長(zhǎng)時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python 簡(jiǎn)單照相機(jī)調(diào)用系統(tǒng)攝像頭實(shí)現(xiàn)方法 pygame

    python 簡(jiǎn)單照相機(jī)調(diào)用系統(tǒng)攝像頭實(shí)現(xiàn)方法 pygame

    今天小編就為大家分享一篇python 簡(jiǎn)單照相機(jī)調(diào)用系統(tǒng)攝像頭實(shí)現(xiàn)方法 pygame,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 解決Python中報(bào)錯(cuò)TypeError: must be str, not bytes問(wèn)題

    解決Python中報(bào)錯(cuò)TypeError: must be str, not bytes問(wèn)題

    這篇文章主要介紹了解決Python中報(bào)錯(cuò)TypeError: must be str, not bytes問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • 關(guān)于pytorch訓(xùn)練分類器

    關(guān)于pytorch訓(xùn)練分類器

    這篇文章主要介紹了關(guān)于pytorch訓(xùn)練分類器問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能實(shí)例

    Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能實(shí)例

    這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能,結(jié)合完整實(shí)例形式分析了Python爬蟲(chóng)的基本原理及爬取百度百科詞條的步驟、網(wǎng)頁(yè)下載、解析、數(shù)據(jù)輸出等相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04
  • matplotlib subplot繪制多個(gè)子圖的方法示例

    matplotlib subplot繪制多個(gè)子圖的方法示例

    這篇文章主要介紹了matplotlib subplot繪制多個(gè)子圖的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論