python - 多进程spider
发布日期:2021-06-30 19:50:34
浏览次数:2
分类:技术文章
本文共 4057 字,大约阅读时间需要 13 分钟。
问题导读:
- 抓取‘君不见’性福模块文章,列表页文章图片
- url queue 1个进程,文章和图片各10个进程
解决方案:
#!/usr/bin/env python# coding=utf-8import multiprocessingimport urllib2import reimport uuidimport osimport timeclass Spider(): def __init__(self): self.counter = 0 self.lock = multiprocessing.Lock() # 文章超链接 self.queue = multiprocessing.Queue() # img self.imgs = multiprocessing.Queue() self.filename = './data/' + str(uuid.uuid1()) + '.txt' self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0'} def getUrls(self): urls = [] for i in range(1, 51): urls.append('http://www.junbujian.cc/xingfu/' + str(i) + '.html') for url in urls: request = urllib2.Request(url, headers=self.headers) try: response = urllib2.urlopen(request) content = response.read().decode('utf-8') except: content = '' print 'error:' + url pass pattern = re.compile(r'') article_url = re.findall(pattern, content) for a_url in article_url: # print a_url self.queue.put(a_url, timeout=2) img_pat = re.compile(r'') img_url = re.findall(img_pat, content) for i_url in img_url: self.imgs.put(i_url, timeout=2) def getImg(self): while not self.imgs.empty(): img_url = self.imgs.get(timeout=2) # os.path.isfile('filename') if not os.path.exists('./img/'): os.mkdir('./img/') img_name = './img/' + str(uuid.uuid1()) + '.jpg' with open(img_name, 'wb') as f: try: f.write(urllib2.urlopen(img_url).read()) self.lock.acquire() # print img_url print multiprocessing.current_process().name, ' ', img_name, ' 已保存...' self.lock.release() except: print 'error:' + img_url pass def getArticle(self): while not self.queue.empty(): art_url = self.queue.get(timeout=2) request = urllib2.Request(art_url, headers=self.headers) try: response = urllib2.urlopen(request) content = response.read().decode('utf-8') except: print 'error:',art_url content = '' # 更改点 (.) 的含义,使它与每一个字符匹配(而不是与除 \n 之外的每个字符匹配) pat = re.compile(r'(.*?) ', re.S) article = re.findall(pat, content) if not os.path.exists('./data'): os.mkdir('./data') with open('./data/' + str(uuid.uuid1()) + '.html', 'w') as f: try: f.write(multiprocessing.current_process().name + '\n' + article[0].encode('utf-8')) except: with open('./data/error.txt', 'a') as e: e.write('url:' + art_url + '\n') pass print '...' def run(self): urls_proc = multiprocessing.Process(target=self.getUrls) urls_proc.daemon =True urls_proc.start() print 'starting urls_proc...' time.sleep(20) imgs_proc_list = [] art_proc_list = [] for i in range(10): imgs_proc = multiprocessing.Process(target=self.getImg) imgs_proc_list.append(imgs_proc) imgs_proc.daemon = True imgs_proc.start() print 'starting proc',i for i in range(10): art_proc = multiprocessing.Process(target=self.getArticle) art_proc_list.append(art_proc) art_proc.daemon = True art_proc.start() print 'staring proc_a',i urls_proc.join(15) for proc in imgs_proc_list: proc.join(15) for proc in art_proc_list: proc.join(15) print 'end...'if __name__ == '__main__': spider = Spider() spider.run()
转载地址:https://lipenglin.blog.csdn.net/article/details/53328455 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月08日 04时36分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
JavaScript原生开关灯效果
2019-04-30
企业邮箱如何申请注册,邮箱申请如何免费注册?
2019-04-30
微信企业邮箱,手机邮箱格式地址怎么写?
2019-04-30
公司如何申请企业邮箱,公司邮箱怎么申请,公司企业邮箱哪个好?
2019-04-30
电子邮箱账号怎么申请,怎样申请邮箱账号呢
2019-04-30
邮箱怎么发邮件,邮件发信量多少,职场新人怎么发汇报邮件呢?
2019-04-30
maven 多层次pom 新引入包,编译成功,还是没有将包引入到本地
2019-04-30
leetCode2 两数相加
2019-04-30
【工具使用】使用pip与conda安装、更新与卸载Pytorch和torchvision
2019-04-30
【工具使用】Google免费云环境Colaboratory使用
2019-04-30
【深度学习笔记】卷积层,全连接层,池化层的相关输出参数计算
2019-04-30
【NLP学习笔记】文本分类概述
2019-04-30
【深度学习笔记】文本分类
2019-04-30
【转载】炼丹实验室:深度学习网络调参技巧
2019-04-30
【论文阅读笔记】文本分类论文汇总
2019-04-30