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

python實(shí)現(xiàn)多線程采集的2個(gè)代碼例子

 更新時(shí)間:2014年07月07日 10:36:23   投稿:junjie  
這篇文章主要介紹了python多線程采集代碼例子,使用了Threading、Queue、MySQLdb等模塊,需要的朋友可以參考下

代碼一:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#encoding=utf-8
 
import threading
import Queue
import sys
import urllib2
import re
import MySQLdb
 
#
# 數(shù)據(jù)庫(kù)變量設(shè)置
#
DB_HOST = '127.0.0.1'
DB_USER = "XXXX"
DB_PASSWD = "XXXXXXXX"
DB_NAME = "xxxx"
 
#
# 變量設(shè)置
#
THREAD_LIMIT = 3
jobs = Queue.Queue(5)
singlelock = threading.Lock()
info = Queue.Queue()
 
def workerbee(inputlist):
    for x in xrange(THREAD_LIMIT):
        print 'Thead {0} started.'.format(x)
        t = spider()
        t.start()
    for i in inputlist:
        try:
            jobs.put(i, block=True, timeout=5)
        except:
            singlelock.acquire()
            print "The queue is full !"
            singlelock.release()
 
    # Wait for the threads to finish
    singlelock.acquire()        # Acquire the lock so we can print
    print "Waiting for threads to finish."
    singlelock.release()        # Release the lock
    jobs.join()              # This command waits for all threads to finish.
    # while not jobs.empty():
    #   print jobs.get()
 
def getTitle(url,time=10):
    response = urllib2.urlopen(url,timeout=time)
    html = response.read()
    response.close()
    reg = r'<title>(.*?)</title>'
    title = re.compile(reg).findall(html)
    # title = title[0].decode('gb2312','replace').encode('utf-8')
    title = title[0]
    return title
 
class spider(threading.Thread):
    def run(self):
        while 1:
            try:
                job = jobs.get(True,1)
                singlelock.acquire()
                title = getTitle(job[1])
                info.put([job[0],title], block=True, timeout=5)
                # print 'This {0} is {1}'.format(job[1],title)
                singlelock.release()
                jobs.task_done()
            except:
                break;
 
if __name__ == '__main__':
    con = None
    urls = []
    try:
        con = MySQLdb.connect(DB_HOST,DB_USER,DB_PASSWD,DB_NAME)
        cur = con.cursor()
        cur.execute('SELECT id,url FROM `table_name` WHERE `status`=0 LIMIT 10')
        rows = cur.fetchall()
        for row in rows:
            # print row
            urls.append([row[0],row[1]])
        workerbee(urls)
        while not info.empty():
            print info.get()
    finally:
        if con:
            con.close()

代碼二:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#encoding=utf-8
#Filename:robot.py
 
import threading,Queue,sys,urllib2,re
#
# 變量設(shè)置
#
THREAD_LIMIT = 3        #設(shè)置線程數(shù)
jobs = Queue.Queue(5)      #設(shè)置隊(duì)列長(zhǎng)度
singlelock = threading.Lock()    #設(shè)置一個(gè)線程鎖,避免重復(fù)調(diào)用
 
urls = ['http://games.sina.com.cn/w/n/2013-04-28/1634703505.shtml','http://games.sina.com.cn/w/n/2013-04-28/1246703487.shtml','http://games.sina.com.cn/w/n/2013-04-28/1028703471.shtml','http://games.sina.com.cn/w/n/2013-04-27/1015703426.shtml','http://games.sina.com.cn/w/n/2013-04-26/1554703373.shtml','http://games.sina.com.cn/w/n/2013-04-26/1512703346.shtml','http://games.sina.com.cn/w/n/2013-04-26/1453703334.shtml','http://games.sina.com.cn/w/n/2013-04-26/1451703333.shtml','http://games.sina.com.cn/w/n/2013-04-26/1445703329.shtml','http://games.sina.com.cn/w/n/2013-04-26/1434703322.shtml','http://games.sina.com.cn/w/n/2013-04-26/1433703321.shtml','http://games.sina.com.cn/w/n/2013-04-26/1433703320.shtml','http://games.sina.com.cn/w/n/2013-04-26/1429703318.shtml','http://games.sina.com.cn/w/n/2013-04-26/1429703317.shtml','http://games.sina.com.cn/w/n/2013-04-26/1409703297.shtml','http://games.sina.com.cn/w/n/2013-04-26/1406703296.shtml','http://games.sina.com.cn/w/n/2013-04-26/1402703292.shtml','http://games.sina.com.cn/w/n/2013-04-26/1353703286.shtml','http://games.sina.com.cn/w/n/2013-04-26/1348703284.shtml','http://games.sina.com.cn/w/n/2013-04-26/1327703275.shtml','http://games.sina.com.cn/w/n/2013-04-26/1239703265.shtml','http://games.sina.com.cn/w/n/2013-04-26/1238703264.shtml','http://games.sina.com.cn/w/n/2013-04-26/1231703262.shtml','http://games.sina.com.cn/w/n/2013-04-26/1229703261.shtml','http://games.sina.com.cn/w/n/2013-04-26/1228703260.shtml','http://games.sina.com.cn/w/n/2013-04-26/1223703259.shtml','http://games.sina.com.cn/w/n/2013-04-26/1218703258.shtml','http://games.sina.com.cn/w/n/2013-04-26/1202703254.shtml','http://games.sina.com.cn/w/n/2013-04-26/1159703251.shtml','http://games.sina.com.cn/w/n/2013-04-26/1139703233.shtml']
 
