ChatGPT:翻译CURL命令post请求到 python代码

作者: cnpim CNPIM 2023年06月09日

问1:

curl "https://api.m.afreecatv.com/station/video/a/view" ^  -H "authority: api.m.afreecatv.com" ^  -H "accept: application/json, text/plain, */*" ^  -H "accept-language: zh-CN,zh;q=0.9" ^  -H "content-type: application/x-www-form-urlencoded" ^  -H "cookie: _au=421a81e0bac4870d1cfd2b24d5d08801; _ga_6HE3866SMQ=GS1.1.1673589888.1.0.1673589890.58.0.0; _ga=GA1.1.1054926493.1673589844; AbroadChk=OK; _au3rd=421a81e0bac4870d1cfd2b24d5d08801; AbroadVod=OK; _ausb=0xf5e895c0; _ausa=0x5d887b57; _ga_9PY781H32Y=GS1.1.1676538738.8.1.1676540865.32.0.0" ^  -H "origin: https://vod.afreecatv.com" ^  -H "referer: https://vod.afreecatv.com/player/98263725/embed?autoPlay=true&mutePlay=true" ^  -H "sec-ch-ua: ^^"Chromium^^";v=^^"110^^", ^^"Not A(Brand^^";v=^^"24^^", ^^"Google Chrome^^";v=^^"110^^"" ^  -H "sec-ch-ua-mobile: ?0" ^  -H "sec-ch-ua-platform: ^^"Windows^^"" ^  -H "sec-fetch-dest: empty" ^  -H "sec-fetch-mode: cors" ^  -H "sec-fetch-site: same-site" ^  -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" ^  --data-raw "nTitleNo=98263725&nApiLevel=10" ^  --compressed翻译成python代码

答1:

import requestsurl = 'https://api.m.afreecatv.com/station/video/a/view'headers = {    'authority': 'api.m.afreecatv.com',    'accept': 'application/json, text/plain, */*',    'accept-language': 'zh-CN,zh;q=0.9',    'content-type': 'application/x-www-form-urlencoded',    'cookie': '_au=421a81e0bac4870d1cfd2b34d5d08801; _ga_6HE3866SMQ=GS1.1.1673589888.1.0.1673589890.58.0.0; _ga=GA1.1.1054926493.1673589844; AbroadChk=OK; _au3rd=421a81e0bac4870d1cfd2b24d5d08801; AbroadVod=OK; _ausb=0xf5e895c0; _ausa=0x5d887b57; _ga_9PY781H32Y=GS1.1.1676538738.8.1.1676540865.32.0.0',    'origin': 'https://vod.afreecatv.com',    'referer': 'https://vod.afreecatv.com/player/98263725/embed?autoPlay=true&mutePlay=true',    'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',    'sec-ch-ua-mobile': '?0',    'sec-ch-ua-platform': 'Windows',    'sec-fetch-dest': 'empty',    'sec-fetch-mode': 'cors',    'sec-fetch-site': 'same-site',    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}data = {  'nTitleNo': '98263725',  'nApiLevel': '10'}response = requests.post(url, headers=headers, data=data)print(response.text)#在上面的代码中,我们使用 requests 库发送了一个 POST 请求到 https://api.m.afreecatv.com/station/video/a/view。headers 变量设置了请求头信息。data 变量设置了要发送的数据。我们使用 requests.post() 方法发送请求,并使用 text 属性获取响应内容。

问2:

使用urllib

答2:

import urllib.parseimport urllib.requesturl = 'https://api.m.afreecatv.com/station/video/a/view'headers = {    'authority': 'api.m.afreecatv.com',    'accept': 'application/json, text/plain, */*',    'accept-language': 'zh-CN,zh;q=0.9',    'content-type': 'application/x-www-form-urlencoded',    'cookie': '_au=421a81e0bac4870d1cfd2b34d5d08801; _ga_6HE3866SMQ=GS1.1.1673589888.1.0.1673589890.58.0.0; _ga=GA1.1.1054926493.1673589844; AbroadChk=OK; _au3rd=421a81e0bac4870d1cfd2b24d5d08801; AbroadVod=OK; _ausb=0xf5e895c0; _ausa=0x5d887b57; _ga_9PY781H32Y=GS1.1.1676538738.8.1.1676540865.32.0.0',    'origin': 'https://vod.afreecatv.com',    'referer': 'https://vod.afreecatv.com/player/98263725/embed?autoPlay=true&mutePlay=true',    'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',    'sec-ch-ua-mobile': '?0',    'sec-ch-ua-platform': 'Windows',    'sec-fetch-dest': 'empty',    'sec-fetch-mode': 'cors',    'sec-fetch-site': 'same-site',    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}data = {  'nTitleNo': '98263725',  'nApiLevel': '10'}data = urllib.parse.urlencode(data).encode('utf-8')req = urllib.request.Request(url, data, headers)with urllib.request.urlopen(req) as f:    response = f.read().decode('utf-8')print(response)#在上面的代码中,我们使用 urllib 库发送了一个 POST 请求到 https://api.m.afreecatv.com/station/video/a/view。#headers 变量设置了请求头信息。data 变量设置了要发送的数据,我们使用 urllib.parse.urlencode() 方法将数据编码#成 application/x-www-form-urlencoded 格式。然后使用 urllib.request.Request() 创建一个请求对象,并将该对象传递#给 urllib.request.urlopen() 方法发送请求。最后使用 read() 方法获取响应内容。

问3:

[{'label': '自动', 'resolution': '', 'bitrate': 'k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/smil:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1.smil/playlist.m3u8', 'name': 'adaptive'}, {'label': '原清晰度', 'resolution': '1920x1080', 'bitrate': '6000k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/mp4:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1.mp4/playlist.m3u8', 'name': 'original'}, {'label': '高清晰度(720p)', 'resolution': '1280x720', 'bitrate': '2000k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/mp4:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1_2000k.mp4/playlist.m3u8', 'name': 'hd2k'}, {'label': '普通(540p)', 'resolution': '960x540', 'bitrate': '1000k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/mp4:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1_1000k.mp4/playlist.m3u8', 'name': 'hd'}]根据resolution从大到小排序

答3:

lst = [{'label': '自动', 'resolution': '', 'bitrate': 'k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/smil:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1.smil/playlist.m3u8', 'name': 'adaptive'}, {'label': '原清晰度', 'resolution': '1920x1080', 'bitrate': '6000k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/mp4:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1.mp4/playlist.m3u8', 'name': 'original'}, {'label': '高清晰度(720p)', 'resolution': '1280x720', 'bitrate': '2000k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/mp4:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1_2000k.mp4/playlist.m3u8', 'name': 'hd2k'}, {'label': '普通(540p)', 'resolution': '960x540', 'bitrate': '1000k', 'file': 'https://vod-archive-global-cdn-z02.afreecatv.com/v101/video/_definst_/mp4:vod/20230202/152/244828152/REGL_42DC8EE8_244828152_1_1000k.mp4/playlist.m3u8', 'name': 'hd'}]sorted_lst = sorted(lst, key=lambda x: int(x['resolution'].split('x')[0]), reverse=True)print(sorted_lst)


本文阅读量:

声明:本信息来源于网络,仅用于学习和技术交流,如有侵权或其他问题,请联系本站处理。

技术支持:CNPIM.COM