python知乎爬虫收藏夹_Python爬取知乎问题收藏夹 爬虫入门

本文介绍了如何使用Python和BeautifulSoup爬取知乎热门问题的数据,包括问题、答主信息、赞数、评论数等,并将数据存储到MongoDB数据库。初学者可通过此实例掌握基础爬虫技术和数据库操作。
摘要由CSDN通过智能技术生成

简介

知乎的网站是比较好爬的,没有复杂的反爬手段,适合初学爬虫的人作为练习

因为刚刚入门python,所以只是先把知乎上热门问题的一些主要信息保存到数据库中,待以后使用这些信息进行数据分析,爬取的网页链接是赞同超过1000的回答

网页分析

1.分析网站的页面结构

准备提取热门问题的问题、答主、赞数、评论数等内容

2cf7a44e7617a00a95fa019db4f4aca9.png

界面分析

2.分析网站的元素

选择页面中需要爬取的内容对应的元素,分析特征(class,id等),稍后使用BeautifulSoap爬取这些内容

8d1de5d5113d0128d114235d04c63451.png

HTML分析

3.用Beautifulsoup解析获取的网页

这些网页的url的数字是递增的,拼接字符串就可以得到网页的链接了

url_part = "https://www.zhihu.com/collection/19928423?page=" # 赞数超过一千的收藏夹

url = url_part + str(i) # 拼接知乎爬取链接

用BeautifulSoap解析部分的代码

def find_answers(url, collection):

get_html = requests.get(url, headers=Web.headers) # requests请求页面内容

soup = BeautifulSoup(get_html.text, 'lxml') # BeautifulSoup解析页面内容

items = soup.find_all('div', class_="zm-item") # 获取所有的热门问题内容

success = 0

error = 0

for item in items:

try:

data = store_answer(item)

collection.insert(data) # 插入到数据表中

except AttributeError as e:

error += 1 # 发生错误

else:

success += 1

def store_answer(answer):

data = {

"title": answer.find("h2", class_="zm-item-title").text, # 问题题目

"like_num": answer.find("div", class_="zm-item-vote").text, # 问题赞数

"answer_user_name": answer.find("div", class_="answer-head").find("span", class_="author-link-line").text, # 答主姓名

"answer_user_sign": answer.find("div", class_="answer-head").find("span", class_="bio").text, # 答主签名

"answer": answer.find("div", class_="zh-summary summary clearfix").text, # 问题摘要

"time": answer.find("p", class_="visible-expanded").find("a", class_="answer-date-link meta-item").text, # 问题编辑时间

"comment": answer.find("div", class_="zm-meta-panel").find("a",

class_="meta-item toggle-comment js-toggleCommentBox").text,

# 问题评论数

"link": answer.find("link").get("href") # 问题链接

}

return data

4.完整代码

import time # 计算程序时间所用的库

import requests # 获取页面所用的库

from bs4 import BeautifulSoup # 提取页面所用的库

from pymongo import MongoClient # 连接数据库所用的库

class Web:

headers = {'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"

"56.0.2924.87 Safari/537.36"} # 请求头

url_part = "https://www.zhihu.com/collection/19928423?page=" # 赞数超过一千的收藏夹

def get_collection():

client = MongoClient('mongodb://localhost:27017/') # 连接到Mongodb

db = client.data # 打开数据库 "data"(数据库名称可以自己修改)

collection = db.zhihu # 打开表 "zhihu"(表名称可以自己修改)

return collection

def store_answer(answer):

data = {

"title": answer.find("h2", class_="zm-item-title").text, # 问题题目

"like_num": answer.find("div", class_="zm-item-vote").text, # 问题赞数

"answer_user_name": answer.find("div", class_="answer-head").find("span", class_="author-link-line").text, # 答主姓名

"answer_user_sign": answer.find("div", class_="answer-head").find("span", class_="bio").text, # 答主签名

"answer": answer.find("div", class_="zh-summary summary clearfix").text, # 问题摘要

"time": answer.find("p", class_="visible-expanded").find("a", class_="answer-date-link meta-item").text, # 问题编辑时间

"comment": answer.find("div", class_="zm-meta-panel").find("a",

class_="meta-item toggle-comment js-toggleCommentBox").text,

# 问题评论数

"link": answer.find("link").get("href") # 问题链接

}

