[hidecontent type="payshow"]

Python使用requests爬取网络文章列表

import requests as req
from openpyxl import Workbook
import json

# 新建Excel档案
wb = Workbook()
ws = wb.active

# 插入 Excel 表格 Header
title = ["作者ID", "文章ID", "文章标题", "文章内容"]
ws.append(title)

# 设置网址并且发送请求
url = "http://jsonplaceholder.typicode.com/posts"
r = req.get(url)

# 解析Json
jsonData = json.loads(r.text)

# 写入 Excel
for data in jsonData:
    post = []
    post.append(data["userId"])
    post.append(data["id"])
    post.append(data["title"])
    post.append(data["body"])

    ws.append(post)

# 储存文件
wb.save("test.xlsx")

[/hidecontent]