当前位置: 首页 > article >正文

requests库如何处理 - POST请求常见的两种请求体格式:表单格式JSON格式

目录:

  • 每篇前言:
  • 一、POST请求的两种常见请求体格式详解
    • 1. 表单格式(form-encoded) - 举例:福州搜索
      • 示例代码(表单数据):
    • 2. JSON格式 - 举例:CSDN搜索
      • 示例代码(JSON格式):
  • 二、实战讲解
    • 1. 表单格式(form-encoded) - 举例:福州搜索
      • ①data参数传递字典
      • ②data参数直接传递字符串
    • 2. JSON格式 - 举例:CSDN搜索
      • ①json参数传递字典
      • ②json参数传递字符串

每篇前言:

  • 🏆🏆作者介绍:【孤寒者】—CSDN全栈领域优质创作者、HDZ核心组成员、华为云享专家Python全栈领域博主、CSDN原力计划作者

  • 🔥🔥本文已同时收录于:《Python全栈系列教程》和《爬虫从入门到精通系列教程》两个专栏
  • 🔥🔥热门专栏推荐:《Django框架从入门到实战》、《爬虫从入门到精通系列教程》、《爬虫高级》、《前端系列教程》、《tornado一条龙+一个完整版项目》。
  • 📝​📝本专栏面向广大程序猿,为的是大家都做到Python从入门到精通,同时穿插有很多很多习题,巩固学习。
  • 🎉🎉订阅专栏后可私聊进一千多人Python全栈交流群(手把手教学,问题解答); 进群可领取Python全栈教程视频 + 多得数不过来的计算机书籍:基础、Web、爬虫、数据分析、可视化、机器学习、深度学习、人工智能、算法、面试题等。
  • 🚀🚀加入我一起学习进步,一个人可以走的很快,一群人才能走的更远!

在这里插入图片描述

其实这个内容在文章《两万字博文教你python爬虫requests库【详解篇】》里讲过,但是过于简略,所以本篇文章来详细讲解一下!

一、POST请求的两种常见请求体格式详解

在发送POST请求的时候,常见的请求体格式一般有两种:form表单格式和JSON格式。

1. 表单格式(form-encoded) - 举例:福州搜索

  • 【谷歌浏览器抓包Form Data】
  • 格式说明:在这种格式中,数据被编码为一串键值对,形如 key1=value1&key2=value2。这种格式通常用于HTML表单提交。
  • requests库的处理:使用data参数并传递一个字典,requests库会自动将字典编码为application/x-www-form-urlencoded格式。服务器将接收到编码后的字符串,如图中的name=wupei&age=18&size=99
  • HTTP头Content-Type被自动设置为application/x-www-form-urlencoded; charset=UTF-8,这告诉服务器正在发送URL编码的表单数据。

示例代码(表单数据):

# -*- coding: utf-8 -*-
# @Time    : 2024/11/10 66:66
# @Author  : GuHanZhe
# @File    : post_form.py
import requests

url = "http://example.com/post"
data = {
    "name": "GuHanZheCoder",
    "age": 18,
    "size": 25
}

response = requests.post(url, data=data)

2. JSON格式 - 举例:CSDN搜索

  • 谷歌浏览器抓包Request Payload
  • 格式说明:在这种格式中,数据被编码为JSON字符串,这是一种轻量级的数据交换格式,能够表示复杂的结构化数据。
  • requests库的处理:使用json参数时,你可以直接传递一个Python字典(或列表),requests将自动将其序列化为JSON格式的字符串并发送。这是处理RESTful API时常用的方法。
  • HTTP头Content-Type被自动设置为application/json; charset=utf-8,告诉服务器正在发送JSON格式的数据。

示例代码(JSON格式):

# -*- coding: utf-8 -*-
# @Time    : 2024/11/10 66:66
# @Author  : GuHanZhe
# @File    : post_json.py
import requests

url = "http://example.com/post"
json_data = {
    "name": "GuHanZheCoder",
    "age": 18,
    "size": 25
}

