Scrape Instagram Web Hashtag帖子

时间:2019-09-23 13:40:05

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

我正在尝试将帖子的数量抓取到给定的标签(#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")

尽管如此,它们都返回错误。

我在做什么错了?

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

1 个答案:

答案 0 :(得分:5)

尝试一下-

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);
}

演示-

Instagram Hashtag count

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