当前位置: 首页 > news >正文

网站建设报价新鸿儒百度联盟一天多少收入

网站建设报价新鸿儒,百度联盟一天多少收入,wordpress域名设置,广州开发区城市更新局文章目录一. Queue#xff08;队列对象#xff09;二. 多线程示意图三. 代码示例一. Queue#xff08;队列对象#xff09; Queue是python中的标准库#xff0c;可以直接import Queue引用;队列是线程间最常用的交换数据的形式 python下多线程的思考 对于资源#xff0… 文章目录一. Queue队列对象二. 多线程示意图三. 代码示例一. Queue队列对象 Queue是python中的标准库可以直接import Queue引用;队列是线程间最常用的交换数据的形式 python下多线程的思考 对于资源加锁是个重要的环节。因为python原生的list,dict等都是not thread safe的。而Queue是线程安全的因此在满足使用条件下建议使用队列 初始化 class Queue.Queue(maxsize) FIFO 先进先出 包中的常用方法: Queue.qsize() 返回队列的大小 Queue.empty() 如果队列为空返回True,反之False Queue.full() 如果队列满了返回True,反之False Queue.full 与 maxsize 大小对应 Queue.get([block[, timeout]])获取队列timeout等待时间 创建一个“队列”对象 import queue myqueue queue.Queue(maxsize 10) 将一个值放入队列中 myqueue.put(10) 将一个值从队列中取出 myqueue.get() 二. 多线程示意图 三. 代码示例 # codingutf-8 import requests from lxml import etree import json from queue import Queue import threadingclass Qiubai:def __init__(self):self.headers {User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWeb\Kit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36}self.url_queue Queue() #实例化三个队列用来存放内容self.html_queue Queue()self.content_queue Queue()def get_total_url(self):获取了所有的页面url并且返回urllistreturn listurl_temp https://www.qiushibaike.com/8hr/page/{}/# url_list []for i in range(1,36):# url_list.append(url_temp.format(i))self.url_queue.put(url_temp.format(i))def parse_url(self):一个发送请求获取响应同时etree处理htmlwhile self.url_queue.not_empty:url self.url_queue.get()print(parsing url:,url)response requests.get(url,headersself.headers,timeout10) #发送请求html response.content.decode() #获取html字符串html etree.HTML(html) #获取element 类型的htmlself.html_queue.put(html)self.url_queue.task_done()def get_content(self)::param url::return: 一个list包含一个url对应页面的所有段子的所有内容的列表while self.html_queue.not_empty:html self.html_queue.get()total_div html.xpath(//div[classarticle block untagged mb15]) #返回divelememtn的一个列表items []for i in total_div: #遍历div标枪获取糗事百科每条的内容的全部信息author_img i.xpath(./div[classauthor clearfix]/a[1]/img/src)author_img https: author_img[0] if len(author_img) 0 else Noneauthor_name i.xpath(./div[classauthor clearfix]/a[2]/h2/text())author_name author_name[0] if len(author_name) 0 else Noneauthor_href i.xpath(./div[classauthor clearfix]/a[1]/href)author_href https://www.qiushibaike.com author_href[0] if len(author_href) 0 else Noneauthor_gender i.xpath(./div[classauthor clearfix]//div/class)author_gender author_gender[0].split( )[-1].replace(Icon, ) if len(author_gender) 0 else Noneauthor_age i.xpath(./div[classauthor clearfix]//div/text())author_age author_age[0] if len(author_age) 0 else Nonecontent i.xpath(./a[classcontentHerf]/div/span/text())content_vote i.xpath(./div[classstats]/span[1]/i/text())content_vote content_vote[0] if len(content_vote) 0 else Nonecontent_comment_numbers i.xpath(./div[classstats]/span[2]/a/i/text())content_comment_numbers content_comment_numbers[0] if len(content_comment_numbers) 0 else Nonehot_comment_author i.xpath(./a[classindexGodCmt]/div/span[last()]/text())hot_comment_author hot_comment_author[0] if len(hot_comment_author) 0 else Nonehot_comment i.xpath(./a[classindexGodCmt]/div/div/text())hot_comment hot_comment[0].replace(\n, ).replace(\n, ) if len(hot_comment) 0 else Nonehot_comment_like_num i.xpath(./a[classindexGodCmt]/div/div/div/text())hot_comment_like_num hot_comment_like_num[-1].replace(\n, ) if len(hot_comment_like_num) 0 else Noneitem dict(author_nameauthor_name,author_imgauthor_img,author_hrefauthor_href,author_genderauthor_gender,author_ageauthor_age,contentcontent,content_votecontent_vote,content_comment_numberscontent_comment_numbers,hot_commenthot_comment,hot_comment_authorhot_comment_author,hot_comment_like_numhot_comment_like_num)items.append(item)self.content_queue.put(items)self.html_queue.task_done() #task_done的时候队列计数减一def save_items(self):保存items:param items:列表while self.content_queue.not_empty:items self.content_queue.get()f open(qiubai.txt,a)for i in items:json.dump(i,f,ensure_asciiFalse,indent2)# f.write(json.dumps(i))f.close()self.content_queue.task_done()def run(self):# 1.获取url list# url_list self.get_total_url()thread_list []thread_url threading.Thread(targetself.get_total_url)thread_list.append(thread_url)#发送网络请求for i in range(10):thread_parse threading.Thread(targetself.parse_url)thread_list.append(thread_parse)#提取数据thread_get_content threading.Thread(targetself.get_content)thread_list.append(thread_get_content)#保存thread_save threading.Thread(targetself.save_items)thread_list.append(thread_save)for t in thread_list:t.setDaemon(True) #为每个进程设置为后台进程效果是主进程退出子进程也会退出t.start() #为了解决程序结束无法退出的问题## for t in thread_list:# t.join()self.url_queue.join() #让主线程等待所有的队列为空的时候才能退出self.html_queue.join()self.content_queue.join()if __name__ __main__:qiubai Qiubai()qiubai.run()
http://www.zqtcl.cn/news/560835/