response = requests.post(url, json=json_data)

总结

  • 使用data参数时,数据可以是字典形式或字符串,主要用于表单提交或其他类型的数据(如XML)。
  • 使用json参数时,数据必须是可以被序列化为JSON格式的Python对象(如字典或列表),主要用于提交JSON数据。

二、实战讲解

1. 表单格式(form-encoded) - 举例:福州搜索

  • 当使用data参数传递字典时,requests库会自动将其转换为URL编码的字符串【形如:key1=value1&key2=value2】,并设置Content-Type头为application/x-www-form-urlencoded
  • 但如果直接传递字符串,requests会将这个字符串原样发送到服务器。在这种情况下,requests不会自动添加Content-Type头。如果您发送的是JSON字符串或其他特定格式的数据,您需要手动指定Content-Type头,来告诉requests库解析为想要的格式。

①data参数传递字典

#! python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/10 66:66
# @Author  : GuHanZhe
# @File    : fz_crawler.py
import requests

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': 'https://www.fuzhou.gov.cn',
    'Pragma': 'no-cache',
    '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/130.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'time': '1731251506475',
}

data = {
    'isCollapse': '',
    'siteType': '1',
    'typeQueryJsonToMap': '',
    'pubOrgType': '',
    'jiGuanList': '',
    'siteCode': '',
    'zhuTiIdList': '',
    'isCrdept': '',
    'mainSiteId': '402849946077df37016077eea95e002f',
    'siteId': '402849946077df37016077eea95e002f',
    'depSiteId': '402849946077df37016077eea95e002f',
    'type': '0',
    'page': '1',
    'rows': '20',
    'historyId': 'ff808081930a6bc8019316a297e22a01',
    'sourceType': 'SSP_ZHSS',
    'isChange': '0',
    'fullKey': 'N',
    'wbServiceType': '13',
    'fileType': '',
    'feaTypeName': '',
    'fileNo': '',
    'pubOrg': '',
    'zfgbPubOrg': '',
    'themeType': '',
    'searchTime': '',
    'sortFiled': 'RELEVANCE',
    'searchFiled': '',
    'dirUseLevel': '',
    'issueYear': '',
    'publishYear': '',
    'issueMonth': '',
    'allKey': '',
    'fullWord': '',
    'oneKey': '',
    'notKey': '',
    'totalIssue': '',
    'chnlName': '',
    'zfgbTitle': '',
    'zfgbContent': '',
    'bsDeptId': '',
    'siteName': '',
    'searchToken': '',
    'keyWord': '公积金基数',
    'isProvince': '',
}

response = requests.post('https://www.fuzhou.gov.cn/ssp/search/api/search', params=params, headers=headers, data=data)
print(response.text)
print(response.request.headers)

通过response.request.headers查看发送的请求头,会发现里面确实加上了相应的请求头:
在这里插入图片描述

②data参数直接传递字符串

此时如果我们不手动指定请求头Content-Type设置为application/x-www-form-urlencoded; charset=UTF-8,下面这个爬虫就会将data的值,这个字符串直接发给服务器,那么服务器就无法正确解析这个请求:

#! python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/10 66:66
# @Author  : GuHanZhe
# @File    : fz_crawler.py
import requests

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': 'https://www.fuzhou.gov.cn',
    'Pragma': 'no-cache',
    '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/130.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'time': '1731251506475',
}
data = 'isCollapse=&siteType=1&typeQueryJsonToMap=&pubOrgType=&jiGuanList=&siteCode=&zhuTiIdList=&isCrdept=&mainSiteId=402849946077df37016077eea95e002f&siteId=402849946077df37016077eea95e002f&depSiteId=402849946077df37016077eea95e002f&type=0&page=1&rows=20&historyId=ff808081930a6bc8019316a297e22a01&sourceType=SSP_ZHSS&isChange=0&fullKey=N&wbServiceType=13&fileType=&feaTypeName=&fileNo=&pubOrg=&zfgbPubOrg=&themeType=&searchTime=&sortFiled=RELEVANCE&searchFiled=&dirUseLevel=&issueYear=&publishYear=&issueMonth=&allKey=&fullWord=&oneKey=&notKey=&totalIssue=&chnlName=&zfgbTitle=&zfgbContent=&bsDeptId=&siteName=&searchToken=&keyWord=%E5%85%AC%E7%A7%AF%E9%87%91%E5%9F%BA%E6%95%B0&isProvince='

