爬取知乎问题答案

news/2024/11/8 14:36:48/

参考博客:基于Python知乎回答爬虫 +jieba关键字统计可视化_知乎爬虫搜索关键词_菠萝柚王子的博客-CSDN博客

 

1、安装依赖包

import numpy
import requests
import certifi
from PIL import Image
from lxml import etree
import jieba
from wordcloud import WordCloud

手动安装插件

1、下载插件包,解压到路径Python3\Lib\site-packages

2、进入插件包,执行 python setup.py install命令进行安装

2、爬取问题答案

def fetch_text():headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8','Cookie': '_zap=45ffaab9-c328-4843-b252-1f521d538595; _xsrf=0c0fc9f1-b57a-43f0-a9c1-c95ec11e59fb; d_c0=AKBYoDTQaRaPTuBvfd2jHmPLHLky7s_fjJ4=|1677838132; ' \'KLBRSID=ca494ee5d16b14b649673c122ff27291|1677897534|1677897456; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1677838134; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1677897488;' \' captcha_session_v2=2|1:0|10:1677897488|18:captcha_session_v2|88:' \'WjlkMlJMeWNSUGQzV1M1ZzVqYVJqR0NUOGVyS1JBWlovZnYxRUVudEN6VnNUc0pVZWhwZGxsZStjWTBJd1Z2Zw==|8c57cc723eb09a7831a22c18cf6a4bcf1b906cf377457432e4abf75a67fd3e5c; ' \'gdxidpyhxdE=8eQjbxDfuEq18dZi20NAsNH%5C6YR%2Fe0ojcGKE%2BSzWTMZ%2F4fn2DbAIOK%2FdTeKrJMVjfZhwRN3Hm00KqXIMm4RMgN4qJ7sPKWI0gl07p3C6tT9oipWWGlnI7mIQDtqrL8M%2BkSc5z4mdOzT7LOluNpqStLP9r' \'PqEgwypqOf7HPppLG4Kvn28%3A1677898391211; YD00517437729195%3AWM_NI=WpDYVR1YaOo%2FKnQBpVcPHYko%2F6Rhxi%2FZOqrVf9HcRCbMlsl5heAV5MD5J9tx0mLPUjUPiRx2iTB%2BQUsKsPZmeYsEE5gYbKckD4EFCv' \'060QUMYmbK7IXwRpdNAwcrOKhvZ3I%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6ee98d44db69fa18ebc4d92e78ea3c54f978a9eb0d46f8fb8a197ea6d89b8a4a5f82af0fea7c3b92a95b08cbad55' \'af387afb0ee2589b29fa3f35ea19088baea65edea9e89fb63ed868192f15ea6e78884d149a293a494ef3facbb8fb7b84788eb86daf774f4b384b1d8629790a7b3fb3aab8c8eccd45fb195bdb1d967a787bda6c83fa5' \'87aeace26188ec8ba7c27ef4ea00b2d8479bb3aeacb6439ca99e91e74683a9ad93ce41acaa96d4cc37e2a3; YD00517437729195%3AWM_TID=thaY088YLfdBVERQQRbVLI09rkwOhxWv; ' \'ariawapChangeViewPort=false; ariaFixed=true; ariaReadtype=1; ariaoldFixedStatus=false; ariaStatus=false'}url = 'https://www.zhihu.com/question/308447090'  # 'https://www.zhihu.com/question/308447090'response = requests.get(url, headers=headers, verify=False)html = etree.HTML(response.text)title = html.xpath("//h1[@class='QuestionHeader-title']")[0].textkeyword = html.xpath("//div/meta[@itemprop='keywords']")[0].get("content")print(keyword)aheader = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0','Accept': '*/*','Accept-Language': 'en-US,en;q=0.5','Accept-Encoding': 'gzip, deflate, br','Referer': 'https://www.zhihu.com/question/308447090,','Cookie': '_zap=45ffaab9-c328-4843-b252-1f521d538595; _xsrf=0c0fc9f1-b57a-43f0-a9c1-c95ec11e59fb; d_c0=AKBYoDTQaRaPTuBvfd2jHmPLHLky7s_fjJ4=|1677838132; ' \'KLBRSID=81978cf28cf03c58e07f705c156aa833|1677922162|1677922052; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1677838134; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1677922160; ' \'captcha_session_v2=2|1:0|10:1677922161|18:captcha_session_v2|88:RzJCaGUwdWdQNXFxTGY4dHpSbVpvQVF4aHJ2Uk90SDRONVg5dUk0VmF0cWVaWnR5S1dJZnVOeDFIK3pZSVJkZg==|' \'8a6bd71d3cb5982308db4766953216ff6683fd1b7bd67324b7c55b1891fccaa0; gdxidpyhxdE=mJsIftPnWfKT9Vw8E082q719gmcIQISluGTpMDV%2BTocfs92oLE%2BujV6Xl%2FIQYxeK980UdIiYBxr7nrgP2Vv%2Bnv' \'LmqAWPy27YaL%2BmS9RRjQQydRA7cjoD8M%5Cf8kbaB1nMIMX45%5Cp5I48E2EjZiAOaPXbci9gI88A8r8qjVO%2BL3ohXe0lB%3A1677922765564; YD00517437729195%3AWM_NI=ruHLHxPBw1sq%2BFlGPhpu8bFoOhP%2BlZAhWu9' \'SimALgSDRgGW1rv9hl15B51cYlxxaY2cI87hRYbU3SXvKgMSeOBV8E%2FnFqi4unZOayj5qgj%2BGOV%2FDPh67LewEEONTU7%2BWaHQ%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eebadc34a9869c9be83f8de78a' \'a3d44e839e8facd8418fedafa5b740a399f9d2e92af0fea7c3b92ab2ab88adcb74949eb9d2f27be99aa4a8d364b4abf883c5658db8a0ccef5283b89996bc59abb38fade659888ea9a8d63bb2aaf789ef3bf79efbd2e253b0a8b882d' \'c3aa197f8d8eb5a9bbe9bd4d254b695a9aed26f8d9e9ad9c825ae9abf8ab77fae868182fc62f7afa98baa4d8d8ae1a5d364f6e98486f64f879182b5d83de989afa9d437e2a3; YD00517437729195%3AWM_TID=thaY088YLfdBVERQ' \'QRbVLI09rkwOhxWv; ariawapChangeViewPort=false; ariaFixed=true; ariaReadtype=1; ariaoldFixedStatus=false; ariaStatus=false'}question_id = 308447090  # 知乎问题idinterval = 5  # 一页html答案的数量offset = 0end = 1202  # 回答数i = 1file = open('anwsers.txt', 'w', encoding="utf8")while True:aurl = f'https://www.zhihu.com/api/v4/questions/{question_id}/feeds?include=content&limit={interval}&offset={offset}&order=default'aresponse = requests.get(aurl, headers=aheader, verify=False)# print(aresponse.text)anws = aresponse.json()["data"]print(len(anws))for anw in anws:content = anw["target"]["content"]excerpt = anw["target"]["excerpt"]file.write("anwser start~~~" + str(i) + "~~~\n")file.write(content + '\n')file.write(excerpt + '\n')file.write("anwser end~~~" + str(i) + "~~~\n")i += 1offset += intervalif offset >= end:print('结束!!!')breakfile.close()

