使用Python通过javascript onclick下载文件?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Python通过javascript onclick下载文件?相关的知识,希望对你有一定的参考价值。

在这个页面上有一个按钮,读取Download CSVhttp://www.nasdaqomxnordic.com/aktier/microsite?Instrument=CSE77855&name=Pandora&ISIN=DK0060252690

如何使用Python下载文件?

该页面的html为:

<a class="floatRight exportTrades" id="exportIntradayTradesCSV">Download CSV</a>

<script>
// #*

var tradesForShare = {
    load: function () {
        var q = {
            "SubSystem": "Prices",
            "Action": "GetInstrument",
            "inst.an": "nm",
            "inst.e": "3",
            "Exchange": "NMF",
            "Instrument": webCore.getInstrument(),
            "cache": "skip",
            "app": location["pathname"],
            "datasource": "prod",
            "translationFile": "translation",
            "DefaultDecimals": false
        };

        $("#tradesForShareOutput").loading("/static/nordic/css/img/loading.gif");
        var nordicRTI = NordicRTI.getInstance();
        var url = window.webCore.getWebAPIURL("prod", "MarketData/GetMarketData", true);
        var tradesRTI = new RTIObject(url, q, function (data) {
            tradesForShare.parseData(data);
                       console.log(tradesRTI);
        });
        nordicRTI.addRTIObject(tradesRTI);

        if($("tradesForShareTable").has("tr.odd")) {
                     $('.exportTrades').removeClass('disabled');
                     $('.exportTrades.disabled').css("pointer-events","auto");
                } else {
                     $('.exportTrades').addClass('disabled');
                     $('.exportTrades').css("pointer-events","none");
                }
        /*webCore.getMarketData(q, function (data) {
            tradesForShare.parseData(data);
        }, true);*/

        //var url = window.webCore.getWebAPIURL("prod", "MarketData/GetMarketData", true);
        /*$.getJSON(url, q, function (data) {
            tradesForShare.parseData(data);
        });*/
        /*$.ajax({
            type: "get",
            url: url,
            data: q,
            dataType: "jsonp",
            cache: true,
            success: function (data) {
                tradesForShare.parseData(data);
            },
            jsonp: "callback"
        });*/

        //setTimeout ( tradesForShare.load, 1000*30 );  // update every minute
    },
    parseData: function (data) {
        if(data.instruments != null) {
            $("#tradesForShareOutput").empty();
            var table = $("<table></table>").attr("id", "tradesForShareTable").addClass("tablesorter");
            var thead = $("<thead></thead>");
            var row = $("<tr></tr>");
            var kurs = $("<th></th>").text(webCore.getTranslationFor("trades", "p", data));// data.attributeTranslations.trades.p.trans[window.currentLanguage]);
            var vol = $("<th></th>").text(webCore.getTranslationFor("trades", "v", data));// data.attributeTranslations.trades.v.trans[window.currentLanguage]);
            var name = $("<th></th>").text(webCore.getTranslationFor("trades", "nm", data));// data.attributeTranslations.trades.nm.trans[window.currentLanguage]);
            var buyer = $("<th></th>").text(webCore.getTranslationFor("trades", "b", data));// data.attributeTranslations.trades.b.trans[window.currentLanguage]);
            var seller = $("<th></th>").text(webCore.getTranslationFor("trades", "s", data));// data.attributeTranslations.trades.s.trans[window.currentLanguage]);
            var time = $("<th></th>").text(webCore.getTranslationFor("trades", "t", data));// data.attributeTranslations.trades.t.trans[window.currentLanguage]);
            row.append(kurs).append(vol).append(name).append(buyer).append(seller).append(time);
            thead.append(row);
            var tbody = $("<tbody></tbody>");
            $.each(data.instruments[webCore.getInstrument().toLowerCase()].trades, function (k, v) {
                row = $("<tr></tr>");
                kurs = $("<td></td>").text(webCore.formatNumeric(v.values.p, 3));
                vol = $("<td></td>").text(window.webCore.formatNumeric(v.values.v, 0));
                name = $("<td></td>").text(v.values.nm);
                buyer = $("<td></td>").text(v.values.b);
                seller = $("<td></td>").text(v.values.s);
                time = $("<td></td>").text(webCore.getTimeFromDateString(v.values.t));
                row.append(kurs).append(vol).append(name).append(buyer).append(seller).append(time);
                tbody.append(row);
            });
            table.append(thead).append(tbody);
            $("#tradesForShareOutput").append(table);
            $("#tradesForShareTable").tablesorter({widgets: ['zebra']});
        }
    },
    excel: function () {
        var instrument = null;
        instrument = window.webCore.getInstrument();
        var utc = new Date().toJSON().slice(0,10).replace(/-/g,'-');
        $("#xlsForm").attr( "action", webCore.getProxyURL("prod"));
        var xmlquery = webCore.createQuery( Utils.Constants.marketAction.getTrades, {}, {
            t__a: "1,2,5,10,7,8,18",
                         FromDate : utc,
                         Instrument : instrument,
            ext_contenttype : "application/vnd.ms-excel",
            ext_contenttypefilename : "share_export.xls",
            ext_xslt:"t_table_simple.xsl",
            ext_xslt_lang: currentLanguage,
                        showall: "1"
        });
        console.log(xmlquery);
        $("#xmlquery").val( xmlquery );
        $("#xlsForm").submit();
    }
};

$(function () {
    tradesForShare.load();
$("#exportIntradayTradesCSV").on({
        click: function (e) {
                tradesForShare.excel();
            //window.webCore.exportTableToCSVClickEvent($("#exportIntradayTradesCSV"), $("#tradesForShareOutput"), '_' + window.webCore.getInstrument() + '.csv');
        }
    });
});



