我以前試過這個。我完全不知所措。python下載價格
在此頁面上,此對話框用於引號。 http://www.schwab.com/public/schwab/non_navigable/marketing/email/get_quote.html?
我用SPY,XLV,IBM,MSFT
輸出是上面的表。
如果你有一個帳戶的報價是實時---通過cookie。
如何使用2.6將表格轉換爲python。作爲列表或字典的數據
我以前試過這個。我完全不知所措。python下載價格
在此頁面上,此對話框用於引號。 http://www.schwab.com/public/schwab/non_navigable/marketing/email/get_quote.html?
我用SPY,XLV,IBM,MSFT
輸出是上面的表。
如果你有一個帳戶的報價是實時---通過cookie。
如何使用2.6將表格轉換爲python。作爲列表或字典的數據
第一個問題:該數據實際上是在一幀中的iframe;您需要查看https://www.schwab.wallst.com/public/research/stocks/summary.asp?user_id=schwabpublic&symbol=APC(在URL末尾替換相應的符號)。
第二個問題:從頁面中提取數據。我個人喜歡lxml和xpath,但是有很多軟件包可以完成這項工作。我可能會想到像
import urllib2
import lxml.html
import re
re_dollars = '\$?\s*(\d+\.\d{2})'
def urlExtractData(url, defs):
"""
Get html from url, parse according to defs, return as dictionary
defs is a list of tuples ("name", "xpath", "regex", fn)
name becomes the key in the returned dictionary
xpath is used to extract a string from the page
regex further processes the string (skipped if None)
fn casts the string to the desired type (skipped if None)
"""
page = urllib2.urlopen(url) # can modify this to include your cookies
tree = lxml.html.parse(page)
res = {}
for name,path,reg,fn in defs:
txt = tree.xpath(path)[0]
if reg != None:
match = re.search(reg,txt)
txt = match.group(1)
if fn != None:
txt = fn(txt)
res[name] = txt
return res
def getStockData(code):
url = 'https://www.schwab.wallst.com/public/research/stocks/summary.asp?user_id=schwabpublic&symbol=' + code
defs = [
("stock_name", '//span[@class="header1"]/text()', None, str),
("stock_symbol", '//span[@class="header2"]/text()', None, str),
("last_price", '//span[@class="neu"]/text()', re_dollars, float)
# etc
]
return urlExtractData(url, defs)
一些代碼,當被稱爲
print repr(getStockData('MSFT'))
返回
{'stock_name': 'Microsoft Corp', 'last_price': 25.690000000000001, 'stock_symbol': 'MSFT:NASDAQ'}
第三個問題:此頁面上的標記是表象,不是結構性的 - 這說來我基於它的代碼可能會很脆弱,即對頁面結構(或頁面之間的差異)的任何更改都需要重新修改xpath。
希望有幫助!
使用類似Beautiful Soup的東西解析來自網站的HTML響應並將其加載到字典中。使用該符號作爲關鍵字,以及您感興趣的任何數據的元組作爲值。迭代所有返回的符號併爲每個符號添加一個條目。
你可以在Toby Segaran的「Programming Collective Intelligence」中看到如何做到這一點的例子。這些示例全部使用Python。
你有沒有想過使用雅虎的報價API?
見:http://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys#h=select%20 *%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%22YHOO%22
您將能夠動態生成的網頁的請求,如:
http://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%22YHOO%22個&診斷= TRUE & ENV =賣場%3A%2F%2Fdatatables.org%2Falltableswithkeys
而只是它輪詢標準的一個http GET請求。響應採用XML格式。
我有來自雅虎的數據運行。我使用cvs下載解析到表中。我不熟悉Http Get和XML格式。 – Merlin 2010-11-18 20:35:27
http get是您使用瀏覽器執行的標準http請求。使用httplib2,對象http.request(the_url,「GET」),您將得到一個xml響應並進行解析。使用這個和XML不需要技巧,但如果你想知道網絡的abc,也許你應該看看它。由於XML只是文本,所以解析它並將所需信息映射到邏輯對象和作業完成。 – 2010-11-19 15:50:03
matplotlib具有模塊從雅虎獲取歷史報價:
>>> from matplotlib.finance import quotes_historical_yahoo
>>> from datetime import date
>>> from pprint import pprint
>>> pprint(quotes_historical_yahoo('IBM', date(2010, 11, 12), date(2010, 11, 18)))
[(734088.0,
144.59,
143.74000000000001,
145.77000000000001,
143.55000000000001,
4731500.0),
(734091.0,
143.88999999999999,
143.63999999999999,
144.75,
143.27000000000001,
3827700.0),
(734092.0,
142.93000000000001,
142.24000000000001,
143.38,
141.18000000000001,
6342100.0),
(734093.0,
142.49000000000001,
141.94999999999999,
142.49000000000001,
141.38999999999999,
4785900.0)]
謝謝我真的試圖從一個經紀人獲取數據。我需要盤中的價格。 – Merlin 2010-11-18 22:19:48
@hugh我會從該頁面獲取數據。然後,使用新發現的技能和代碼爲其他頁面,感謝您的想法。它有任何方式來瀏覽該頁面。添加認證並獲得實時數據。我只需要提供的數據。 sybmol列表50+ ....再次感謝你的努力。我需要學習如何去尋找那個頁面...... – Merlin 2010-11-21 15:37:27
它完全破壞了,現在我開始使用lxml了,如果你修復它,你能告訴我你是如何修復它的。 – Merlin 2011-04-14 02:28:02
@ user428862:它以什麼方式「完全破碎」?上面的代碼似乎仍然適用於我。 – 2011-04-14 21:39:13