代码一:

#!/usr/bin/python

# -*- coding: utf-8 -*-

#encoding=utf-8

import threading

import Queue

import sys

import urllib2

import re

import MySQLdb

#

# 数据库变量设置

#

DB_HOST = '127.0.0.1'

DB_USER = "XXXX"

DB_PASSWD = "XXXXXXXX"

DB_NAME = "xxxx"

#

# 变量设置

#

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 = 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

#

# 变量设置

#

THREAD_LIMIT = 3 #设置线程数

jobs = Queue.Queue(5) #设置队列长度

singlelock = threading.Lock() #设置一个线程锁,避免重复调用

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 = 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)

python多线程url采集器 + github_python实现多线程采集的2个代码例子相关推荐

  1. python制作壁纸获取器exe,壁纸采集

    python制作壁纸获取器exe,壁纸采集 [为想学习爬虫的小白朋友分享的入门级代码,一键获取大量壁纸] sorry: 很久没更新了,因为参加了实训就紧接着去学车了(每天都是车少人多,最多练三次),最 ...

  2. 33-Figma-数据采集器使用方式-后裔采集器

    33-Figma-数据采集器使用方式-后裔采集器 填充app行业相关的数据,通过后裔采集器完成 效果 1.填入地址,点击智能采集 2.不需要的字段可以删除 3.需要修改标题的话右键操作 4.一切合适, ...

  3. 火车头采集器V10下载-火车头采集器免费

    火车头采集器V10下载,火车头采集器V10操作难吗?使用火车头采集器需要一定的代码技术,以及编程能力才能更好的运用好火车头采集器V10,建议你先看HTML代码方法撰写采集规则.今天给大家分享一款可视化 ...

  4. 用火车头采集器8.6免费版采集图片

    用火车头采集器8.6免费版采集图片 最近,尝试了一下用"火车头采集器"采集页面上的图片,果然成功了. 关键是在设置"内容"标签的时候,勾选上"下载图片 ...

  5. 杰奇python采集器_极速杰奇采集器

    一,功能特色 本程序是杰奇小说系统的辅助采集器,使用的是杰奇后台采集规则,不像网上其他一些采集器,还需要重新编写采集规则,杰奇后台的采集规在网上随处都可以下到,并且本程序在所有windows系统上都可 ...

  6. 火车头采集器百度知道聚合采集插件说明文档!

    大家好,我是淘小白~今天来整理一下百度知道聚合采集插件的的说明文档! 1.应用软件 火车头采集器 2.插件类型 Python插件 3.插件逻辑说明 1.百度知道搜索关键词 提取前2页的百度知道url, ...

  7. php在线采集器,一个简单PHP采集器

    自己写的一个简单PHP采集器 //**************************************************************** $url = "http: ...

  8. php网页采集器 源码,PHP采集器的简单示例代码

    复制代码 代码示例: #zs#* * 采集器代码一例 * by www.jbxue.com #fzs# $url = "http://book.sina.com.cn/nzt/lit/zhu ...

  9. 图片采集器-网页图片批量采集器免费

    图片采集器可以采集网站上的各种图片,每个人都可以采集到各种高清图源.支持任意格式的图片采集,只需要导入链接即可批量采集图片.还有更多的采集方式:输入关键词全网图片采集/任意网站所有图片采集!不仅可以采 ...

最新文章

  1. 第六篇:基于朴素贝叶斯分类算法的邮件过滤系统
  2. 达摩院三周年,当初立的flag都实现了吗?
  3. python爬取百度文库付费文档_亲测免费转换百度文库付费文件
  4. 1.14 java内部类是什么鬼东西
  5. java中如何改变状态栏_如何创建状态栏
  6. solr analyzer_查看您的Solr缓存大小:Eclipse Memory Analyzer
  7. (48)VHDL实现8位奇偶校验电路(process语句语句)
  8. Android如何通过shareduserid获取系统权限
  9. 如何利用FineBI做财务分析
  10. mysql 处理一条语句卡死_一条MySQL查询语句,卡死机器,不知道为什么,求高手指点!...
  11. android 内存测试
  12. FR JavaScript 调用存储执行并输出影响行数
  13. 医学统计学计算机操作教程第3版附录答案,医学统计学课后习题集答案解析.doc...
  14. vsftpd的参数说明和虚拟用户配置
  15. 【自动驾驶】ROS机器人操作系统总结
  16. 【语音处理】时域信号分析基本工具,什么是窗函数
  17. 三维空间中点到点、点到直线、点到平面的距离计算
  18. PPT:动画出现设置
  19. 2024年上海美博会-上海浦东美博会(上海CBE)
  20. 软件设计模式与体系结构实验——2.1-1(2)(抽象)工厂模式的应用

热门文章

  1. opencv 高通滤波和低通滤波_一阶低通滤波原理详解
  2. 用python画圣诞树-python圣诞树
  3. python自学-学习Python,从入门到精通,其实只需要两个月就够了
  4. python编程入门书籍-零基础学习Python编程,这8本书必看!
  5. python有哪些用途-Python能用来做什么?以下是Python的三大主要用途
  6. 0基础学python做什么工作好-零基础学了8个月的Python,到底有啥感悟
  7. python从入门到放弃 图-python从入门到放弃(二)
  8. 第Q题:聪明的木匠(队列解答)=======一位老木匠需要将一根长的木棒切成N段...
  9. yarn在vscode里启动报错
  10. suse mysql 库文件_suse 10 下mysql安装