Curl 命令成功但 python 请求得到 403 Forbidden

Posted

技术标签:

【中文标题】Curl 命令成功但 python 请求得到 403 Forbidden【英文标题】:Curl command succeeded but python requests get 403 Forbidden 【发布时间】:2018-07-15 18:06:04 【问题描述】:

我尝试从this website 获取数据。 我得到 cUrl 命令:

curl 'http://mayaapi.tase.co.il/api/report/filter?logo=0' -H 'Origin: http://maya.tase.co.il' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/63.0.3239.132 Safari/537.36' -H 'X-Maya-With: allow' -H 'Content-Type: application/json;charset=UTF-8' -H 'Referer: http://maya.tase.co.il/reports/fund?q=%7B%22DateFrom%22:%222016-12-31T22:00:00.000Z%22,%22DateTo%22:%222018-02-03T22:00:00.000Z%22,%22QOpt%22:1,%22Page%22:1,%22Q%22:%22%D7%93%D7%95%D7%97%20%D7%97%D7%95%D7%93%D7%A9%D7%99%22,%22events%22:%5B%5D,%22subevents%22:%5B%5D%7D' --data-binary $'"Page":1,"GroupData":[],"DateFrom":"2016-12-31T22:00:00.000Z","DateTo":"2018-02-03T22:00:00.000Z","IsBreakingAnnouncement":false,"IsForTaseMember":false,"IsSpecificFund":false,"Q":"\u05d3\u05d5\u05d7 \u05d7\u05d5\u05d3\u05e9\u05d9","QOpt":1,"ViewPage":4' --compressed

当我从命令行执行此操作时,我会收到响应。但是当我尝试在 python 中使用requests 时,我得到403 Forbidden

python代码:

response = requests.post(
                "http://mayaapi.tase.co.il/api/report/filter?logo=0",
                data=
                    "Page": 1,
                    "GroupData": [],
                    "DateFrom": "2016-12-31T22:00:00.000Z",
                    "DateTo": "2018-02-03T22:00:00.000Z",
                    "IsBreakingAnnouncement": False,
                    "IsForTaseMember": False,
                    "IsSpecificFund": False,
                    "Q": "דוח חודשי",
                    "QOpt": 1,
                    "ViewPage": 4,
                ,
                headers=
                    "Content-Type": "application/json;charset=UTF-8",
                    "Origin": "http://maya.tase.co.il",
                    "Referer": "http://maya.tase.co.il/reports/fund?q=%7B%22DateFrom%22:%222016-12-31T22:00:00.000Z%22,%22DateTo%22:%222018-02-03T22:00:00.000Z%22,%22QOpt%22:1,%22Page%22:1,%22Q%22:%22%D7%93%D7%95%D7%97%20%D7%97%D7%95%D7%93%D7%A9%D7%99%22,%22events%22:%5B%5D,%22subevents%22:%5B%5D%7D",
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
                    "X-Maya-With": "allow",
                ,
                cookies=
            )
            print('req1', response)

我尝试了任何我能想到的方法,但都没有成功。

【问题讨论】:

试试json参数 【参考方案1】:

请求未成功,因为 POST 有效负载(data 参数的字典)需要包装在 json 中。

response = requests.post(
    "http://mayaapi.tase.co.il/api/report/filter?logo=0",
    data=json.dumps(
        "Page": 1,
        "GroupData": [],
        "DateFrom": "2016-12-31T22:00:00.000Z",
        "DateTo": "2018-02-03T22:00:00.000Z",
        "IsBreakingAnnouncement": False,
        "IsForTaseMember": False,
        "IsSpecificFund": False,
        "Q": "דוח חודשי",
        "QOpt": 1,
        "ViewPage": 4,
    ),
    headers=
        "Content-Type": "application/json;charset=UTF-8",
        "Origin": "http://maya.tase.co.il",
        "Referer": "http://maya.tase.co.il/reports/fund?q=%7B%22DateFrom%22:%222016-12-31T22:00:00.000Z%22,%22DateTo%22:%222018-02-03T22:00:00.000Z%22,%22QOpt%22:1,%22Page%22:1,%22Q%22:%22%D7%93%D7%95%D7%97%20%D7%97%D7%95%D7%93%D7%A9%D7%99%22,%22events%22:%5B%5D,%22subevents%22:%5B%5D%7D",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
        "X-Maya-With": "allow",
    ,
    cookies=
)
print('req1', response)

【讨论】:

以上是关于Curl 命令成功但 python 请求得到 403 Forbidden的主要内容,如果未能解决你的问题,请参考以下文章

让请求在 curl 中工作,但不在 Python 中

怎么判断curl调用接口返回成功:响应码

python - 使用 curl 发送请求时,Flask 可以处理会话吗? [复制]

Python 请求 - 上传 Zip 文件

将 cURL 命令转换为 python 请求

Chrome etag 总是显示 200 ok 但 curl 得到 304 未修改