爬虫多线程模板,xpath,etree
class QuiShi:
def __init__(self):
self.temp_url = "http://www.lovehhy.net/Joke/Detail/QSBK/{0}"
self.headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"}
#1.Queue url队列
self.url_query = Queue()
# html网页队列
self.html_query = Queue()
# content内容队列
self.content_query = Queue()
def get_url_list(self):
for i in range(1,5):
self.url_query.put(self.temp_url.format(i)) def parse_url(self):
while True:
url = self.url_query.get()
self.html_query.put(requests.get(url,headers=self.headers).content.decode("gbk"))
self.url_query.task_done() def get_content_list(self):
# print(html_str)
#etree.HTML 变成树状结构
while True:
html_str = self.html_query.get()
html_str = html_str.replace("<br />","").strip("")
html = etree.HTML(html_str)
# s = html.xpath('//div[@id="footzoon"]')
h3_list = html.xpath('//div[@id="footzoon"]/h3')
content_list=[]
for h3 in h3_list:
item = {}
item["title"] = h3.xpath("./a/text()")
item["title_href"] = h3.xpath("./a/@href")
item["content"] =[]
s = h3.xpath('./following-sibling::div/text()')
for i in s:
item["content"].append(i.replace("\u3000",""))
content_list.append(item)
self.content_query.put(content_list)
self.html_query.task_done() def save_content_list(self):
while True:
cons = self.content_query.get()
print(cons)
self.content_query.task_done() def run(self):
# 1.获取url地址列表 t1 = threading.Thread(target=self.get_url_list)
t21 = threading.Thread(target=self.parse_url)
t22 = threading.Thread(target=self.parse_url)
t23 = threading.Thread(target=self.parse_url)
t3 = threading.Thread(target=self.get_content_list)
t4 = threading.Thread(target=self.save_content_list)
t1.start()
t21.start()
t22.start()
t23.start()
t3.start()
t4.start()
self.url_query.join()
self.html_query.join()
self.content_query.join() if __name__ == '__main__':
t1 = time.time()
quishi = QuiShi()
quishi.run()
print(time.time() - t1)
爬虫多线程模板,xpath,etree的更多相关文章
- 爬虫系列(九) xpath的基本使用
一.xpath 简介 究竟什么是 xpath 呢?简单来说,xpath 就是一种在 XML 文档中查找信息的语言 而 XML 文档就是由一系列节点构成的树,例如,下面是一份简单的 XML 文档: &l ...
- 从0开始学爬虫3之xpath的介绍和使用
从0开始学爬虫3之xpath的介绍和使用 Xpath:一种HTML和XML的查询语言,它能在XML和HTML的树状结构中寻找节点 安装xpath: pip install lxml HTML 超文本标 ...
- 爬虫 xpath etree自动补全页面
aa = etree.HTML(response.content) bb = etree.tostring(aa) doc = etree.HTML(bb)
- python动态网站爬虫实战(requests+xpath+demjson+redis)
目录 前言 一.主要思路 1.观察网站 2.编写爬虫代码 二.爬虫实战 1.登陆获取cookie 2.请求资源列表页面,定位获得左侧目录每一章的跳转url(难点) 3.请求每个跳转url,定位右侧下载 ...
- 爬虫之BS&Xpath
BeautifulSoup 一 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...
- 爬虫——BeautifulSoup和Xpath
爬虫我们大概可以分为三部分:爬取——>解析——>存储 一 Beautiful Soup: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功 ...
- sumafan:python爬虫多线程爬取数据小练习(附答案)
抓取 https://www.cnbeta.com/ 首页中新闻内容页网址, 抓取内容例子: https://hot.cnbeta.com/articles/game/825125 将抓取下来的内容页 ...
- python3 多线程 采集 xpath
#!/usr/bin/python # -*- coding: UTF-8 -*- '''Thread3 多线程测试采集''' import threading,time,queue,Mongo_ut ...
- 爬虫解析库xpath
# xpath简介 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言.用于在 XML 文档中通过元素和属性进行导航. XPath基于XM ...
随机推荐
- CDN实现原理
避让:尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定. 检测:通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时监 ...
- Cocos2d-x 学习笔记(25) 渲染 绘制 Render
[Cocos2d-x]学习笔记目录 本文链接:https://www.cnblogs.com/deepcho/p/cocos2dx-render.html 1. 从程序入口到渲染方法 一个Cocos2 ...
- SpringCloud之RefreshScope 源码解读
SpringCloud之RefreshScope @Scope 源码解读 Scope(org.springframework.beans.factory.config.Scope)是Spring 2. ...
- Rsync服务常见问题
转---Rsync 故障排查整理 Rsync服务常见问题汇总讲解: 1. 客户端的错误现象:No route to host rsync服务端开启的iptables**防火墙** [root@nfs0 ...
- fullpage.js报如下错的解决办法
控制台报错:fullPage: Fullpage.js version 3 has changed its license to GPLv3 and it requires a `licenseKey ...
- windows设置本地域名解析
1.首先找到host文件:C:\Windows\System32\drivers\etc 2.打开host文件: # Copyright (c) 1993-2009 Microsoft Corp. # ...
- Java基础(二十八)Java IO(5)RandomAccessFile类与过滤器流(Filter Stream)
一.RandomAccessFile类 使用RandomAccessFile类可以读取任意位置数据的文件. 1.构造方法 RandomAccessFile(String name, String mo ...
- 按钮组件.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Xbim.GLTF源码解析(四):轻量化处理
原创作者:flowell,转载请标明出处:https://www.cnblogs.com/flowell/p/10839433.html 在IFC标准中,由IfcRepresentationMap支持 ...
- 用Python编写简单的发红包程序和计算器原理
用Python编写简单的发红包程序: 第一种解法:数轴方法解决 import random def red_packet(money,num): money = money * 100 #将钱数转换成 ...