def workerbee(inputlist):
  for x in xrange(THREAD_LIMIT):
    print 'Thead {0} started.'.format(x)
    t = spider()
    t.start()
  for i in inputlist:
    try:
      jobs.put(i, block=True, timeout=5)
    except:
      singlelock.acquire()
      print "The queue is full !"
      singlelock.release()
 
  # Wait for the threads to finish
  singlelock.acquire()    # Acquire the lock so we can print
  print "Waiting for threads to finish."
  singlelock.release()    # Release the lock
  jobs.join()       # This command waits for all threads to finish.
  # while not jobs.empty():
  #  print jobs.get()
 
def getTitle(url,time=10):
  response = urllib2.urlopen(url,timeout=time)
  html = response.read()
  response.close()
  reg = r'<title>(.*?)</title>'
  title = re.compile(reg).findall(html)
  title = title[0].decode('gb2312','replace').encode('utf-8')
  return title
 
class spider(threading.Thread):
  def run(self):
    while 1:
      try:
        job = jobs.get(True,1)
        singlelock.acquire()
        title = getTitle(job)
        print 'This {0} is {1}'.format(job,title)
        singlelock.release()
        jobs.task_done()
      except:
        break;
 
if __name__ == '__main__':
  workerbee(urls)

相關(guān)文章

  • YOLOv5以txt或json格式輸出預(yù)測(cè)結(jié)果的方法詳解

    YOLOv5以txt或json格式輸出預(yù)測(cè)結(jié)果的方法詳解

    這篇文章主要給大家介紹了關(guān)于YOLOv5以txt或json格式輸出預(yù)測(cè)結(jié)果的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Python如何使用cv2.canny進(jìn)行圖像邊緣檢測(cè)

    Python如何使用cv2.canny進(jìn)行圖像邊緣檢測(cè)

    這篇文章主要介紹了Python如何使用cv2.canny進(jìn)行圖像邊緣檢測(cè)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python?類型轉(zhuǎn)換函數(shù)示例詳解

    python?類型轉(zhuǎn)換函數(shù)示例詳解

    這篇文章主要介紹了python?類型轉(zhuǎn)換函數(shù)示例詳解,通過(guò)float()將一個(gè)字符串或數(shù)字轉(zhuǎn)換為浮點(diǎn)數(shù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Tensorflow之MNIST CNN實(shí)現(xiàn)并保存、加載模型

    Tensorflow之MNIST CNN實(shí)現(xiàn)并保存、加載模型

    這篇文章主要為大家詳細(xì)介紹了Tensorflow之MNIST CNN實(shí)現(xiàn)并保存、加載模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • python切割圖片的示例

    python切割圖片的示例

    這篇文章主要介紹了利用python切割圖片的示例,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下
    2020-11-11
  • 利用 Flask 動(dòng)態(tài)展示 Pyecharts 圖表數(shù)據(jù)方法小結(jié)

    利用 Flask 動(dòng)態(tài)展示 Pyecharts 圖表數(shù)據(jù)方法小結(jié)

    本文將介紹如何在 web 框架 Flask 中使用可視化工具 pyecharts, 看完本教程你將掌握幾種動(dòng)態(tài)展示可視化數(shù)據(jù)的方法。感興趣的朋友跟隨小編一起看看吧
    2019-09-09
  • Pygame Surface創(chuàng)建圖像的實(shí)現(xiàn)

    Pygame Surface創(chuàng)建圖像的實(shí)現(xiàn)

    本文主要介紹了Pygame Surface創(chuàng)建圖像的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Pytorch微調(diào)BERT實(shí)現(xiàn)命名實(shí)體識(shí)別

    Pytorch微調(diào)BERT實(shí)現(xiàn)命名實(shí)體識(shí)別

    命名實(shí)體識(shí)別(NER)是自然語(yǔ)言處理(NLP)中的一項(xiàng)關(guān)鍵任務(wù),它涉及識(shí)別和分類文本中的關(guān)鍵實(shí)體,BERT是一種強(qiáng)大的語(yǔ)言表示模型,在各種 NLP 任務(wù)中顯著提高了性能,包括 NER,在本文中,我們將展示如何使用 PyTorch 對(duì)預(yù)訓(xùn)練的 BERT 模型進(jìn)行微調(diào),以用于 NER 任務(wù)
    2025-03-03
  • Python爬蟲制作翻譯程序的示例代碼

    Python爬蟲制作翻譯程序的示例代碼

    這篇文章主要介紹了Python爬蟲制作翻譯程序的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Django1.3添加app提示模塊不存在的解決方法

    Django1.3添加app提示模塊不存在的解決方法

    這篇文章主要介紹了Django1.3添加app提示模塊不存在的解決方法,原因是新版和舊版的APP名稱寫法問(wèn)題,需要的朋友可以參考下
    2014-08-08

最新評(píng)論

银川市| 新乐市| 乐亭县| 全椒县| 罗田县| 老河口市| 池州市| 肇东市| 光泽县| 赫章县| 江华| 泽州县| 襄汾县| 阿拉尔市| 建湖县| 凤山市| 区。| 新密市| 进贤县| 格尔木市| 新绛县| 宣威市| 丰城市| 织金县| 大荔县| 西乌珠穆沁旗| 岳池县| 合作市| 鄂托克前旗| 上饶县| 姜堰市| 钟祥市| 永寿县| 丰原市| 阿拉善盟| 云梦县| 乌拉特前旗| 章丘市| 太仆寺旗| 吉林省| 庄浪县|