【问题标题】:How can I scrape from tiprank through Google Sheet Apps Script?如何通过 Google Sheet Apps Script 从tiprank 中抓取?
【发布时间】:2021-07-30 06:22:56
【问题描述】:

我正在使用以下 Google Sheet Apps 脚本抓取目标价格的排名。

function TRTP(url) {
const html = UrlFetchApp.fetch(url).getContentText();
const res = html.match(/<div class="flexcb_ bgwhite h12    w12  px0  displayflex positionrelative py3">.+?(<text .+?<\/text>)/);
if (!res) throw new Error("Value cannot be retrieved.")
return res;
}

我正在使用以下网址:https://www.tipranks.com/stocks/msft/forecast

这是试图从上面的 URL 的以下快照中提取数据。

Tiprank 的目标价格:

这应该会给我一个很长的 OuterHTML,其中也存在值。我使用 Google Sheet 公式从中提取目标价格。

这适用于 WSJ 等其他网站,但不适用于 Tiprank。

【问题讨论】:

  • 仅供参考,它是 scrape(和 scrapingscrapedscraper)不是废品。 “废弃”意味着像垃圾一样扔掉:-(

标签: web-scraping google-sheets


【解决方案1】:

另一种解决方案,不那么优雅

function TRTP(url) {
const html = UrlFetchApp.fetch(url).getContentText();
var res = html;
res = res.match(/(?<=flexccc    mt3 displayflex colorpale shrink0 lineHeight2 fontSize2 ml2 ipad_fontSize3.*>).*/g)[0].substring(0,50);
res = res.match(/.*(?=<)/g)[0];
if (!res) throw new Error("Value cannot be retrieved.")
return res;
}

【讨论】:

    【解决方案2】:
    =importxml("https://www.tipranks.com/stocks/msft/forecast","//div[@class='flexccc    mt3 displayflex colorpale shrink0 lineHeight2 fontSize2 ml2 ipad_fontSize3']")
    

    【讨论】:

    • 嗨。我很感激你的努力,但使用 importxml 有一个限制。它一次只能调用 5 到 10 个符号。appscript 一次可以处理更多的符号。你能帮我解决一个脚本吗? ?
    • 好的,我会努力的,请耐心等待(除非其他人解决了我之前的请求)
    【解决方案3】:

    试试这个:

    最低价格目标:

    =importxml(CONCATENATE("https://www.tipranks.com/stocks/", A1,"/forecast"), "//*[@class='colorpurple-dark  ml3 mobile_fontSize7 laptop_ml0']")
    

    平均价格目标:

    =importxml(CONCATENATE("https://www.tipranks.com/stocks/", A4,"/forecast"), "//*[@class='colorgray-1  ml3 mobile_fontSize7 laptop_ml0']")
    

    最高价格目标:

    =importxml(CONCATENATE("https://www.tipranks.com/stocks/", A4,"/forecast"), "//*[@class='colorpale  ml3 mobile_fontSize7 laptop_ml0']")
    

    【讨论】:

    • 请不要对多个问题发布相同的答案。如果两个问题可以用 exact 相同的答案来回答,则应考虑它们是否重复——在这种情况下,应标记较弱的问题。否则,您的答案应该根据问题的具体情况进行定制,以确保它满足作者的特定需求。
    猜你喜欢
    • 2019-11-25
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多