我在私有ftp文件服务器上有一系列文件,我试图使用mechanize下载。
机械化链接对象具有结构
Link(base_url='http://myfileserver.com/cgi-bin/index.cgi', url='index.cgi?page=download&file=%2Fhome%2Fjmyfileserver%2Fpublic_html%2Fuser_data%2Fmycompany%2F.ftpquota', text='Download [IMG]', tag='a', attrs=[('href', 'index.cgi?page=download&file=%2Fhome%2Fjmyfileserver%2Fpublic_html%2Fuser_data%2Fmycompany%2F.ftpquota'), ('class', 'ar')])
这基本上对应于文件图标链接到文件
的链接
我是机械化的新手 但是如何从
中下载链接文件urlparse.urljoin(base_url , url)
将两者结合起来得到:
http://myfileserver.com/cgi-bin/index.cgi?page=download&file=%2Fhome%2Fjmyfileserver%2Fpublic_html%2Fuser_data%2Fmycompany%2F.ftpquota
我不知道如何继续。
我的原始代码
import mechanize
import subprocess
import urlparse
br = mechanize.Browser()
br.open("http://myfileserver.com/cgi-bin/index.cgi")
br.select_form(nr=0)
br['login'] = "mylogin"
br['password'] = "mypassword"
br.submit()
#print dir(br)
myfiles = []
for alink in br.links():
print alink
myfiles.append(alink)
def downloadlink(l):
print " Trying to download", l.url.split("%2F")[-1]
f=open(l.url.split("%2F")[-1],"w")
myurl = urlparse.urljoin(l.base_url,l.url)
print myurl
# Dont know how to proceed
for linkobj in myfiles:
if "sca" in linkobj.url:
#br.follow_link(text='[IMG]', nr=0)
downloadlink(linkobj)