-1
我正在處理一個項目,我試圖讓lxml從分開的網頁上的單獨表中提取庫存數據。當我運行我的程序要打印我想要的值來拉我得到空方括號Xpath不返回值lxml Python
('Cash_and_short_term_investments:', [])
('EPSNextYear:', [])
下面來看看,順便我打電話這樣的:
#the url at this point is http://finviz.com/quote.ashx?t=RAIL confirmed with print statement
url = driver.current_url
page2 = requests.get(url)
tree2 = html.fromstring(page2.content)
EPSNextYear =
tree2.xpath('/html/body/table[3]/tr[1]/td/table/tr[7]/td/table/tr[2]/td[6]/b')
#Original XPath:/html/body/table[3]/tbody/tr[1]/td/table/tbody/tr[7]/td/table/tbody/tr[2]/td[6]/b
print ('EPSNextYear:', EPSNextYear)
和:
#the url at this point is https://www.google.com/finance?q=NASDAQ%3ARAIL&fstype=ii&ei=hGwhWNHVPOW7iwLMiIfIDA I've confirmed this with a print
url = driver.current_url
page3 = requests.get(url)
tree3 = html.fromstring(page3.content)
Cash_and_Short_Term_Investments = tree3.xpath('//*[@id="fs-table"]/tr[3]/td[2]/text()')
print('Cash_and_short_term_investments:', Cash_and_Short_Term_Investments)
我已經從XPath中刪除了tbody,就像一些類似的問題所建議的一樣。任何幫助或建議將不勝感激,謝謝!
這是一個很好的解決方案來獲取字符串,然後我用正則表達式來隔離數字。 – Marc