1

I need to do something like this, but in Python instead of 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:

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

Could anyone help me with this?

SOLVED! :)

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

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

How do I mark the topic as solved?

Thanks again.

4

6 回答 6

3

尝试

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

使用%s而不是,

于 2013-05-30T19:33:09.717 回答
1

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

import urllib, os

for i in range(4):
    filename = 'fshared_{}.7z'.format(i)
    urllib.urlretrieve('http://somewebsite.net/shared/'+filename, filename)
于 2013-05-30T19:39:59.747 回答
1

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

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

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

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

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

于 2013-05-30T19:35:29.213 回答
0

尝试这个

import urllib, os
i = 0
while i <= 3:
    os.system('wget http://somewebsite.net/shared/fshared_%s.7z' % i)
    i = i + 1
于 2013-05-30T19:57:14.953 回答
0

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

例如:

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

输出:

HelloWorld!
Hello Stack Overflow!

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

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

粘贴在 Ideone.com 上的代码

于 2013-05-31T02:36:08.603 回答
0

您还可以指定 python 来询问您要传递给 wget 的 url

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

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

于 2015-06-12T11:26:46.543 回答