爬虫小试牛刀(知乎用户关注列表爬取 python+requests+json)

知乎用户关注列表爬取

基于环境:python3.6 + requests + json 模块
调试浏览器:chrome浏览器

环境安装

  1. python可自行到官网下载
  2. requests模块和json模块安装,直接pip

    pip install requests
    pip install json

目标分析

  1. 目标用户
    廖雪峰:https://www.zhihu.com/people/liaoxuefeng

  2. f12开启调试工具

  3. 将文件类型 定位到 XHR,并点击 关注者 如下图:
    请求返回的数据一般能够在XHR 或者 JS 中可以找到,命名前缀都会与我们要的东西相关,在左边列表中找到followers开头的文件,点击,在右边的Preview下可以看到用户的关注用户数据
    在这里插入图片描述

  4. 我们从 Preview 切到 Headers 可以看到请求信息和响应信息,第一个便是请求 url

    Request URL: https://www.zhihu.com/api/v4/members/liaoxuefeng/followers?include=data[*].answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge[%3F(type%3Dbest_answerer)].topics&offset=0&limit=20

    先测试一下:

    import requests
    
    url = "https://www.zhihu.com/api/v4/members/liaoxuefeng/followers?include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=0&limit=20"
    response =</
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Python 模拟爬虫抓取知乎用户信息以及人际拓扑关系,使用scrapy爬虫框架,数据存储使用mongo数据库。   本地运行请注意:爬虫依赖mongo和rabbitmq,因此这两个服务必须正常运行和配置。为了加快下载效率,图片下载是异步任务,因此在启动爬虫进程执行需要启动异步worker,启动方式是进入zhihu_spider/zhihu目录后执行下面命令:   celery -A zhihu.tools.async worker --loglevel=info   爬虫项目部署:   进入zhihu_spider后执行```docker-compose up``` ,进入container后和本地运行方法相同,依次启动mongo、rabbitmq、异步任务、爬虫进程即可。   其它需要说明的问题:   爬虫框架从start\_requests开始执行,此部分会提交知乎主页的访问请求给引擎,并设置回调函数为post_login.   post\_login解析主页获取\_xsrf保存为成员变量中,并提交登陆的POST请求,设置回调函数为after\_login.   after\_login拿到登陆后的cookie,提交一个start\_url的GET请求给爬虫引擎,设置回调函数parse\_people.   parse\_people解析个人主页,一次提交关注人和粉丝列表页面到爬虫引擎,回调函数是parse\_follow, 并把解析好的个人数据提交爬虫引擎写入mongo。   parse\_follow会解析用户列表,同时把动态的人员列表POST请求发送只引擎,回调函数是parse\_post\_follow,把解析好的用户主页链接请求也发送到引擎,人员关系写入mongo。   parse\_post\_follow单纯解析用户列表,提交用户主页请求至引擎。
以下是Python爬取知乎数据的代码实现,其中使用了八爪鱼爬虫工具和数据预处理库pandas: ```python import requests import json import pandas as pd from octopus import Octopus # 设置请求头信息 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' } # 构造请求url def get_url(keyword, offset): url = 'https://www.zhihu.com/api/v4/search_v3?t=general&q={}&correction=1&offset={}&limit=20&lc_idx=0&show_all_topics=0&search_hash_id='.format(keyword, offset) return url # 爬取知乎数据并保存为json文件 def crawl_data(keyword): otto = Octopus( concurrency=8, auto_start=True, expiration_in_seconds=120, raise_all_exceptions=True, worker_lifespan_in_seconds=120 ) result = [] for i in range(0, 100, 20): url = get_url(keyword, i) otto.enqueue(get_data, url) otto.wait() for res in otto.results(): result += res with open('zhihu.json', 'w', encoding='utf-8') as f: json.dump(result, f, ensure_ascii=False) # 解析json文件并使用pandas进行数据预处理 def process_data(): with open('zhihu.json', 'r', encoding='utf-8') as f: data = json.load(f) results = [] for item in data: result = {} result['问题'] = item['highlight']['title'] result['链接'] = 'https://www.zhihu.com/question/{}'.format(item['object']['question']['id']) result['答案'] = item['highlight']['description'] results.append(result) df = pd.DataFrame(results) df.to_excel('zhihu.xlsx', index=False) # 获取数据 def get_data(url): response = requests.get(url, headers=headers) data = response.json()['data'] result = [] for item in data: if item['object']['type'] == 'answer': result.append(item) return result if __name__ == '__main__': crawl_data('Python') process_data() ``` 此代码实现了爬取知乎关于“Python”的数据,并将数据保存成json文件,再使用pandas进行数据预处理,最终将结果保存成Excel文件。你可以根据自己的需求修改关键词以及保存的文件格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_linbobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值