bash 如何使用变量在 Python 中使用 wget 下载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16844428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:33:22  来源:igfitidea点击:

How to download with wget in Python using variables?

pythonbashwget

提问by user2437965

I need to do something like this, but in Python instead of Bash:

我需要做这样的事情,但在 Python 而不是 Bash:

i=1
while [ $i <= 10 ] ; do
    wget http://somewebsite.net/shared/fshared_$i.7z
    $i = $i + 1
done

In Python I tried with the following:

在 Python 中,我尝试了以下操作:

import urllib, os
i = 0
while i <= 3:
    os.system('wget http://somewebsite.net/shared/fshared_',i,'.7z')
    i = i + 1

But it does not work, the variable is not concatenated correctly (or something similar).
Using Bash code does not work, apparently in Bash can't do something simple like: i = i + 1

但它不起作用,变量没有正确连接(或类似的东西)。
使用 Bash 代码不起作用,显然在 Bash 中不能做一些简单的事情,比如:i = i + 1

Could anyone help me with this?

有人可以帮我解决这个问题吗?

SOLVED!:)

解决了!:)

Now I have the script both Bash and Python, actually with Python I have several variants.

现在我有 Bash 和 Python 的脚本,实际上我有 Python 的几个变体。

Thanks to all... thanks a lot ^-^

谢谢大家...非常感谢^-^

How do I mark the topic as solved?

如何将主题标记为已解决?

Thanks again.

再次感谢。

回答by karthikr

Try

尝试

os.system('wget http://somewebsite.net/shared/fshared_%s.7z'%i)

use %sinstead of ,

使用%s代替,

回答by karthikr

You canincrement in Bash. You have to do something like:

可以在 Bash 中递增。您必须执行以下操作:

i=3
(( i++ ))
echo $i

That last like should print 4. So your script would be:

最后一个喜欢应该打印 4。所以你的脚本应该是:

i=1
while [ $i -le 10 ] ; do
    wget http://somewebsite.net/shared/fshared_$i.7z
    (( i++ ))
done

Edit: fixed code to use -leinstead of <=since <=doesn't work in bash.

编辑:固定代码使用-le而不是<=因为<=在 bash 中不起作用。

回答by Thomas Fenzl

You can use python without calling out to the shell for each file using urllib

您可以使用 python 而无需使用urllib为每个文件调用 shell

import urllib, os

for i in range(4):
    filename = 'fshared_{}.7z'.format(i)
    urllib.urlretrieve('http://somewebsite.net/shared/'+filename, filename)

回答by maazza

Try this

尝试这个

import urllib, os
i = 0
while i <= 3:
    os.system('wget http://somewebsite.net/shared/fshared_%s.7z' % i)
    i = i + 1

回答by Zignd

To fix the problem with the concatenation, you need to use +to concatenate instead of ,, because when you use ,to concatenate two strings it separate them with a space character between, the same doesn't happen when you use +because it "really" concatenate the strings.

要解决连接问题,您需要使用+to concatenate 而不是,,因为当您使用,连接两个字符串时,它会用空格字符将它们隔开,使用时不会发生同样的情况,+因为它“真正”连接了字符串。

E.g.:

例如:

print('Hello' + 'World' + '!')
print('Hello', 'Stack', 'Overflow!')

Output:

输出:

HelloWorld!
Hello Stack Overflow!

Your code would look like this now using +instead of ,:

你的代码现在看起来像这样使用+而不是,

#...
os.system('wget http://somewebsite.net/shared/fshared_' + i + '.7z')
#...                                                    ^   ^

Code pasted on Ideone.com

代码粘贴在 Ideone.com 上

回答by Saumyakanta Sahoo

you can also specify python to ask you the url to pass to wget

你也可以指定python来询问你传递给wget的url

import os
path = raw_input("enter the url:")
os.system('wget -r -nd -l1 --no-parent -A mp3 %s'%path)

in place of 'mp3' use whatever format you want to download.

使用您要下载的任何格式代替“mp3”。