相关文章:

  • 廊坊网站推广局域网网站建设的步骤过程
  • 如何在工信部网站注册简易网页设计代码
  • 做石油系统的公司网站做艺术品展览的网站
  • 枣庄公司网站建设珠海蓝迪装饰设计工程有限公司
  • 广州企业网站营销电话成都网站建设制作设计
  • 求个网站带图片素材域名及密码登录域名管理网站
  • 文交所网站开发wordpress页面编辑插件
  • 丹徒网站建设价格做矿产公司的网站
  • 北京的制作网站的公司在哪里软件程序员
  • 企业网站怎么扣费的网站建设合同的性质
  • 聚美优品一个专注于做特价的网站如何制作个人网页兼职
  • 滨州做网站的公司最好wordpress主题
  • 福州网站设计软件公司dw网站开发流程
  • 合肥网站搭建公司哪家好深圳二维码网站建设
  • 东莞微信网站开发免费html模板素材网站
  • 海淀专业企业网站建设青岛平面设计公司
  • 北京正规网站建设比较wordpress cookies因预料之外的输出被阻止
  • 自助微信网站设计什么叫一级域名二级域名
  • 上海 顶尖 网站设计wordpress多站点不同主题
  • asp c 网站开发wordpress 动静分离
  • 服装网站建设规定wordpress禁止自动升级
  • 如何在网站上做社交的链接毕设给学校做网站
  • 网页设计与网站建设指标点您身边的网站建设顾问
  • 个人网站的制作广州网站优化招聘
  • 做网站产生的流量费怎么算软件开发前景和收入
  • 网站空间 .de单页型网站
  • 网站建设com品牌建设的作用
  • 优质作文网站柳州做网站去哪家公司好
  • 呼和浩特网站建设价格网站建设服务器
  • 做的比较好的电商网站西安有那些做网站的公司好