开发者

How to download files from a web based file server with Python Mechanize

I have a series of files on a private ftp file server that I am trying to download using mechanize.

The mechan开发者_开发百科ize link objects have the structure

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')])

This basically corresponds to a link where a file icon is linked to the file

I am new to mechanize.

But how do I download the linked file can be got from

urlparse.urljoin(base_url , url)

Which combines the two to get :

http://myfileserver.com/cgi-bin/index.cgi?page=download&file=%2Fhome%2Fjmyfileserver%2Fpublic_html%2Fuser_data%2Fmycompany%2F.ftpquota

I dont know how to proceed.

My raw code

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)


You could try with:

for index, linkobj in enumerate(myfiles):
    if "sca" in linkobj.url:
        resp = br.follow_link(text='Download [IMG]',nr=0)
        content = resp.read()
        with open('output%s.txt' % index, 'w') as fo:
           fo.write(content)


If you have a ftp server, mechanize is not the way to download your file, you should look at ftplib. This tutorial should get you on your way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