第一步:获取话题需要的url需要,并向上取整

 for idx in range(0,math.ceil(totals/5)):
url = f"https://www.zhihu.com/api/v4/questions/29114634/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_recognized%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cvip_info%2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled&limit=5&offset={idx*5}&platform=desktop&sort_by=default"
url_list.append(url)e 

第二步:使用多线程,批量请求所有话题内容,获取到【“书籍”】列表

#创建十个个线程作为生产者,请求
for x in range(10):
product = threading.Thread(target=get_pic_url)
product.start()

  

#生产者:请求url,获取所有书籍list
def get_pic_url():
while True:
glock.acquire()
if len(url_list) == 0:
glock.release()
break
else:
page_url = url_list.pop()
glock.release()
res = urllib.request.Request(page_url,headers=headers)#请求
res2 = urllib.request.urlopen(res).read().decode("utf-8")#获取html
objContent = json.loads(res2)['data']#获取数据data
ddd = re.compile(r'《.*?.》')#正则《》包裹的书籍
glock.acquire()
for rel in objContent:
result = ddd.findall(str(rel['content']))
for gtygty in result:
if len(gtygty)<30: #《》如果小于30个字符,就是正常书籍
contentList.append(gtygty)
else:
zz = re.compile(r'>.*?.<')#带有超链接的,则在处理一遍
hrefcontent = zz.findall(gtygty)
data = str(hrefcontent).replace(">","《",1).replace("<","》",1)
contentList.append(data[2:-2])
glock.release()

  第三:获取到图书后,统计每本书出现的次数

# 统计书籍出现的频率
def download_picture():
ifStop=0
submit = []
while True:
glock.acquire()
if len(contentList) ==0:
glock.release()
ifStop +=1
if ifStop == 2:
y2 = {k: v for k, v in sorted(tongjicishu.items(), key=lambda item: item[1], reverse=True)}
for key in y2.keys():
submit.append("<p>"+ key+ ":推荐人数"+str(y2[key])+"人</p>")
return ''.join(submit)
break
else:
continue
else:
url = contentList.pop()
glock.release()
#修改文件名
if tongjicishu.__contains__(url) :
tongjicishu[url]=tongjicishu[url]+1
else :
tongjicishu[url]=1

  第四步:调用download_picture函数,获取到可发布的带标签的content,并发布

putcontent = download_picture()
submitPut("<p>本话题汇总,目前"+str(totals)+"回答</p>"+"<p>每天"+time.strftime('%H:%M:%S')+"更新</p>"+json.dumps(putcontent,ensure_ascii=False))

  

def submitPut(putcontent):
putUrl = "https://www.zhihu.com/api/v4/answers/2342429808?include=is_visible%2Cpaid_info%2Cpaid_info_content%2Cadmin_closed_comment%2Creward_info%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_normal%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cattachment%2Crelationship.is_authorized%2Cvoting%2Cis_thanked%2Cis_author%2Cis_nothelp%2Cis_recognized%2Cis_labeled%3Bmark_infos%5B*%5D.url%3Bauthor.vip_info%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled"
data = {
"content":putcontent,
"reshipment_settings":"disallowed",
"comment_permission":"all",
"reward_setting":{"can_reward":False,"tagline":""},
"disclaimer_status":"close",
"disclaimer_type":"none",
"commercial_report_info":{"is_report":False},
"is_report":False,
"push_activity":True,
"table_of_contents_enabled":False,
"thank_inviter_status":"close"
}
datascontent=json.dumps(data).encode('utf8')
# data = urllib.parse.urlencode(formData).encode("utf-8")
putres = urllib.request.Request(putUrl,data=datascontent,headers=headers,method='PUT')#请求
putres2 = urllib.request.urlopen(putres).read().decode("utf-8")#获取html

 代码地址>>

