python中data.find_all爬取网站为空列表_入门级案例 | 爬取知乎和微博热门数据的代码和思路...

源 / TEDxPY    文 / TED 今天来分享下这两天写的入门级的爬取知乎热榜和微博热门数据的代码和思路。 网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。 知乎热榜中的内容热度值,是根据该条内容近24小时内的浏览量、互动量、专业加权、创作时间及在榜时间等维度,综合计算得出的。知乎热榜即根据内容热度值制定的排行榜。

知乎热榜链接:https://www.zhihu.com/billboard
https://www.zhihu.com/hot

e32d4a00222b99fed30ee61cb9ccf3a2.png 微博的热度值是根据该篇微博被转发、点赞数和微博发布时间等各项因素,来算出热度基数,再与热度权重相加,得出最终的热度值。微博热门即话题热度排行榜。 微博热门链接: https://s.weibo.com/top/summary b9145228360b79f54dde9e6c556174e6.png 今天我们要做的就是将相关排行榜中的话题内容爬取下来当作数据素材。换句话说,我们要把页面上排好的信息,通过代码读取并保存起来。 <
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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文件。你可以根据自己的需求修改关键词以及保存的文件格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值