Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何下载到特定目录?_Python - Fatal编程技术网

Python 如何下载到特定目录?

Python 如何下载到特定目录?,python,Python,我是这样下载的: webbrowser.open(download_url) 我已经得到了URL,我只是用webbrowser模块打开它。这会将URL另一端的内容下载到默认浏览器的默认下载位置 如何将此位置更改为我在程序中指定的位置?我觉得这应该有一个简单的解决方案,但我在网上找不到任何关于它的东西。我尝试了os.chdir(),但没有效果。使用(在Python 3.x中): 注意第二个参数应该是文件路径,而不是目录路径。 使用以下命令: import urllib urllib.ur

我是这样下载的:

    webbrowser.open(download_url)
我已经得到了URL,我只是用
webbrowser
模块打开它。这会将URL另一端的内容下载到默认浏览器的默认下载位置

如何将此位置更改为我在程序中指定的位置?我觉得这应该有一个简单的解决方案,但我在网上找不到任何关于它的东西。我尝试了
os.chdir()
,但没有效果。

使用(在Python 3.x中):

注意第二个参数应该是文件路径,而不是目录路径。

使用以下命令:

import urllib
urllib.urlretrieve('http://python.org/images/python-logo.gif', '/tmp/foo.gif')

您应该使用
.read()
函数列出或获取该目录中的所有文件

import urllib

dirpath = 'http://www.sitesomehting.com/directory/'

url = urlopen(dirpath)

str = url.read().decode('utf-8')

pattern = re.compile('*.*')

files = pattern.findall(str)
然后,您可以使用urlretrieve在该目录中逐个获取这些文件

for file in files:
    urllib.urlretrieve(dirpath + file, localfilelocation)
请注意,这是未经测试的代码,主要是关于如何解决问题的逻辑

另外,这是一个带有
BeautifulSoup
urllib

的菜单,请看这里:
for file in files:
    urllib.urlretrieve(dirpath + file, localfilelocation)