[英]How to download with wget in Python using variables?
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. 再次感谢。
Try 尝试
os.system('wget http://somewebsite.net/shared/fshared_%s.7z'%i)
use %s
instead of ,
使用%s
代替,
You can increment in Bash. 您可以在Bash中增加。 You have to do something like: 您必须执行以下操作:
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 -le
instead of <=
since <=
doesn't work in bash. 编辑:修复了使用-le
而不是<=
代码,因为<=
在bash中不起作用。
Try this 尝试这个
import urllib, os
i = 0
while i <= 3:
os.system('wget http://somewebsite.net/shared/fshared_%s.7z' % i)
i = i + 1
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. 要使用串联解决这个问题,你需要用+
来连接,而不是,
因为当你使用,
连接两个字符串就他们之间的空间字符分隔,当您使用相同的不会发生+
,因为它“确实”将字符串连接起来。
Eg: 例如:
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')
#... ^ ^
you can also specify python to ask you the url to pass to wget 您还可以指定python要求您将URL传递给wget
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”使用任何您要下载的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.