response = requests.post('https://www.fuzhou.gov.cn/ssp/search/api/search', params=params, headers=headers, data=data)
print(response.text)
print(response.request.headers)

在这里插入图片描述

但是,如果我们手动设置了请求头Content-Typeapplication/x-www-form-urlencoded; charset=UTF-8,就正常了:
在这里插入图片描述

2. JSON格式 - 举例:CSDN搜索

  • 当使用json参数传递一个可序列化的数据(通常是一个字典或列表)时,requests库会自动将其序列化成 JSON 字符串,并自动设置 HTTP 头的 Content-Typeapplication/json
  • 当使用json参数传递一个字符串时,requests库会抛出一个异常,因为它期望输入是一个可以被序列化为JSON的Python对象(如字典或列表)

①json参数传递字典

#! python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/10 66:66
# @Author  : GuHanZhe
# @File    : CSDN_crawler.py
import requests


headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': 'https://so.csdn.net',
    'Pragma': 'no-cache',
    'Referer': 'https://so.csdn.net/so/search?spm=1000.2115.3001.4498&q=%E5%AD%A4%E5%AF%92%E8%80%85&t=&u=',
    '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/130.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

json_data = [
    {
        'title': '2024年【大学生Python学习】社区&amp;&amp;小博主【<em>孤寒者</em>】的年度总结(1)-CS...',
        'linkUrl': 'https://blog.csdn.net/2401_84123557/article/details/138093620',
        'abstract': '希望在奔赴未来的路上,我们有着生生不息的热爱,如星灿烂,如风自由 第二部分—CSDN博主【<em>孤寒者</em>】的年度总结: === 记录第一次上热榜: 依稀记得,第一次上CSDN热榜时的场景: 那天下午我在宿舍部署自己的一个小项目,搞完之后感觉要写个小短文记录一下新手如何入坑云服务器(我习惯学完什么都写笔记记录下来,“好...',
        'image': '',
        'dispUrl': 'blog.csdn.net/2401_84123557/article/d...',
        'dispTime': '2024-11-4',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/2401_84123557/article/details/138093620',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/2401_84123557/article/details/138093620?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-138093620.142^v100^pc_search_result_base9',
            'index': '0',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '<em>孤寒者</em>',
        'linkUrl': 'https://gu-han-zhe.blog.csdn.net/',
        'abstract': '<em>孤寒者</em>擅长Python全栈系列教程,Django框架从入门到实战,爬虫必备前端技术教程,等方面的知识,孤寒者关注算法,python,django,tornado,flask,数据结构领域.',
        'image': '',
        'dispUrl': 'gu-han-zhe.blog.csdn.net/...',
        'dispTime': '2024-11-8',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://gu-han-zhe.blog.csdn.net/',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://gu-han-zhe.blog.csdn.net/?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-gu-han-zhe.blog.csdn.net.142^v100^pc_search_result_base9',
            'index': '1',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '三大数据库深入讲解_<em>孤寒者</em>的博客',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/category_9734109',
        'abstract': '三大数据库之Mysql,Mongodb,Redis深入讲解; Python操作三大数据库&amp;Python对三大数据库常用方法封装讲解;三大数据库可视化工具使用。关注数:35 文章数:7 文章阅读量:137429 文章收藏量:524 作者: <em>孤寒者</em>  HDZ核心组成员、华为云享专家、CSDN原力计划作者、CSDN全栈领域优质创作者。专注分享Python领域原创系列文章,如...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/category_97...',
        'dispTime': '2024-10-31',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/category_9734109',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/category_9734109?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-category_9734109.142^v100^pc_search_result_base9',
            'index': '3',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '<em>孤寒者</em>',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/',
        'abstract': '<em>孤寒者</em>的博客 深入浅出的讲解Python基础知识&amp;爬虫初阶及进阶&amp;主流Web框架(Django等) 博客(389) 资源(28) 收藏 关注 只看原创 排序: 按最后发布时间 按访问量 RSS订阅 原创《Python全栈系列教程》目录 《Python全栈基础教程》目录导读:一、Python基础部分;二、Python常用模块部分;三、Python实战部分;四、Python习题部...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/...',
        'dispTime': '2024-10-12',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-qq_44907926.142^v100^pc_search_result_base9',
            'index': '4',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '三大数据库深入讲解_<em>孤寒者</em>的博客',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/category_9734109.html',
        'abstract': '三大数据库之Mysql,Mongodb,Redis深入讲解; Python操作三大数据库&amp;Python对三大数据库常用方法封装讲解;三大数据库可视化工具使用。关注数:35 文章数:7 文章阅读量:137401 文章收藏量:523 作者: <em>孤寒者</em>  HDZ核心组成员、华为云享专家、CSDN原力计划作者、CSDN全栈领域优质创作者。专注分享Python领域原创系列文章,如...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/category_97...',
        'dispTime': '2024-10-22',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/category_9734109.html',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/category_9734109.html?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-5-category_9734109.html.142^v100^pc_search_result_base9',
            'index': '5',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '【Mongodb数据库】的介绍和安装(windows下和ubuntu16.04下安装及启动...',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/article/details/127492667',
        'abstract': 'name:&quot;<em>孤寒者</em>&quot;,age:18, address: {city:&quot;河南&quot;, country:&quot;china&quot;} } 1 2 3 4 5 6 7 8 Mongodb既可用于S端存储数据,即server;也可供C端操作处理(如查询等)数据,即client。 SQL和NoSQL的主要区别: 在SQL中层级关系:数据库 &gt; 表 &gt; 数据 ...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/article/det...',
        'dispTime': '2024-10-20',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/article/details/127492667',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/article/details/127492667?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-6-127492667.142^v100^pc_search_result_base9',
            'index': '6',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '万字博文教你python爬虫pyquery库【详解篇】_pyuirwtuwqwrrwrrtrwrweee...',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/article/details/122231537',
        'abstract': '🏆🏆作者介绍:【<em>孤寒者</em>】—CSDN全栈领域优质创作者、HDZ核心组成员、华为云享专家Python全栈领域博主、CSDN原力计划作者 🔥🔥本文已收录于爬虫从入门到精通系列教程:《爬虫从入门到精通系列教程》 🔥🔥热门专栏推荐:了解本专栏 订阅专栏 解锁全文 孤寒者 关注 20 17 觉得还不错?  一键收藏 15 专栏目录 ...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/article/det...',
        'dispTime': '2024-10-31',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/article/details/122231537',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/article/details/122231537?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-7-122231537.142^v100^pc_search_result_base9',
            'index': '7',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    }
]

