gpt4 book ai didi

python - 修复 Python requests.exception.InvalidURL : Invalid percent-escape sequence 'u2' error?

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:21 26 4
gpt4 key购买 nike

我的 python 应用程序接受使用 Javascript escape 函数处理的 url,该函数在我的 python 代码中使用 urllib.unquote 进行了未转义,这适用于大多数 url 但如果文件名(也是 url 的一部分)中包含 & 并出现以下错误,则会失败。

requests.exception.InvalidURL:无效的百分比转义序列“u2”

编辑:有错误的示例代码

import urllib,requests
url = "https%3A//r20---sn-cvh7zn76.googlevideo.com/videoplayback%3Fipbits%3D0%26ms%3Dau%26fexp%3D931328%2C931946%2C934804%2C914004%2C931818%2C937417%2C913434%2C923328%2C936916%2C934022%2C936923%26sparams%3Dclen%2Cdur%2Cgir%2Cid%2Cip%2Cipbits%2Citag%2Clmt%2Crequiressl%2Csource%2Cupn%2Cexpire%26source%3Dyoutube%26mv%3Dm%26dur%3D278.593%26id%3Do-AOxIEhMchATdRjU99Gveow8reeBWtxFaqwpWifXC9KwS%26expire%3D1397662367%26clen%3D4425254%26sver%3D3%26signature%3D9A0CFEC5F59C2C7FC35A8CF87491F4E7F9683C59.C46B3A3602A20611C73CC4228FCB8B287034F52D%26mt%3D1397639060%26upn%3DBknKrHPqCCw%26gir%3Dyes%26itag%3D140%26key%3Dyt5%26ip%3D117.200.252.163%26lmt%3D1386126879207085%26requiressl%3Dyes%26ratebypass%3Dyes%26title%3DHum%20Tuhmaray%20hain%20%u2022%20SRK%20_%20Madhuri%20Dixit%20%u2022%20HD%201080p%20%u2022%20Hindi%20%u2022%20Bollywood%20Songs"
url = urllib.unquote_plus(url).decode('utf-8')
resp = requests.head(url, verify=False, allow_redirects=True)
print resp

字符串 Hum Tuhmaray hain • SRK & Madhuri Dixit • 导致问题,问题似乎是 url 中的 unicode 项目符号字符 %u2022

最佳答案

在 javascript 代码中使用 encodeURIComponent() 而不是 escape() 解决了这个问题。

谢谢

关于python - 修复 Python requests.exception.InvalidURL : Invalid percent-escape sequence 'u2' error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23105164/

26 4 0