【问题标题】:Scrape Instagram Web Hashtag Posts抓取 Instagram 网络标签帖子
【发布时间】:2019-09-23 13:40:05
【问题描述】:

我正在尝试将帖子数量抓取到给定主题标签 (#castles) 并使用 ImportXML 填充 Google 表格单元格。

我尝试从 Chrome 复制 Xpath 并将其粘贴到单元格中的 ImportXML 参数,如下所示:

=ImportXML("https://www.instagram.com/explore/tags/castels/", "//*[@id="react-root"]/section/main/header/div[2]/div/div[2]/span/span")

我看到引号有问题所以我也尝试了:

=ImportXML("https://www.instagram.com/explore/tags/castels/", "//*[@id='react-root']/section/main/header/div[2]/div/div[2]/span/span")

不过,两者都返回错误。

我做错了什么?

附:我知道元标记描述 "//meta[@name='description']/@content" 的 Xpath,但是我想抓取确切的帖子数量而不是缩写数字。

【问题讨论】:

  • 基于应用程序脚本的解决方案是否可行,或者您是否希望仅通过使用=IMPORTXML 函数来完成它?我想出了公式,但它不适用于 Result too large =REGEXEXTRACT(ImportXML("https://www.instagram.com/explore/tags/cats/", "//body/script[1]"),"edge_hashtag_to_media[[:punct:]][[:punct:]][[:punct:]][[:punct:]]count[[:punct:]][[:punct:]](\d+)\,[[:punct:]]page_info[[:punct:]]") 编辑说明:不 work
  • 我很好奇...应用程序脚本如何运行?
  • 给你 - 当然,这只是一个示例实现 - function hashCount() { var url = 'instagram.com/explore/tags/cats'; var response = UrlFetchApp.fetch(url).getContentText(); var regex = /(edge_hashtag_to_media":{"count":)(\d+)(,"page_info":)/gm; var count = regex.exec(response)[2]; Logger.log(count) }跨度>
  • 返回一个空值 :-(

标签: xpath google-apps-script web-scraping google-sheets instagram


【解决方案1】:

试试这个 -

function hashCount() {
  var url = 'instagram.com/explore/tags/cats/';
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getContentText();
  var regex = /(edge_hashtag_to_media":{"count":)(\d+)(,"page_info":)/gm;
  var count = regex.exec(response)[2];
  Logger.log(count);
}

演示 -

我添加了muteHttpExceptions: true,这在我上面的评论中没有添加。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 2017-07-22
    相关资源
    最近更新 更多