3、 截取分析答案

#判断是否为汉字
def is_han(text):return all('\u4e00' <= char <= '\u9fff' for char in text)
def count_worlds():text = open('./anwsers.txt','r',encoding='utf-8').read()word_count = {}text_list = jieba.lcut(text, cut_all=True)new_text_list = []for content in text_list:if len(content) <= 1:continueif is_han(content):#print(content)word_count[content] = word_count.get(content, 0) + 1new_text_list.append(content)generate_text = ' '.join(new_text_list)sort_txt = sorted(list(word_count.items()), key = lambda a: a[1] ,reverse=True)print(sort_txt)mask_pic = numpy.array(Image.open('1.png'))#打开背景图片wordcloud = WordCloud(font_path=r"C:/Windows/Fonts/STSONG.TTF",collocations=False,max_words=100,min_font_size=10,max_font_size=500,mask=mask_pic).generate(generate_text)image = wordcloud.to_image()# image.show()wordcloud.to_file('result.png')  # 把词云保存下来

http://www.ppmy.cn/news/31606.html

相关文章

数据结构:链式二叉树初阶

目录 一.链式二叉树的逻辑结构 1.链式二叉树的结点结构体定义 2.链式二叉树逻辑结构 二.链式二叉树的遍历算法 1.前序遍历 2.中序遍历 3.后序遍历 4.层序遍历(二叉树非递归遍历算法) 层序遍历概念: 层序遍历算法实现思路: 层序遍历代码实现: 三.链式二叉树遍历算…

