requests

创建日期:2024-06-21
更新日期:2025-01-01

官方网站:Requests: HTTP for Humans™ — Requests 2.32.3 documentation (python-requests.org)

常见问题

1、urllib3.exceptions.IncompleteRead: IncompleteRead(267143787 bytes read, 3677548538 more expected)。

这是由于HTTP 1.0和HTTP 1.1不同导致的,可以用pycurl使用HTTP1.0请求。

import pycurl
import certifi
from io import BytesIO

def download(url, file_path):
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.HTTP_VERSION, c.CURL_HTTP_VERSION_1_0)
    c.setopt(c.URL, url.encode("utf-8"))
    c.setopt(c.WRITEDATA, buffer)
    c.setopt(c.CAINFO, certifi.where())
    c.perform()
    c.close()
    body = buffer.getvalue()
    file = open(file_path, "wb")
    file.write(body)
    file.close()