最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基python實(shí)現(xiàn)多線程網(wǎng)頁爬蟲

 更新時(shí)間:2015年09月06日 09:56:37   作者:糖拌咸魚  
python是支持多線程的, 主要是通過thread和threading這兩個(gè)模塊來實(shí)現(xiàn)的,本文主要給大家分享python實(shí)現(xiàn)多線程網(wǎng)頁爬蟲,需要的朋友可以參考下

一般來說,使用線程有兩種模式, 一種是創(chuàng)建線程要執(zhí)行的函數(shù), 把這個(gè)函數(shù)傳遞進(jìn)Thread對(duì)象里,讓它來執(zhí)行. 另一種是直接從Thread繼承,創(chuàng)建一個(gè)新的class,把線程執(zhí)行的代碼放到這個(gè)新的class里。

實(shí)現(xiàn)多線程網(wǎng)頁爬蟲,采用了多線程和鎖機(jī)制,實(shí)現(xiàn)了廣度優(yōu)先算法的網(wǎng)頁爬蟲。

先給大家簡(jiǎn)單介紹下我的實(shí)現(xiàn)思路:

對(duì)于一個(gè)網(wǎng)絡(luò)爬蟲,如果要按廣度遍歷的方式下載,它是這樣的:

        1.從給定的入口網(wǎng)址把第一個(gè)網(wǎng)頁下載下來

        2.從第一個(gè)網(wǎng)頁中提取出所有新的網(wǎng)頁地址,放入下載列表中

        3.按下載列表中的地址,下載所有新的網(wǎng)頁

        4.從所有新的網(wǎng)頁中找出沒有下載過的網(wǎng)頁地址,更新下載列表

        5.重復(fù)3、4兩步,直到更新后的下載列表為空表時(shí)停止

python代碼如下:

#!/usr/bin/env python
#coding=utf-8
import threading
import urllib
import re
import time
g_mutex=threading.Condition()
g_pages=[] #從中解析所有url鏈接
g_queueURL=[] #等待爬取的url鏈接列表
g_existURL=[] #已經(jīng)爬取過的url鏈接列表
g_failedURL=[] #下載失敗的url鏈接列表
g_totalcount=0 #下載過的頁面數(shù)
class Crawler:
  def __init__(self,crawlername,url,threadnum):
    self.crawlername=crawlername
    self.url=url
    self.threadnum=threadnum
    self.threadpool=[]
    self.logfile=file("log.txt",'w')
  def craw(self):
    global g_queueURL
    g_queueURL.append(url)  
    depth=0
    print self.crawlername+" 啟動(dòng)..."
    while(len(g_queueURL)!=0):
      depth+=1
      print 'Searching depth ',depth,'...\n\n'
      self.logfile.write("URL:"+g_queueURL[0]+"........")
      self.downloadAll()
      self.updateQueueURL()
      content='\n>>>Depth '+str(depth)+':\n'
      self.logfile.write(content)
      i=0
      while i<len(g_queueURL):
        content=str(g_totalcount+i)+'->'+g_queueURL[i]+'\n'
        self.logfile.write(content)
        i+=1
  def downloadAll(self):
    global g_queueURL
    global g_totalcount
    i=0
    while i<len(g_queueURL):
      j=0
      while j<self.threadnum and i+j < len(g_queueURL):
        g_totalcount+=1
        threadresult=self.download(g_queueURL[i+j],str(g_totalcount)+'.html',j)
        if threadresult!=None:
          print 'Thread started:',i+j,'--File number =',g_totalcount
        j+=1
      i+=j
      for thread in self.threadpool:
        thread.join(30)
      threadpool=[]
    g_queueURL=[]
  def download(self,url,filename,tid):
    crawthread=CrawlerThread(url,filename,tid)
    self.threadpool.append(crawthread)
    crawthread.start()
  def updateQueueURL(self):
    global g_queueURL
    global g_existURL
    newUrlList=[]
    for content in g_pages:
      newUrlList+=self.getUrl(content)
    g_queueURL=list(set(newUrlList)-set(g_existURL))  
  def getUrl(self,content):
    reg=r'"(http://.+?)"'
    regob=re.compile(reg,re.DOTALL)
    urllist=regob.findall(content)
    return urllist
