Python请求POST方法响应中的意外内容

2024-04-27 22:58:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Python请求在网页https://www.meo.pt/tv/canais-programacao/guia-tv上获取EPG数据。我经常使用这个模块,但主要是GET方法。但是,此请求正在使用POST。每次向下滚动页面时,都会向下面的API发送一个请求,使用这些参数将其他程序数据加载到页面:

import requests

#post request
url = 'https://www.meo.pt/_layouts/15/Ptsi.Isites.GridTv/GridTvMng.asmx/getProgramsFromChannels'

headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
'Connection': 'keep-alive',
'Content-Length': '214',
'Content-type': 'application/json; charset=UTF-8',
'Host': 'www.meo.pt',
'Origin': 'https://www.meo.pt',
'Referer': 'https://www.meo.pt/tv/canais-programacao/guia-tv',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'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/89.0.4389.114 Safari/537.36',
'X-KL-Ajax-Request': 'Ajax_Request'
}


data = {"service":"channelsguide",
"channels":["LVTV","TOROTV","CAÇAP","CAÇAV","RTPACRS","CLUBB","MCM T","TRACE","24KITC","E!"],
"dateStart":"2021-04-20T23:00:00.000Z",
"dateEnd":"2021-04-21T23:00:00.000Z",
"accountID":""}


r = requests.post(url=url, headers=headers, data=data)
print(r.text)

我已经尝试了这个请求,无论使用了还是没有使用头,因为我不知道POST请求是否需要它们。但是,这两个选项都没有返回我所期望的结果,即一个包含这些通道的程序数据的JSON对象

我做错了什么


Tags: 数据httpspturldatawwwsecfetch
3条回答

在请求函数中考虑使用^ {CD1>}参数而不是^ {CD2>}。json参数将您的正文解析为JSON格式,同时data您正在发送一个原始字典

data = {"service":"channelsguide",
"channels":["LVTV","TOROTV","CAÇAP","CAÇAV","RTPACRS","CLUBB","MCM T","TRACE","24KITC","E!"],
"dateStart":"2021-04-20T23:00:00.000Z",
"dateEnd":"2021-04-21T23:00:00.000Z",
"accountID":""}


r = requests.post(url=url, headers=headers, json=data)

如果您想继续使用data参数,您应该将数据字典解析为JSON以发送正确的正文格式

您可以使用以下示例将json数据发布到API Url:

import json
import requests


url = "https://www.meo.pt/_layouts/15/Ptsi.Isites.GridTv/GridTvMng.asmx/getProgramsFromChannels"

payload = {
    "accountID": "",
    "channels": [
        "SCPHD",
        "EURHD",
        "EURS2HD",
        "DISNY",
        "CART",
        "BIGGS",
        "SICK",
        "NICKELO",
        "DISNYJ",
        "PANDA",
    ],
    "dateEnd": "2021-04-21T22:00:00.000Z",
    "dateStart": "2021-04-20T22:00:00.000Z",
    "service": "channelsguide",
}

data = requests.post(url, json=payload).json()

# pretty print the data:    
print(json.dumps(data, indent=4))

印刷品:

{
    "d": {
        "__type": "Ptsi.Isites.GridTv.CanaisService.GridTV",
        "ExtensionData": {},
        "services": [],
        "channels": [
            {
                "__type": "Ptsi.Isites.GridTv.CanaisService.Channels",
                "ExtensionData": {},
                "id": 36,
                "name": "SPORTING TV HD",
                "sigla": "SCPHD",
                "friendlyUrlName": "Sporting_TV_HD",
                "url": "https://meogo.meo.pt/direto?canalUrl=Sporting_TV_HD",
                "meogo": true,
                "logo": "https://www.meo.pt/PublishingImages/canais/sporting-tv-hd.png",
                "isAdult": false,
                "categories": [
                    {
                        "ExtensionData": {},
                        "id": 2,
                        "name": "Desporto"
                    }
                ],

...

If you want to keep using data argument you should parse data dictionary to JSON to send the correct body format.

您应该将标题设置为:

headers = {
    'Content-type': 'application/json'
}

完整代码为:

import json
import requests


url = "https://www.meo.pt/_layouts/15/Ptsi.Isites.GridTv/GridTvMng.asmx/getProgramsFromChannels"

headers = {
    'Content-type': 'application/json'
}

payload = {
    "accountID": "",
    "channels": [
        "SCPHD",
        "EURHD",
        "EURS2HD",
        "DISNY",
        "CART",
        "BIGGS",
        "SICK",
        "NICKELO",
        "DISNYJ",
        "PANDA",
    ],
    "dateEnd": "2021-04-21T22:00:00.000Z",
    "dateStart": "2021-04-20T22:00:00.000Z",
    "service": "channelsguide",
}

resp = requests.post(url,headers=headers,data=json.dumps(payload))
print(resp.text)

相关问题 更多 >