簡體   English   中英

Python requests.get 返回亂碼

[英]Python requests.get returns Gibberish

我正在嘗試抓取以下網址:

link='https://www.opensubtitles.org/en/subtitleserve/sub/6646133'

當我做

html = requests.get(link)

它返回

html.content

亂碼(從b'PK\\x03\\x04\\x14\\x00\\x00\\x00\\x08\\x00z\\x8c8Q\\xd5H\\xc5\\xd7\\xaf7\\x00\\x00\\xdf\\x95\\x00\\x00^\\x00\\x00\\x00 ...)

為什么我沒有得到清晰的文本?

您可以使用zipfile解壓縮它,然后檢查文件名。 如果您有興趣提取 srt 文件,以下將獲得內容:

import requests, zipfile, io

r = requests.get("https://www.opensubtitles.org/en/subtitleserve/sub/6646133",
    headers = {
        "referer": "https://www.opensubtitles.org/en/subtitles/6646133/america-s-got-talent-audition-1-en"
})
z = zipfile.ZipFile(io.BytesIO(r.content))
filenames = z.namelist()
print(filenames)
srt_files = [t for t in filenames if t.endswith(".srt")]
for t in srt_files:
    content = z.read(t)
    print(content)

在 repl.it 上運行它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM