前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AI网络爬虫:批量获取post请求动态加载的json数据

AI网络爬虫:批量获取post请求动态加载的json数据

作者头像
AIGC部落
发布2024-06-24 19:44:19
970
发布2024-06-24 19:44:19
举报
文章被收录于专栏:Dance with GenAIDance with GenAI

网站https://www.futurepedia.io/ai-innovations的数据是通过post请求动态加载的:

查看几页的请求载荷:

{"companies":[],"startDate":"2023-12-01T00:00:00.000Z","endDate":"2024-06-09T12:25:08.525Z","limit":25,"page":9,"categories":[],"itemTypes":[],"query":null}

{"companies":[],"startDate":"2023-12-01T00:00:00.000Z","endDate":"2024-06-09T12:25:08.525Z","limit":25,"page":7,"categories":[],"itemTypes":[],"query":null}

{"companies":[],"startDate":"2023-12-01T00:00:00.000Z","endDate":"2024-06-09T12:25:08.525Z","limit":25,"page":5,"categories":[],"itemTypes":[],"query":null}

这三个请求载荷的主要区别在于它们的"page"参数。这个参数通常用于分页,表示请求的是第几页的数据。具体来说:

  1. 第一个请求载荷请求的是第9页的数据。
  2. 第二个请求载荷请求的是第7页的数据。
  3. 第三个请求载荷请求的是第5页的数据。

其他参数,如"companies"、"startDate"、"endDate"、"limit"、"categories"和"itemTypes",在这三个请求中都是相同的。"startDate"和"endDate"定义了请求数据的时间范围,"limit"定义了每页显示的数据条数,而"categories"和"itemTypes"可能用于过滤数据,但在这里它们都是空的,表示没有应用任何过滤条件。"query"参数也是空的,表示没有使用任何搜索查询。

查看返回的json数据:

{

"products": [

{

"id": "2dd3fed5-fb31-473d-8c13-b731c9617657",

"name": "Copilot for Data Factory",

"company": {

"name": "Microsoft",

"slug": "microsoft"

},

"category": "Automation",

"itemType": "Feature",

"shortDescription": "Automates data ingestion and transformation processes",

"longDescription": "Provides AI-driven guidance for data integration, ensuring up-to-date and accurate warehouse data.",

"releaseDate": "2024-05-23",

"sources": [

"https://blog.fabric.microsoft.com/en-us/blog/announcing-the-public-preview-of-copilot-for-data-warehouse-in-microsoft-fabric?ft=All"

]

},

ChatGPT输入提示词:

你是一个Python编程专家,完成一个Python脚本编写的任务,具体步骤如下:

在F盘新建一个Excel文件:AIInnovations20240609.xlsx

爬取网页:

请求网址:

https://www.futurepedia.io/api/product-releases

请求方法:

POST

状态代码:

200 OK

远程地址:

172.67.176.202:443

引荐来源网址政策:

strict-origin-when-cross-origin

请求载荷:

{"companies":[],"startDate":"2023-12-01T00:00:00.000Z","endDate":"2024-06-09T12:25:08.525Z","limit":25,"page":{pagenumber},"categories":[],"itemTypes":[],"query":null}

{pagenumber}的值是从1开始,以1递增,以160结束;

获取网页的json数据;

提取这个json数据中"products"键的值,这个值也是一个json数据;

提取这个json数据中所有键的名称,写入Excel文件的表头,所有键对应的值,写入Excel文件的数据列;

保存Excel文件;

注意:每一步都输出信息到屏幕;

每爬取1页数据后暂停5-9秒;

需要对 JSON 数据进行预处理,将嵌套的字典和列表转换成适合写入 Excel 的格式,比如将嵌套的字典转换为字符串。

要设置请求标头:

Accept:

*/*

Accept-Encoding:

gzip, deflate, br, zstd

Accept-Language:

zh-CN,zh;q=0.9,en;q=0.8

Content-Length:

156

Content-Type:

text/plain;charset=UTF-8

Origin:

https://www.futurepedia.io

Priority:

u=1, i

Referer:

https://www.futurepedia.io/ai-innovations

Sec-Ch-Ua:

"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"

Sec-Ch-Ua-Mobile:

?0

Sec-Ch-Ua-Platform:

"Windows"

Sec-Fetch-Dest:

empty

Sec-Fetch-Mode:

cors

Sec-Fetch-Site:

same-origin

User-Agent:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

ChatGPT生成的源代码:

import requests

import openpyxl

import time

import random

import json

# 文件路径和名称

file_path = r"F:\AIInnovations20240609.xlsx"

# 创建一个新的 Excel 工作簿

wb = openpyxl.Workbook()

ws = wb.active

ws.title = "Product Releases"

# 初始页码

page_number = 1

# 请求的 URL 和方法

url = "https://www.futurepedia.io/api/product-releases"

# 请求头

request_headers = {

'Accept': '*/*',

'Accept-Encoding': 'gzip, deflate, br, zstd',

'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',

'Content-Length': '156',

'Content-Type': 'text/plain;charset=UTF-8',

'Origin': 'https://www.futurepedia.io',

'Priority': 'u=1, i',

'Referer': 'https://www.futurepedia.io/ai-innovations',

'Sec-Ch-Ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',

'Sec-Ch-Ua-Mobile': '?0',

'Sec-Ch-Ua-Platform': '"Windows"',

'Sec-Fetch-Dest': 'empty',

'Sec-Fetch-Mode': 'cors',

'Sec-Fetch-Site': 'same-origin',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'

}

# 表头写入标志

headers_written = False

# 数据处理函数:将嵌套的字典或列表转换为字符串

def clean_data(value):

if isinstance(value, (dict, list)):

return json.dumps(value, ensure_ascii=False) # 确保非 ASCII 字符被正确编码

return value

# 爬取数据

while page_number <= 190:

print(f"正在爬取第 {page_number} 页的数据...")

# 请求载荷

payload = {

"companies": [],

"startDate": "2023-12-01T00:00:00.000Z",

"endDate": "2024-06-09T12:25:08.525Z",

"limit": 25,

"page": page_number,

"categories": [],

"itemTypes": [],

"query": None

}

# 确保 request_headers 是字典类型

if not isinstance(request_headers, dict):

print(f"错误:请求头不是字典类型:{type(request_headers)}")

break

# 调试打印请求头

print(f"请求头:{request_headers}")

# 发送 POST 请求

try:

response = requests.post(url, headers=request_headers, json=payload)

except Exception as e:

print(f"请求失败,错误信息:{e}")

break

# 检查请求是否成功

if response.status_code != 200:

print(f"请求失败,状态码:{response.status_code}")

break

# 获取 JSON 数据

data = response.json()

products = data.get("products", [])

if not headers_written and products:

# 获取 JSON 数据中的键作为表头

excel_headers = list(products[0].keys())

ws.append(excel_headers)

headers_written = True

# 将数据写入 Excel

for product in products:

cleaned_product = [clean_data(product.get(header, "")) for header in excel_headers]

ws.append(cleaned_product)

# 保存 Excel 文件

wb.save(file_path)

print(f"第 {page_number} 页的数据已写入 Excel 文件。")

# 随机暂停 5-9 秒

sleep_time = random.randint(5, 9)

print(f"暂停 {sleep_time} 秒...")

time.sleep(sleep_time)

# 更新页码

page_number += 2

print(f"数据爬取完成,文件已保存至:{file_path}")

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Dance with GenAI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档