爬虫中对Request payload传参方式的处理

时间:2024-04-08 13:04:31

例:

图片右下角就是Request payload

爬虫中对Request payload传参方式的处理

其中,Request payload以json格式进行传参,所以在构造模拟请求的时候,也需要用json格式

代码:

import requests
import json
import jsonpath
import time
from lxml import etree
from urllib.parse import urlencode
​
class ZhongXinYouyu(object):
    def __init__(self,base_url=None):
        self.index_url = "https://mi.creditcard.ecitic.com/citiccard/yp-market-pc/goodsclass/queryRecord?&rtnUrl=https://mi.creditcard.ecitic.com/citiccard/yp-market-pc/search.html?classifyId=ea2e539117724781&className=%E6%89%8B%E6%9C%BA"
        self.session = requests.session()
        self.payload_header = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Length': '127',
    'Content-Type': 'application/json;charset=UTF-8',
    'Host': 'mi.creditcard.ecitic.com',
    'X-Requested-With': 'XMLHttpRequest',
}
    
    
    def index_html(self):
        """
        解析主页,获取分类信息的json文件
        :return: json_data
        """
        payload_data ={
            "bizChannel":"11",
        }
        payload_data = json.dumps(payload_data )    #json化
​
        response = self.session.post(url=self.index_url,data=payload_data,headers=self.payload_header)
        response.encoding = response.apparent_encoding  # 页面编码
        if response.status_code == 200:
            json_data = json.loads(response.text)
            # with open(r"documents\index_json.txt",'w+',encoding='utf-8') as f:
            #     f.write(json.dumps(json_data))
            #     f.close()
            return json_data
        else:
            print("请求失败")
    def run(self):
        self.index_html()
        
if __name__ == '__main__':
    runs = ZhongXinYouyu()
    runs.run()