爬虫练习-知乎热搜榜
小于 1 分钟
爬虫练习-知乎热搜榜
代码
import requests
import json
def get_zhihu_hot_list():
url = "https://api.zhihu.com/topstory/hot-list" # 移动端获取热搜榜单数据api地址
# 模拟请求头
headers = {
"User-Agent": "ZhihuHybrid com.zhihu.android/Futureve/6.59.0 Mozilla/5.0 (Linux; Android 10; SM-G9650 Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36",
}
try:
# 获取api数据
response = requests.get(url, headers=headers)
# 检测https请求状态
response.raise_for_status()
# 最终接口数据
data = response.json()
if "data" in data:
print(f"{'排名':<5}{'热度':<15}{'标题'}")
print("-" * 80)
# enumerate(list):同时获取索引和值
for index, item in enumerate(data["data"], 1):
# 获取字典中的数据
target = item.get("target", {})
title = target.get("title", "No Title")
# 容错
hot_value = item.get("detail_text", "")
if not hot_value: # 找不到数据时
hot_value = "N/A"
print(f"{index:<5}{hot_value:<15}{title}")
else:
print("Failed to retrieve data format expected.")
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
except json.JSONDecodeError:
print("Error decoding JSON response")
if __name__ == "__main__":
print("---------欢迎启动python爬虫程序✈️,将爬取的网站信息为:知乎热搜榜---------")
get_zhihu_hot_list()
爬取效果

Loading...