response = requests.post('https://so.csdn.net/api/v1/supplement_baidu_js', headers=headers, json=json_data)
print(response.text)
print(response.request.headers)

通过response.request.headers查看发送的请求头,会发现里面确实加上了相应的请求头:

在这里插入图片描述

②json参数传递字符串

这是不被支持的,如果你仍这样错误的使用,爬虫的响应可能会让你摸不着头脑,比如CSDN这个接口的响应就会变成维护页面,但实际上这个接口是正常的!

在这里插入图片描述
在这里插入图片描述

如果你非要使用一个字符串,那么就要使用data参数了!并手动指定请求头:

#! python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/10 66:66
# @Author  : GuHanZhe
# @File    : CSDN_crawler.py
import requests
import json

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': 'https://so.csdn.net',
    'Pragma': 'no-cache',
    'Referer': 'https://so.csdn.net/so/search?spm=1000.2115.3001.4498&q=%E5%AD%A4%E5%AF%92%E8%80%85&t=&u=',
    '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/130.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'content-type': 'application/json'
}

json_data = [
    {
        'title': '2024年【大学生Python学习】社区&amp;&amp;小博主【<em>孤寒者</em>】的年度总结(1)-CS...',
        'linkUrl': 'https://blog.csdn.net/2401_84123557/article/details/138093620',
        'abstract': '希望在奔赴未来的路上,我们有着生生不息的热爱,如星灿烂,如风自由 第二部分—CSDN博主【<em>孤寒者</em>】的年度总结: === 记录第一次上热榜: 依稀记得,第一次上CSDN热榜时的场景: 那天下午我在宿舍部署自己的一个小项目,搞完之后感觉要写个小短文记录一下新手如何入坑云服务器(我习惯学完什么都写笔记记录下来,“好...',
        'image': '',
        'dispUrl': 'blog.csdn.net/2401_84123557/article/d...',
        'dispTime': '2024-11-4',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/2401_84123557/article/details/138093620',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/2401_84123557/article/details/138093620?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-138093620.142^v100^pc_search_result_base9',
            'index': '0',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '<em>孤寒者</em>',
        'linkUrl': 'https://gu-han-zhe.blog.csdn.net/',
        'abstract': '<em>孤寒者</em>擅长Python全栈系列教程,Django框架从入门到实战,爬虫必备前端技术教程,等方面的知识,孤寒者关注算法,python,django,tornado,flask,数据结构领域.',
        'image': '',
        'dispUrl': 'gu-han-zhe.blog.csdn.net/...',
        'dispTime': '2024-11-8',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://gu-han-zhe.blog.csdn.net/',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://gu-han-zhe.blog.csdn.net/?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-gu-han-zhe.blog.csdn.net.142^v100^pc_search_result_base9',
            'index': '1',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '三大数据库深入讲解_<em>孤寒者</em>的博客',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/category_9734109',
        'abstract': '三大数据库之Mysql,Mongodb,Redis深入讲解; Python操作三大数据库&amp;Python对三大数据库常用方法封装讲解;三大数据库可视化工具使用。关注数:35 文章数:7 文章阅读量:137429 文章收藏量:524 作者: <em>孤寒者</em>  HDZ核心组成员、华为云享专家、CSDN原力计划作者、CSDN全栈领域优质创作者。专注分享Python领域原创系列文章,如...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/category_97...',
        'dispTime': '2024-10-31',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/category_9734109',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/category_9734109?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-category_9734109.142^v100^pc_search_result_base9',
            'index': '3',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '<em>孤寒者</em>',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/',
        'abstract': '<em>孤寒者</em>的博客 深入浅出的讲解Python基础知识&amp;爬虫初阶及进阶&amp;主流Web框架(Django等) 博客(389) 资源(28) 收藏 关注 只看原创 排序: 按最后发布时间 按访问量 RSS订阅 原创《Python全栈系列教程》目录 《Python全栈基础教程》目录导读:一、Python基础部分;二、Python常用模块部分;三、Python实战部分;四、Python习题部...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/...',
        'dispTime': '2024-10-12',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-qq_44907926.142^v100^pc_search_result_base9',
            'index': '4',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '三大数据库深入讲解_<em>孤寒者</em>的博客',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/category_9734109.html',
        'abstract': '三大数据库之Mysql,Mongodb,Redis深入讲解; Python操作三大数据库&amp;Python对三大数据库常用方法封装讲解;三大数据库可视化工具使用。关注数:35 文章数:7 文章阅读量:137401 文章收藏量:523 作者: <em>孤寒者</em>  HDZ核心组成员、华为云享专家、CSDN原力计划作者、CSDN全栈领域优质创作者。专注分享Python领域原创系列文章,如...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/category_97...',
        'dispTime': '2024-10-22',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/category_9734109.html',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/category_9734109.html?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-5-category_9734109.html.142^v100^pc_search_result_base9',
            'index': '5',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '【Mongodb数据库】的介绍和安装(windows下和ubuntu16.04下安装及启动...',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/article/details/127492667',
        'abstract': 'name:&quot;<em>孤寒者</em>&quot;,age:18, address: {city:&quot;河南&quot;, country:&quot;china&quot;} } 1 2 3 4 5 6 7 8 Mongodb既可用于S端存储数据,即server;也可供C端操作处理(如查询等)数据,即client。 SQL和NoSQL的主要区别: 在SQL中层级关系:数据库 &gt; 表 &gt; 数据 ...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/article/det...',
        'dispTime': '2024-10-20',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/article/details/127492667',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/article/details/127492667?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-6-127492667.142^v100^pc_search_result_base9',
            'index': '6',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    },
    {
        'title': '万字博文教你python爬虫pyquery库【详解篇】_pyuirwtuwqwrrwrrtrwrweee...',
        'linkUrl': 'https://blog.csdn.net/qq_44907926/article/details/122231537',
        'abstract': '🏆🏆作者介绍:【<em>孤寒者</em>】—CSDN全栈领域优质创作者、HDZ核心组成员、华为云享专家Python全栈领域博主、CSDN原力计划作者 🔥🔥本文已收录于爬虫从入门到精通系列教程:《爬虫从入门到精通系列教程》 🔥🔥热门专栏推荐:了解本专栏 订阅专栏 解锁全文 孤寒者 关注 20 17 觉得还不错?  一键收藏 15 专栏目录 ...',
        'image': '',
        'dispUrl': 'blog.csdn.net/qq_44907926/article/det...',
        'dispTime': '2024-10-31',
        'flagIcon': 'flag_icon1',
        'tip': '博客',
        'requestId': 'blogRequsetId',
        'bizId': 102,
        'type': 'blog',
        'is_baidu': True,
        'pageIndex': 1,
        'url_location': 'https://blog.csdn.net/qq_44907926/article/details/122231537',
        'report': {
            'mod': 'popu_271',
            'dest': 'https://blog.csdn.net/qq_44907926/article/details/122231537?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%AD%A4%E5%AF%92%E8%80%85&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-7-122231537.142^v100^pc_search_result_base9',
            'index': '7',
            'spm': '',
            'strategy': '2~all~sobaiduweb~default',
            'request_id': 'blogRequsetId',
            'biz_id': '102',
            'ops_request_misc': '',
            'extra': '{"fe_request_id":"1731252527574_2691_1166981"}',
        },
    }
]

