本文翻译自:Download file from web in Python 3
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. 我正在创建一个程序,该程序将通过读取同一游戏/应用程序的.jad文件中指定的URL从Web服务器下载.jar(java)文件。 I'm using Python 3.2.1 我正在使用Python 3.2.1
I've managed to extract the URL of the JAR file from the JAD file (every JAD file contains the URL to the JAR file), but as you may imagine, the extracted value is type() string. 我已经成功地提取从JAD文件JAR文件的URL(每JAD文件中包含的URL JAR文件),但正如你可能想象,提取的值类型()的字符串。
Here's the relevant function: 相关功能如下:
def downloadFile(URL=None):
import httplib2
h = httplib2.Http(".cache")
resp, content = h.request(URL, "GET")
return content
downloadFile(URL_from_file)
However I always get an error saying that the type in the function above has to be bytes, and not string. 但是,我总是得到一个错误,指出上面函数中的类型必须是字节,而不是字符串。 I've tried using the URL.encode('utf-8'), and also bytes(URL,encoding='utf-8'), but I'd always get the same or similar error. 我已经使用URL.encode(“UTF-8”),并且还试图字节(URL编码=“UTF-8”),但我总是得到相同的或类似的错误。
So basically my question is how to download a file from a server when the URL is stored in a string type? 因此,基本上我的问题是,当URL以字符串类型存储时,如何从服务器下载文件?
#1楼
参考:https://stackoom.com/question/uoqG/使用Python-从网上下载文件
#2楼
I hope I understood the question right, which is: how to download a file from a server when the URL is stored in a string type? 我希望我理解正确的问题,即:当URL以字符串类型存储时,如何从服务器下载文件?
I download files and save it locally using the below code: 我下载文件,并使用以下代码将其保存在本地:
import requests
url = 'https://www.python.org/static/img/python-logo.png'
fileName = 'D:\Python\dwnldPythonLogo.png'
req = requests.get(url)
file = open(fileName, 'wb')
for chunk in req.iter_content(100000):
file.write(chunk)
file.close()
#3楼
I use requests
package whenever I want something related to HTTP requests because its API is very easy to start with: 每当我想要与HTTP请求相关的内容时,我都会使用requests
包,因为它的API很容易开头:
first, install requests
首先,安装requests
$ pip install requests
then the code: 然后是代码:
from requests import get # to make GET request
def download(url, file_name):
# open in binary mode
with open(file_name, "wb") as file:
# get request
response = get(url)
# write to file
file.write(response.content)
#4楼
from urllib import request
def get(url):
with request.urlopen(url) as r:
return r.read()
def download(url, file=None):
if not file:
file = url.split('/')[-1]
with open(file, 'wb') as f:
f.write(get(url))
#5楼
You can use wget which is popular downloading shell tool for that. 您可以使用wget ,它是流行的下载shell工具。 https://pypi.python.org/pypi/wget This will be the simplest method since it does not need to open up the destination file. https://pypi.python.org/pypi/wget这将是最简单的方法,因为它不需要打开目标文件。 Here is an example. 这是一个例子。
import wget
url = 'https://i1.wp.com/python3.codes/wp-content/uploads/2015/06/Python3-powered.png?fit=650%2C350'
wget.download(url, '/Users/scott/Downloads/cat4.jpg')
#6楼
Here we can use urllib's Legacy interface in Python3: 在这里,我们可以在Python3中使用urllib的Legacy接口:
The following functions and classes are ported from the Python 2 module urllib (as opposed to urllib2). 以下函数和类是从Python 2模块urllib(与urllib2相对)移植的。 They might become deprecated at some point in the future. 他们可能在将来的某个时候被弃用。
Example (2 lines code) : 示例(两行代码) :
import urllib.request
url = 'https://www.python.org/static/img/python-logo.png'
urllib.request.urlretrieve(url, "logo.png")