Download file from web in Python 3

前端 未结 9 709
星月不相逢
星月不相逢 2020-11-22 16:43

I am creating a program that will download a .jar (java) file from a web server, by reading the URL that is specified in the .jad file of the same game/application. I\'m usi

9条回答
  •  广开言路
    2020-11-22 17:28

    Yes, definietly requests is great package to use in something related to HTTP requests. but we need to be careful with the encoding type of the incoming data as well below is an example which explains the difference

    
    from requests import get
    
    # case when the response is byte array
    url = 'some_image_url'
    
    response = get(url)
    with open('output', 'wb') as file:
        file.write(response.content)
    
    
    # case when the response is text
    # Here unlikely if the reponse content is of type **iso-8859-1** we will have to override the response encoding
    url = 'some_page_url'
    
    response = get(url)
    # override encoding by real educated guess as provided by chardet
    r.encoding = r.apparent_encoding
    
    with open('output', 'w', encoding='utf-8') as file:
        file.write(response.content)
    
    

提交回复
热议问题