2014-03-25 97 views
1

我想要使用JavaScript填充返回的網站。如何在JavaScript中返回搜索結果(使用python)

我可以簡單地調用腳本並使用其結果嗎? (當然,沒有分頁)。我不想運行整個東西來刮取格式化的HTML,但是原始的源代碼是空白的。

看一看:http://kozbeszerzes.ceu.hu/searchresults.xhtml?q=1998&page=0

回報的來源僅僅

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="/templates/base_template.xsl"?> 
<content> 
    <head> 
    <SCRIPT type="text/javascript" src="/js/searchResultsView.js"></SCRIPT>  
    </head> 
    <whitebox> 
    <div id = "hits"></div> 
    </whitebox> 
</content> 

我寧願簡單的Python的工具。

+1

我只是看着這個,但試試PhantomJS和Selenium WebDriver。我會盡力爲你解答。 – Qrtn

回答

2

我下載了SeleniumChromeDriver

from selenium import webdriver 

driver = webdriver.Chrome() 
driver.get('http://kozbeszerzes.ceu.hu/searchresults.xhtml?q=1998&page=0') 

for e in driver.find_elements_by_class_name('result'): 
    link = e.find_element_by_tag_name('a') 
    print(link.text.encode('ascii', 'ignore'), link.get_attribute('href').encode('ascii', 'ignore')) 

driver.quit() 

如果您使用Chrome,則可以使用F12檢查頁面屬性,這非常有用。

1

簡而言之:你不能只用Python做到這一點。

正如你所說,這是由JavaScript(jquery)填充,即時添加內容。

您可以嘗試在本地運行帶nodejs的腳本,並在某處將DOM轉儲爲html。但是無論如何,你需要深入研究js代碼。

+0

謝謝,那麼你能幫助我嗎(或幫助重新解釋這個問題)如何運行正確的一段JavaScript例如通過AppleScript調用('告訴應用程序谷歌瀏覽器執行... .js',但具體如何?)。如果你看看.js文件,我很滿意它在'resp'中的回報,沒有分頁我只需要在1998 - 2014年每年只運行一次。 –

+0

[nodejs](http://nodejs.org/)是js解釋器,您可以使用它來安裝和運行js腳本。看看它,它不比python shell/interpreter更難使用。 –

+0

會做什麼,我不知道如何可以指定這個遠程函數的函數參數,該函數構建用於在包含它的頁面中查詢。謝謝! –

2

事實上,你可以用Python做到這一點。你需要python-ghost或者Selenium。我更喜歡後者combined with PhantomJS,更輕和更簡單的安裝,易於使用:

安裝與NPM phantomjs(節點包管理器):

apt-get install nodejs 
npm install phantomjs 

安裝硒:

pip install selenium 

和得到像這樣的結果頁面,並像往常一樣用beautifulSoup(或其他庫)解析它:

from BeautifulSoup4 import BeautifulSoup as bs 
from selenium import webdriver 
client = webdriver.PhantomJS() 
client.get("http://foo") 
soup = bs(client.page_source)