</script>

我尝试在谷歌浏览器中使用Inspect并点击Event Listeners

单击按钮时,我得到以下输出:

<post>
<param name="SubSystem" value="Prices"/>
<param name="Action" value="GetTrades"/>
<param name="Exchange" value="NMF"/>
<param name="t__a" value="1,2,5,10,7,8,18"/>
<param name="FromDate" value="2018-08-29"/>
<param name="Instrument" value="CSE77855"/>
<param name="ext_contenttype" value="application/vnd.ms-excel"/>
<param name="ext_contenttypefilename" value="share_export.xls"/>
<param name="ext_xslt" value="/nordicV3/t_table_simple.xsl"/>
<param name="ext_xslt_lang" value="en"/>
<param name="showall" value="1"/>
<param name="app" value="/aktier/microsite"/>
</post>

所以我认为我可以做类似以下的事情,但它不起作用;见下面的输出。

import requests

url = 'http://www.nasdaqomxnordic.com/WebAPI/api/MarketData/GetMarketData'
params = {
"SubSystem": "Prices",
"Action": "GetTrades",
"Exchange": "NMF",
"t__a": "1,2,5,10,7,8,18",
"FromDate": "2018-08-29",
"Instrument": "CSE77855",
"ext_contenttype": "application/vnd.ms-excel",
"ext_contenttypefilename": "share_export.xls",
"ext_xslt": "/nordicV3/t_table_simple.xsl",
"ext_xslt_lang": "en",
"showall": "1",
"app": "/aktier/microsite",
}

r = requests.get(url, params=params)

print(r.json())

我得到以下输出:

{'linkCall': 'SubSystem=Prices&Action=GetTrades&Exchange=NMF&t.a=1&t.a=2&t.a=5&t.a=10&t.a=7&t.a=8&t.a=18&FromDate=2018-08-29&Instrument=CSE77855&ext_contenttype=application%2fvnd.ms-excel&ext_contenttypefilename=share_export.xls&ext_xslt=%2fnordicV3%2ft_table_simple.xsl&ext_xslt_lang=en&showall=1&app=%2faktier%2fmicrosite', 'instruments': None, 'derivatives': None, 'warrants': None, 'attributeTranslations': {}, 'message': None, 'success': False}

如果可能的话我想避免使用Selenium

答案

检查html我注意到表单的动作是/webproxy/DataFeedProxy.aspx,方法是post。这意味着表单通过POST请求提交给:http://www.nasdaqomxnordic.com/webproxy/DataFeedProxy.aspx。表单有一个名为xmlquery的字段,并在您的问题中为html值。下面的代码应该下载该文件。

import requests

url = 'http://www.nasdaqomxnordic.com/webproxy/DataFeedProxy.aspx'
xmlquery = '''<post>
<param name="SubSystem" value="Prices"/>
<param name="Action" value="GetTrades"/>
<param name="Exchange" value="NMF"/>
<param name="t__a" value="1,2,5,10,7,8,18"/>
<param name="FromDate" value="2018-08-29"/>
<param name="Instrument" value="CSE77855"/>
<param name="ext_contenttype" value="application/vnd.ms-excel"/>
<param name="ext_contenttypefilename" value="share_export.xls"/>
<param name="ext_xslt" value="/nordicV3/t_table_simple.xsl"/>
<param name="ext_xslt_lang" value="en"/>
<param name="showall" value="1"/>
<param name="app" value="/aktier/microsite"/>
</post>'''

r = requests.post(url, data = {'xmlquery': xmlquery})
html = r.text

该文件不是csv(我从浏览器获取的文件也不是),它有.xls扩展名但包含一个大的html表。但是,你可以在BeautifulSoupcsv的帮助下创建一个csv文件。

from bs4 import BeautifulSoup
import csv

soup = BeautifulSoup(html, 'html.parser')
names = [i.text for i in soup.select('th')] + ['Name']
values = [
    [td.text for td in tr.select('td')] + [tr.td['title'].rstrip(' - ')]
    for tr in soup.select('tr')[1:]
]

with open('file.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(names)
    writer.writerows(values)

请注意,BeautifulSoup可能需要一些时间来解析文件,因为它非常大。如果你正在使用Python 2x open不接受newline参数。在这种情况下,您必须以二进制模式打开文件,否则它可能包含空行。


正如tommy.carstensen所提到的,pandas更适合这项任务。它有正确的工具(read_htmlto_csv),它比BeautifulSoup更快。

import pandas as pd

pd.read_html(htm_string, index_col='Time', parse_dates=True)[0].to_csv(path)

Name列不包含在文件中,因为它不在表列中,但它是title属性的值。但我们可以通过其他方式获取此列 - 例如,从原始URL。由于所有列都相同,我们可以使用查询字符串的Name值创建一个新的name列。

import pandas as pd
from urllib.parse import urlparse, parse_qs

url = 'http://www.nasdaqomxnordic.com/aktier/microsite?Instrument=CSE77855&name=Pandora&ISIN=DK0060252690'
df = pd.read_html(html, index_col='Time', parse_dates=True)[0]
df['Name'] = parse_qs(urlparse(url).query)['name'][0]
df.to_csv('file.csv')

以上是关于使用Python通过javascript onclick下载文件?的主要内容,如果未能解决你的问题,请参考以下文章

防止点击页面跳转

如何“重载” websocket?

使用Python通过javascript onclick下载文件?

如何调用onclick以获得更多href,而不仅仅是触发最后一个-嵌入式javascript模板

js事件监听

切换音乐的 onClick 图像