iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python+多线程+队列爬虫
  • 907
分享到

Python+多线程+队列爬虫

爬虫队列多线程 2023-01-31 02:01:50 907人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

python+多线程+队列,爬虫例子 # -*- coding: utf-8-*- import urllib2 import urllib import JSON import time import datetime import t

python+多线程+队列,爬虫例子

# -*- coding: utf-8-*-
import urllib2
import urllib
import JSON
import time
import datetime
import threading
import Queue
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

def get_response(url):
    for a in range(3):
        try:
            request = urllib2.Request(url)
            response = urllib2.urlopen(request)
            result= response.read()

            return result

        except Exception,e:
            print e
            time.sleep(2)
            continue

class ThreadCity(threading.Thread):
    def __init__(self,queue_zq_citys):
        threading.Thread.__init__(self)
        self.queue_zq_citys=queue_zq_citys
    def run(self):

        sql = 'select cityid,cname from  table '
        citylist=DBHelper.SqlHelper.ms.ExecQuery(sql)

        for c in citylist:
            try:
                #根据搜索城市名称获取城市
                qm_cname=c[1]
                data=urllib.urlencode({'q':qm_cname})
                url='xxx.xxx.com/ajax.do?' % data
                result=get_response(url)
                cjson=json.loads(result.decode('gb2312', 'ignore')) #json格式字符串转换为Python对象
                cityId=cjson["id"]
                cityname=cjson["cName"]

                #加入队列
                self.queue_zq_citys.put({'cityid':cityId,'cityname':cityname})
                time.sleep(1)

            except Exception,e:
               pass

class ThreadCityDB(threading.Thread):
    def __init__(self, queue_zq_citys):
        threading.Thread.__init__(self)
        self.queue_zq_citys = queue_zq_citys
    def run(self):
        while True:
            try:
                if self.queue_zq_citys.empty(): #队列为空
                    pass
                else:
                    citys=self.queue_zq_citys.get() #从队列中取出数据
                    if  citys is not None:
                        sql = "insert into Table(cityid,cityname) values(%s,'%s')" % (
                            citys['cityid'], citys['cityname'])
                        #print  sql
                        DBHelper.SqlHelper.ms.ExecNonQuery(sql.encode('utf-8'))
                        self.queue_zq_citys.task_done() #告诉线程我完成了这个任务 是否继续join阻塞 让线程向前执行或者退出
                    else:
                        pass
            except Exception,e:
                pass



def main():
    try:

        queue_zq_citys=Queue.Queue()  # 实例化存放抓取到的城市队列

        #创建线程
        city=ThreadCity(queue_zq_citys) #抓取线程 入队操作
        cityDB=ThreadCityDB(queue_zq_citys) #出队操作 存入数据库

        #启动线程
        city.start()
        cityDB.start()

        #阻塞等待子线程执行完毕后再执行主线程
        city.join()
        cityDB.join()
    except Exception,e:
        pass

if __name__ == '__main__':
    main()


--结束END--

本文标题: Python+多线程+队列爬虫

本文链接: https://www.lsjlt.com/news/185348.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作