使用python获取知乎**话题下的所有回答,并统计后发布。的更多相关文章

  1. Python获取指定文件夹下的文件名

    本文采用os.walk()和os.listdir()两种方法,获取指定文件夹下的文件名. 一.os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, t ...

  2. python获取知乎日报另存为txt文件

    前言 拿来练手的,比较简单(且有bug),欢迎交流~ 功能介绍 抓取当日的知乎日报的内容,并将每篇博文另存为一个txt文件,集中放在一个文件夹下,文件夹名字为当日时间. 使用的库 re,Beautif ...

  3. python 获取当前文件夹下所有文件名

    os 模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os 4 5 def file_name(file_d ...

  4. python获取当前文件夹下所有文件名【转】

    os 模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os 4 5 def file_name(file_d ...

  5. python获取指定文件夹下的文件路径

    #!/usr/bin/python# -*- coding: UTF-8 -*-# @date: 2018/1/6 23:08# @name: tmp2# @author:vickey-wu impo ...

  6. python获取指定文件夹下的文件和文件夹

    import os filepaths = []; dirpaths = []; pathName = r'C:\anfei\json\20191128' for root, dirs, files ...

  7. Python获取路径下所有文件名

    python 获取当前文件夹下所有文件名   os 模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os 4 ...

  8. python获取指定目录下特定格式的文件名

    之前一直用windows下的bat脚本获取一个目录下的指定格式的文件名,如下所示: dir *.jpg /b/s > train.set pause 十分简单,将这个bat文件放到你想要获取文件 ...

  9. python获取指定目录下所有文件名os.walk和os.listdir

    python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...

  10. Python+selenium之获取文本值和下拉框选择数据

    Python+selenium之获取文本值和下拉框选择数据 一.结合实例进行描述 1. 实例如下所示: #新增标签操作 def func_labels(self): self.driver.find_ ...

随机推荐

  1. 简单实用算法——二分查找法(BinarySearch)

    目录 算法概述 适用情况 算法原理 算法实现(C#) 实际应用:用二分查找法找寻边界值 参考文章 算法概述 二分查找(英语:binary search),也叫折半查找(英语:half-interval ...

  2. 从null-ls归档再看nvim的代码格式化与lint方案

    由于null-lsp的归档和暂停更新,我们需要重新审视并思考还有哪些架构简单易于理解的插件配置方案.本文将介绍脱离null-ls插件体系下的代码格式化和lint的插件配置方案. 在之前的文章中< ...

  3. VR虚拟现实技术下的汽车展厅:优劣势及运作方式

    虚拟现实汽车展厅其实是一种在线商店,可让客户在模拟环境中体验产品.这对无法亲自到店的人很有帮助.客户可以使用虚拟现实耳机来探索可用的不同型号和颜色.这可以帮助他们就购买哪辆汽车做出更明智的决定.虚拟现 ...

  4. python高级技术(进程一)

    一 什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...

  5. 爬虫实战:探索XPath爬虫技巧之热榜新闻

    之前我们已经详细讨论了如何使用BeautifulSoup这个强大的工具来解析HTML页面,另外还介绍了利用在线工具来抓取HTTP请求以获取数据的方法.在今天的学习中,我们将继续探讨另一种常见的网络爬虫 ...

  6. 降低FTP服务器速度的解决方案(Filezilla等)

    我最近发现,尽管有70Mbps(8.75MB / s)的互联网连接和1Gbps(125MB / s)的专用服务器可以从中下载,但我似乎只能从FTP服务器上以大约16.8Mbps(2.1MB / s)的 ...

  7. KingbaseES V8R6 集群运维系列 -- trusted_server

    案例说明: 在KingbaseES V8R3及V8R6早期的版本,对于读写分离的集群如果网关地址无法连通,将会导致整个集群关闭,数据库服务无法访问.在后期版本的改进中,降低了对网关的依赖性,当网关地址 ...

  8. 【mybatis踩坑】mybatis获取类型为字符串String的参数自动加引号

    今天写了一个简单的测试例子,用mybatis实现新建一个MySQL数据表 整体是JavaWeb项目,下面的代码是不完整的. 这是mapper 1 <?xml version="1.0& ...

  9. Echarts世界地图和网页表格数据交互联动

    html布局: 1 <div class="column"> 2 <div class="panel bl bar1"> 3 <d ...

  10. C语言跨平台时间操作计算时间差

    头文件 #pragma once #if defined(_WIN32) #include<sys/timeb.h> #if defined(__UNIX__)||defined(__AP ...