开发者

Spynner programmatic python browser to download files from cgi file-server: jquery selector for browser.click()?

开发者 https://www.devze.com 2023-03-20 00:42 出处:网络
I am trying to use spynn开发者_运维知识库er , the stateful programmatic web browser to download files from a cookie protected web / cgi file repository.

I am trying to use spynn开发者_运维知识库er , the stateful programmatic web browser to download files from a cookie protected web / cgi file repository.

Spynner uses jquery like selectors to tell the browser what to click.

I have a link to the file

<a href="index.cgi?page=download&amp;file=%2Fhome%2Fjdataserver%2Fpublic_html%2Fuser_data%2Fcompany%2F.ftpquota" class="ar">

However when I tell spynner to click that link nothing gets downloaded. My code is

import spynner
from spynner import browser
import pyquery
import private
import pynotify
import time


User_File_Area_URL="http://dataserver.com/cgi-bin/index.cgi"
agent = browser.Browser()
agent.load("http://dataserver.com/cgi-bin/index.cgi")
agent.wait(3)
agent.create_webview(True)
agent.show()
agent.fill("input[name=login]",private.uname)
agent.fill("input[name=password]",private.password)
agent.click("input[type=submit]")
#agent.wait(3)
#Pyquery Browser
d=pyquery.PyQuery(agent.html)
print str(d(".td1 .ar"))
agent.click(".td1 .ar",wait_load=True)
cookies = agent.get_cookies()
print cookies

The whole element is given below. I have changed server name and other attributes for privacy reasons .

<a href="index.cgi?dir=%2Fhome%2Fjdataserver%2Fpublic_html%2Fuser_data%2Fcompany%2FUntarred" class="ar"><img src="http://www.dataserver.com/img/efm_v1_6/folder.gif" width="16" height="16" border="0"/> Untarred</a><a href="index.cgi?page=download&amp;file=%2Fhome%2Fjdataserver%2Fpublic_html%2Fuser_data%2Fcompany%2F.ftpquota" class="ar"><img src="http://www.dataserver.com/img/efm_v1_6/download.gif" width="16" height="16" border="0" alt="Download" title="Download"/></a>


I got this to work with the appropriate jquery selector and also using the spynner.browser.download() method.

The file links were nested in image tags

Once I used the appropriate tag for the href link that held the file a browser.click downloaded the file to a directory called www.servername.com .

To target all files ending with *.sca for example

browser.click('a[href$="*.sca"]')

I also could use the browser.download(human_readable_url, open("outfile.txt","w")) to write to the said file.

The code is pasted here

#!/usr/bin/python
import private
import spynner
from spynner import browser
import pyquery
import urlparse
import urllib

b = browser.Browser()
b.load("http://structures.com/cgi-bin/index.cgi")
#browser.debug_level = spynner.DEBUG

def fill_login_form():
    b.fill("input[name=login]",private.uname)
    b.fill("input[name=password]",private.password)
    b.click("input[type=submit]")

def click_download():
    pass

#b.create_webview(True)
b.fill("input[name=login]",private.uname)
b.fill("input[name=password]",private.password)
b.click("input[type=submit]")

b.wait_load()
d = pyquery.PyQuery(b.html)
print str(d('a[href$="ftpquota"]'))
# Test Downloading by clicking a link which will download file called .ftpquota
# Jquery pattern $= indicates a pattern that ends with "ftpquota"

#b.click('a[href$="ftpquota"]')

raw_href = d('a[href$="ftpquota"]').attr("href")
href = urllib.unquote(raw_href)
print "HREF" , raw_href
print "Unquted" , href
print "urlparse.urlsplit()", urlparse.urlsplit(href)
print  "Current URL", b.url
print "Synthesized url" , urlparse.urljoin(b.url, href) 
#d.make_links_absolute(base_url=b.url)
filename = raw_href.split("%2F")[-1]
b.download(href, open(filename, "w"))

The download worked

0

精彩评论

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

关注公众号