class CrawlerThread(threading.Thread):
  def __init__(self,url,filename,tid):
    threading.Thread.__init__(self)
    self.url=url
    self.filename=filename
    self.tid=tid
  def run(self):
    global g_mutex
    global g_failedURL
    global g_queueURL
    try:
      page=urllib.urlopen(self.url)
      html=page.read()
      fout=file(self.filename,'w')
      fout.write(html)
      fout.close()
    except Exception,e:
      g_mutex.acquire()
      g_existURL.append(self.url)
      g_failedURL.append(self.url)
      g_mutex.release()
      print 'Failed downloading and saving',self.url
      print e
      return None
    g_mutex.acquire()
    g_pages.append(html)
    g_existURL.append(self.url)
    g_mutex.release()
if __name__=="__main__":
  url=raw_input("請(qǐng)輸入url入口:\n")
  threadnum=int(raw_input("設(shè)置線程數(shù):"))
  crawlername="小小爬蟲"
  crawler=Crawler(crawlername,url,threadnum)
  crawler.craw()

以上代碼就是給大家分享的基python實(shí)現(xiàn)多線程網(wǎng)頁爬蟲,希望大家喜歡。

相關(guān)文章

  • Django學(xué)習(xí)筆記之View操作指南

    Django學(xué)習(xí)筆記之View操作指南

    這篇文章主要給大家介紹了關(guān)于Django學(xué)習(xí)筆記之View操作指南的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python中的?pass?占位語句

    Python中的?pass?占位語句

    這篇文章主要介紹了Python中的?pass?占位語句,Python?pass是空語句,是為了保持程序結(jié)構(gòu)的完整性,下文具體的相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下
    2022-04-04
  • python時(shí)間time模塊處理大全

    python時(shí)間time模塊處理大全

    這篇文章主要給大家介紹了關(guān)于python時(shí)間time模塊處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 基于Python實(shí)現(xiàn)智能停車場(chǎng)車牌識(shí)別計(jì)費(fèi)系統(tǒng)

    基于Python實(shí)現(xiàn)智能停車場(chǎng)車牌識(shí)別計(jì)費(fèi)系統(tǒng)

    這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)一個(gè)智能停車場(chǎng)車牌識(shí)別計(jì)費(fèi)系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下
    2022-04-04
  • 詳解python 3.6 安裝json 模塊(simplejson)

    詳解python 3.6 安裝json 模塊(simplejson)

    這篇文章主要介紹了python 3.6 安裝json 模塊(simplejson),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 利用python 更新ssh 遠(yuǎn)程代碼 操作遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)代碼

    利用python 更新ssh 遠(yuǎn)程代碼 操作遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)代碼

    這篇文章主要介紹了利用python 更新ssh 遠(yuǎn)程代碼 操作遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2018-02-02
  • Pytorch損失函數(shù)nn.NLLLoss2d()用法說明

    Pytorch損失函數(shù)nn.NLLLoss2d()用法說明

    這篇文章主要介紹了Pytorch損失函數(shù)nn.NLLLoss2d()用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 使用python和yolo方法實(shí)現(xiàn)yolo標(biāo)簽自動(dòng)標(biāo)注

    使用python和yolo方法實(shí)現(xiàn)yolo標(biāo)簽自動(dòng)標(biāo)注

    本文介紹了基于YOLOv10的自動(dòng)標(biāo)注方法,從初階的固定標(biāo)注到高階的基于YOLO檢測(cè)結(jié)果的自動(dòng)標(biāo)注,兩者相比,高階方法顯著提高了標(biāo)注的準(zhǔn)確性,并減少了人工操作的時(shí)間,</P><P>
    2024-11-11
  • PIP安裝python包出現(xiàn)超時(shí)問題的解決

    PIP安裝python包出現(xiàn)超時(shí)問題的解決

    這篇文章主要介紹了PIP安裝python包出現(xiàn)超時(shí)問題的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python使用pymysql實(shí)現(xiàn)操作mysql

    python使用pymysql實(shí)現(xiàn)操作mysql

    本文給大家講解的是在python中使用pymysql實(shí)現(xiàn)操作mysql的方法匯總,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下
    2016-09-09

最新評(píng)論

扎兰屯市| 文成县| 辽宁省| 漳州市| 仙桃市| 铜川市| 南阳市| 彭阳县| 子长县| 全州县| 潼关县| 大理市| 波密县| 双辽市| 双辽市| 昌邑市| 库尔勒市| 云阳县| 宜春市| 叶城县| 钟山县| 宁远县| 石门县| 保德县| 谢通门县| 出国| 全南县| 安岳县| 武邑县| 惠安县| 浦东新区| 娄底市| 南汇区| 科技| 陈巴尔虎旗| 林芝县| 舞阳县| 淅川县| 安平县| 新乡县| 团风县|