中移杭研一面经历

文章目录 1、常用的Java元注解@Documented@Target@Retention@Override@Deprecated@Inherited@Repeatable@Native2、Java注解的原理3、spring boot starter开发过程1、原理浅谈2、大概步骤3、项目介绍1、常用的Java元注解 @Documented @Documented 是一个标记注解,没有成员变…

A.特定领域知识图谱知识推理方案:知识图谱推理算法综述[二](DTransE/PairRE:基于表示学习的知识图谱链接预测算法)

推荐参考文章: A.特定领域知识图谱知识推理方案:知识图谱推理算法综述[一](基于距离的翻译模型:TransE、TransH、TransR、TransH、TransA、RotatE) A.特定领域知识图谱知识推理方案:知识图谱推理算法综述[二](DTransE/PairRE:基于表示学习的知识图谱链接预测算法) A.…

java多线程与线程池-04线程池与AQS

第7章 线程池与AQS java.util.concurrent包中的绝大多数同步工具,如锁(locks)和屏障(barriers)等,都基于AbstractQueuedSynchronizer(简称AQS)构建而成。这个框架提供了一套同步管理的通用机制,如同步状态的原子性管理、线程阻塞与解除阻塞,还有线程排队等。 在JD…

GM8773C 是一款 1:2 DSI 桥接芯片,可实现 4 路进 8 路出转换器功能、视频分离器功能。

GM8773C 是一款 1&#xff1a;2 DSI 桥接芯片&#xff0c;可实现 4 路进 8 路出转换器功能、视频分离器功能。芯片内集成了一个 4 路单一链路的 MIPI DSI 接收器和 8 路双链路 MIPI DSI 发送器。 接 收 器 每 路 可 以 支 持 到 2.0Gbps/lane &#xff0c; 可 以 最 高 支 持 到…

ccc-pytorch-LSTM(8)

文章目录一、LSTM简介二、LSTM中的核心结构三、如何解决RNN中的梯度消失/爆炸问题四、情感分类实战&#xff08;google colab&#xff09;一、LSTM简介 LSTM&#xff08;long short-term memory&#xff09;长短期记忆网络&#xff0c;RNN的改进&#xff0c;克服了RNN中“记忆…

驱动程序开发:FTP服务器和OpenSSH的移植与搭建、以及一些笔记

目录一、FTP服务器移植与搭建1、在ubuntu下安装vsftpd2、在window下安装FileZilla3、移植vsftpd到开发板上4、Filezilla 连接测试5、注意点二、开发板 OpenSSH 移植与使用1、移植 zlib 库2、移植 openssl 库3、移植 openssh 库4、openssh 使用测试三、关于u-boot上的操作及根文…

JavaScript 高级实例集合

文章目录JavaScript 高级实例集合创建一个欢迎 cookie简单的计时另一个简单的计时在一个无穷循环中的计时事件带有停止按钮的无穷循环中的计时事件使用计时事件制作的钟表创建对象的实例创建用于对象的模板JavaScript 高级实例集合 创建一个欢迎 cookie 源码 <!DOCTYPE ht…