json_str = json.dumps(json_data)

response = requests.post('https://so.csdn.net/api/v1/supplement_baidu_js', headers=headers, data=json_str)
print(response.text)
print(response.request.headers)

在这里插入图片描述


http://www.kler.cn/a/391434.html

相关文章:

  • 【H3C华三 】VRRP与BFD、Track联动配置案例
  • WebRTC视频 01 - 视频采集整体架构
  • 【C++课程学习】:string的模拟实现
  • 两化融合评估流程
  • GPT模型发展放缓?《The Information》称是,OpenAI专家Noam Brown称否!
  • TDesign了解及使用
  • MySQL系列:一句SQL,MySQL是怎么工作的?
  • Linux SSH私钥认证结合cpolar内网穿透安全高效远程登录指南
  • 网站小程序app怎么查有没有备案?
  • uniapp上拉刷新下拉加载
  • Codeforces Round 984 (Div. 3) (A~E)
  • Python | Leetcode Python题解之第553题最优除法
  • Java poi 模板导出Word 带图片
  • React中右击出现自定弹窗
  • 了解外呼系统线路分类,提升业务效率
  • Django框架:Form组件及参数
  • ubuntu 22.04 镜像源更换
  • 51单片机使用NRF24L01进行2.4G无线通信
  • 系统架构设计师论文:大数据Lambda架构
  • 【JAVA基础】JVM双亲委派