【爬虫小程序:爬取斗鱼所有房间信息】Xpath(协程池版)
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正
from gevent import monkey
monkey.patch_all() from gevent.pool import Pool
from queue import Queue
import requests
from lxml import etree
import time """爬取目标:http://www.qiushibaike.com/8hr/page/1
利用协程池实现
""" class QiuShi: def __init__(self): # url和headers
self.base_url = 'http://www.qiushibaike.com/8hr/page/{}'
self.headers = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36' # 定义队列,用来传递数据
self.url_queue = Queue()
self.request_queue = Queue()
self.html_queue = Queue() # 初始化线程池
self.pool = Pool() # 定义计数器
self.request_num = 0
self.response_num = 0 self.is_running = True def get_url_list(self):
"""获取所有的url"""
for i in range(1, 14):
target_url = self.base_url.format(i)
print(target_url)
self.request_num += 1
self.url_queue.put(target_url) def request_url(self):
"""向url发起请求"""
target_url = self.url_queue.get()
response = requests.get(target_url, self.headers)
self.request_queue.put(response)
self.url_queue.task_done() def get_content(self):
"""获取数据"""
html_text = self.request_queue.get().content.decode()
html = etree.HTML(html_text)
div_list = html.xpath('//div[@id="content-left"]/div')
content_list = []
for div in div_list:
item = {}
item['author'] = div.xpath('.//h2/text()')[0].strip()
item['content'] = div.xpath('.//span/text()')[0].strip()
print(item)
content_list.append(item)
self.html_queue.put(content_list)
self.request_queue.task_done() def save_data(self):
"""保存入库"""
data_list = self.html_queue.get()
for data in data_list:
with open('qiushi.text', 'a+') as f:
f.write(str(data))
f.write('\r\n')
self.html_queue.task_done() def execute_request_response_save(self):
"""线程池子,执行请求,回应,保存"""
self.request_url()
self.get_content()
self.save_data()
self.response_num += 1 def _callback(self, temp):
"""线程池的回调函数:"""
if self.is_running:
self.pool.apply_async(self.execute_request_response_save, callback=self._callback) def main(self):
"""主程序逻辑"""
self.get_url_list()
# 执行线程池
for n in range(4): # 用4个线程去执行线程池任务
self.pool.apply_async(self.execute_request_response_save, callback=self._callback) # time.sleep(10)
while True:
time.sleep(0.0001) # 为什么要写sleep? 注销执行一下,看一下结果
if self.request_num == self.response_num:
self.is_running = False
break if __name__ == '__main__':
qiushi = QiuShi()
qiushi.main()
【爬虫小程序:爬取斗鱼所有房间信息】Xpath(协程池版)的更多相关文章
- 【爬虫小程序:爬取斗鱼所有房间信息】Xpath
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正from selenium import webdriver import time class Douyu: "&q ...
- 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(线程池版)
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...
- 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多线程版)
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...
- 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多进程版)
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 import requests from lxml import etree from multiprocessing imp ...
- 基于webmagic的爬虫小应用--爬取知乎用户信息
听到“爬虫”,是不是第一时间想到Python/php ? 多少想玩爬虫的Java学习者就因为语言不通而止步.Java是真的不能做爬虫吗? 当然不是. 只不过python的3行代码能解决的问题,而Jav ...
- [python爬虫] Selenium定向爬取PubMed生物医学摘要信息
本文主要是自己的在线代码笔记.在生物医学本体Ontology构建过程中,我使用Selenium定向爬取生物医学PubMed数据库的内容. PubMed是一个免费的搜寻引擎,提供生物医学方 ...
- python爬虫实战之爬取智联职位信息和博客文章信息
1.python爬取招聘信息 简单爬取智联招聘职位信息 # !/usr/bin/env python # -*-coding:utf-8-*- """ @Author ...
- Python爬虫基础--分布式爬取贝壳网房屋信息(Client)
1. client_code01 2. client_code02 3. 这个时候运行多个client就可以分布式进行数据爬取.
- selenium,webdriver爬取斗鱼主播信息 实操
from selenium import webdriver import time from bs4 import BeautifulSoup class douyuSelenium(): #初始化 ...
随机推荐
- 【Linux命令】modprobe命令
modprobe(module probe)命令 用于自动处理可载入模块. 1)语法 modprobe [-acdlrtvV][--help][模块文件][符号名称 = 符号值] 2)补充 modpr ...
- Fire Balls 07——砖块的淡出,消失以及砖塔的下落
版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...
- CF922A Cloning Toys
A. Cloning Toys time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 201871010134-周英杰《面向对象程序设计(java)》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- Java并发包下的几个API
并发包 (计数器)CountDownLatch (屏障)CyclicBarrier (计数信号量)Semaphore 案例: 需求: 代码: 并发包 (计数器)CountDownLatch Coun ...
- Vert.x 学习之MongoDB Client
Vert.x MongoDB Client 原文档:Vert.x MongoDB Client 组件介绍 您的 Vert.x 应用可以使用 Vert.x MongoDB Client(以下简称客户端) ...
- Django之FBV和CBV的用法
FBV FBV,即 func base views,函数视图,在视图里使用函数处理请求. 以用户注册代码为例, 使用两个函数完成注册 初级注册代码 def register(request): &qu ...
- 用Python分析2000款避孕套,得出这些有趣的结论
到现在为止,我们的淘宝教程已经写到了第四篇,前三篇分别是: 第一篇:Python模拟登录淘宝,详细讲解如何使用requests库登录淘宝pc端. 第二篇:淘宝自动登录2.0,新增Cookies序列化, ...
- xml文档的解析并通过工具类实现java实体类的映射:XML工具-XmlUtil
若有疑问,可以联系我本人微信:Y1141100952 声明:本文章为原稿,转载必须说明 本文章地址,否则一旦发现,必追究法律责任 1:本文章显示通过 XML工具-XmlUtil工具实现解析soap报文 ...
- 使用 Docker 安装 showdoc
一.简介 ShowDoc 是一个非常适合IT团队在线共享文档的工具,在线访问地址为:https://www.showDoc.cc 本来也可以直接 pull showdoc 镜像到本地,使用 docke ...