使用JavaScript下载网页的Python代码
我想用Python从一个股票交易所下载共享数据。问题是没有直接的下载链接,而是需要用JavaScript来导出数据。
数据页面的链接是:
http://tase.co.il/TASE/Templates/Company/CompanyHistory.aspx?NRMODE=Published&NRORIGINALURL=%2fTASEEng%2fGeneral%2fCompany%2fcompanyHistoryData.htm%3fcompanyID%3d001216%26ShareID%3d01091248%26subDataType%3d0%26&NRNODEGUID={045D6005-5C86-4A8E-ADD4-C151A77EC14B}&NRCACHEHINT=Guest&shareID=01820083&companyID=000182&subDataType=0
当我在浏览器中打开数据页面,然后再打开下载页面时,一切都很顺利。可是如果我直接打开下载页面,就什么都下载不下来。我猜这是因为数据页面会把实际的数据注入到一些变量里,比如Columns、Titles等等。
我试着在Python脚本中模拟这个过程,但没有成功。
def download_CSV (shareID, compID):
data_url ="http://tase.co.il/TASE/Templates/Company/CompanyHistory.aspx?NRMODE=Published&NRORIGINALURL=%2fTASEEng%2fGeneral%2fCompany%2fcompanyHistoryData.htm%3fsubDataType%3d0%26shareID%3d00759019&NRNODEGUID={045D6005-5C86-4A8E-ADD4-C151A77EC14B}&NRCACHEHINT=Guest&shareID="+shareID+"&companyID="+compID+"&subDataType=0"
import urllib2
response = urllib2.urlopen(data_url)
html = response.read()
down_url ="http://tase.co.il/TASE/Pages/Export.aspx?tbl=0&Columns=AddColColumnsHistory&Titles=AddColTitlesHistory&sn=dsHistory&enumTblType=GridHistorydaily&ExportType=3"
import urllib
urllib.urlretrieve (down_url, "test.csv")
非常感谢!