我很乐意从这个网站获取数据:https://finviz.com/screener.ashx?v=141&f=sh_avgvol_o500,sh_curvol_o2000,sh_price_u50&o=-volume
我想刮掉整张桌子。我试过用这个:
function myFunction(start) {
var url = "https://finviz.com/screener.ashx?
v=141&f=sh_avgvol_o500,sh_curvol_o2000,sh_price_u50&o=-volume&r="+
start;
var fromText = '<tbody>';
var toText = '</tbody>';
var content = UrlFetchApp.fetch(url).getContentText();
var scraped = Parser
.data(content)
.from(fromText)
.to(toText)
.iterate();
}
我可以使用xpath抓取每个元素,但我认为它会很慢。
我可以拿整张桌子吗? 感谢
答案 0 :(得分:2)
以下修改怎么样?检索到的数据将导入到电子表格。
注意:
查看详细信息Parser
是一个GAS库。您可以在https://www.kutil.org/2016/01/easy-data-scrapping-with-google-apps.html
function myFunction(start) {
var url = "https://finviz.com/screener.ashx?v=141&f=sh_avgvol_o500,sh_curvol_o2000,sh_price_u50&o=-volume&r="+ start;
var content = UrlFetchApp.fetch(url).getContentText();
var scraped = Parser.data(content).from('class=\"screener-body-table-nw\"').to('</td>').iterate();
var res = [];
// If you don't want column titles, please remove this part.
var temp = [];
var titles = Parser.data(content).from("style=\"cursor:pointer;\">").to("</td>").iterate();
titles.forEach(function(e){
if (!~e.indexOf('\">')) {
temp.push(e);
} else if (~e.indexOf('img')) {
temp.push(e.replace(/<img.+>/g, ''));
}
});
res.push(temp);
// -----
var temp = [];
var oticker = "";
scraped.forEach(function(e){
var ticker = Parser.data(e).from("<a href=\"quote.ashx?t=").to("&").build();
var data1 = Parser.data(e).from("screener-link\">").to("</a>").build();
var data2 = Parser.data(data1).from(">").to("<").build();
if (oticker == "") oticker = ticker;
if (ticker != oticker) {
temp.splice(1, 0, oticker);
res.push(temp);
temp = [];
oticker = ticker;
temp.push(data1);
} else {
if (!~(data2 || data1).indexOf('<')) temp.push(data2 || data1);
}
});
var ss = SpreadsheetApp.getActiveSheet();
ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}