return data

def find_answers(url, collection):

get_html = requests.get(url, headers=Web.headers) # requests请求页面内容

soup = BeautifulSoup(get_html.text, 'lxml') # BeautifulSoup解析页面内容

items = soup.find_all('div', class_="zm-item") # 获取所有的热门问题内容

success = 0

error = 0

for item in items:

try:

data = store_answer(item)

collection.insert(data) # 插入到数据表中

except AttributeError as e:

error += 1 # 发生错误

else:

success += 1

print("Error: %d" % error, end=' ')

return success

def get_zhihu():

collection = get_collection()

start_time = time.time() # 获取初始时间

answer_num = 0 # 记录已爬取问题数

start_page = 1 # 记录已爬取网页数

last_page = 6319 # 爬取的收藏夹最后一页的页码,可以根据当前数目自行调整

page_list = [i for i in range(start_page, last_page)]

for page in page_list:

print("Page: %d" % page)

url = Web.url_part + str(page)

try:

answer_num += find_answers(url, collection)

print("Used: %.1fs Total: %d" % (time.time() - start_time, answer_num))

except Exception as e:

print(e)

page_list.append(page)

if __name__ == "__main__":

get_zhihu() # 运行爬虫程序

print("Done")

代码基于Python3.6环境,需要先安装pymongo,BeautifulSoap等依赖库,可在终端中输入pip3 install pymongo bs4 lxml -i https://pypi.douban.com/simple/安装,运行爬虫后的数据库的结果如下图

987d577ecd14770d0255ab66f0e42867.png

数据库

总共爬取了近六万条回答,可以在终端使用mongoexport -d data -c zhihu --csv -o zhihu.csv -f question,user_info_name,link,like,comment,time,answer,user_info_sign这条命令将数据库中的数据导出到zhihu.csv(utf-8编码)文件中,也可以改变命令的参数导出成json格式

0.安装及数据库入门

运行本项目需要安装 Python3,Mongodb数据库,可以使用Compass或者Robomongo等可视化软件管理数据库。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
需要使用Python编程语言来爬取知乎问题下的所有回答。具体步骤如下: 1. 首先需要安装Python的requests和beautifulsoup4库,用于发送HTTP请求和解析HTML页面。 2. 获取知乎问题页面的URL,可以手动复制粘贴,或者使用爬虫自动获取。 3. 使用requests库发送GET请求,获取知乎问题页面的HTML源代码。 4. 使用beautifulsoup4库解析HTML源代码,获取所有回答的信息。 5. 对每个回答进行解析,获取回答的文本、作者、点赞数、评论数等信息。 6. 将获取到的信息存储到本地文件或数据库中。 下面是一段示例代码,可以爬取知乎某个问题下的所有回答: ```python import requests from bs4 import BeautifulSoup # 知乎问题页面的URL url = 'https://www.zhihu.com/question/xxxxxx' # 发送GET请求,获取页面HTML源代码 response = requests.get(url) html = response.text # 解析HTML页面,获取所有回答的信息 soup = BeautifulSoup(html, 'html.parser') answers = soup.find_all('div', class_='List-item') # 遍历每个回答,解析并存储信息 for answer in answers: # 解析回答文本、作者、点赞数、评论数等信息 text = answer.find('div', class_='RichContent-inner').get_text() author = answer.find('div', class_='ContentItem-head').get_text() upvotes = answer.find('button', class_='Button VoteButton VoteButton--up').get_text() comments = answer.find('button', class_='Button ContentItem-action Button--plain Button--withIcon Button--hoverCard').get_text() # 将信息存储到本地文件或数据库中 with open('answers.txt', 'a', encoding='utf-8') as f: f.write(f'{author}\n{text}\n赞同数:{upvotes} 评论数:{comments}\n\n') ``` 需要注意的是,爬取知乎数据属于个人行为,需要遵守知乎的相关规定,不得用于商业用途。另外,爬取速度也需要适当控制,避免给服务器造成过大的负担。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值