简体   繁体   English

Spynner 编程 python 浏览器从 cgi 文件服务器下载文件:jquery browser.click() 选择器?

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

I am trying to use spynner, the stateful programmatic web browser to download files from a cookie protected web / cgi file repository.我正在尝试使用 spynner,有状态的程序化 web 浏览器从受 cookie 保护的 web / cgi 文件存储库下载文件。

Spynner uses jquery like selectors to tell the browser what to click. Spynner 使用 jquery 之类的选择器来告诉浏览器点击什么。

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.但是,当我告诉 spynner 单击该链接时,什么都没有下载。 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.我让它与适当的 jquery 选择器一起使用,并且还使用了 spynner.browser.download() 方法。

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.一旦我为保存文件的 href 链接使用了适当的标签,浏览器就会将文件下载到名为 www.servername.com 的目录中。

To target all files ending with *.sca for example例如,以 *.sca 结尾的所有文件为目标

browser.click('a[href$="*.sca"]') 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.我也可以使用 browser.download(human_readable_url, open("outfile.txt","w")) 写入所述文件。

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下载有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM