2016-05-15 91 views
1

我對python非常陌生(事實上,這是我的第一個Python項目),我在編寫這個web刮板時遇到了一些麻煩。我用tutorial來解決這個問題,但代碼沒有產生任何結果。我真的很感謝一些幫助。刮板不會產生任何結果

from lxml import html 
import requests 

page = requests.get('http://openbook.sfgov.org/openbooks/cgi-bin/cognosisapi.dll?b_action=cognosViewer&ui.action=run&ui.object=/content/folder%5B%40name%3D%27Reports%27%5D/report%5B%40name%3D%27Budget%27%5D&ui.name=20Budget&run.outputFormat=&run.prompt=false') 
tree = html.fromstring(page.content) 

#This will find the table headers: 
categories = tree.xpath('//*[@id="rt_NS_"]/tbody/tr[2]/td/table/tbody/tr[4]/td/table/tbody/tr[2]/td/div/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]') 
# This will find the budgets 
category_budget = tree.xpath('//*[@id="rt_NS_"]/tbody/tr[2]/td/table/tbody/tr[4]/td/table/tbody/tr[2]/td/div/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td[2]/span[1]') 

print 'Cateogries: ', categories 
print 'Budget: ', category_budget 

回答

0

看起來像是由JavaScript生成的table id="rt_NS_"的內容。

在這種情況下requests不會幫助你。

page = requests.get('http://openbook.sfgov.org/openbooks/cgi-bin/cognosisapi.dll?b_action=cognosViewer&ui.action=run&ui.object=/content/folder%5B%40name%3D%27Reports%27%5D/report%5B%40name%3D%27Budget%27%5D&ui.name=20Budget&run.outputFormat=&run.prompt=false') 

ctx = page.content 
if "id=\"rt_NS_\"" in ctx: 
    print "Found!" 
else: 
    print "Not Found!" 

Not Found! 

您需要使用其他方法。 Selenium with python可能是一個選項。

+0

這是不幸的,但謝謝你看Samuel!看起來這是[相關文章](http://stackoverflow.com/questions/19738920/python-selenium-scraping-webpage-javascript-table)。這應該有助於我走向正確的方向。你搖滾! – tonestrike

相關問題