【问题标题】:Getting og information when there is redirect重定向时获取og信息
【发布时间】:2018-02-09 21:14:03
【问题描述】:

我正在尝试获取此 URL 的 og 信息:

https://www.reddit.com/r/DunderMifflin/comments/6x62mz/just_michael_pouring_sugar_into_a_diet_coke/

我使用open-graph-scraper。但问题是它首先加载一个没有 og 信息的空页面,然后加载具有正确 og 信息的第二个页面。 open-graph-scraper 仅从第一页加载 og 信息。有什么方法可以让我得到正确的信息?

【问题讨论】:

  • open-graph-scraper 库使用下面的请求库,它会自动跟随重定向(至少在理论上)。您是否尝试过增加超时时间?
  • var ogs = require('open-graph-scraper'); var options = {'url': 'https://www.reddit.com/r/DunderMifflin/comments/6x62mz/just_michael_pouring_sugar_into_a_diet_coke/'}; ogs(options, function (err, results) { console.log('err:', err); // This is returns true or false. True if there was a error. The error it self is inside the results object. console.log('results:', results); }); 刚才试了上面的代码,又得到了OG信息,能不能把你的代码贴出来?

标签: javascript node.js web-scraping facebook-opengraph reddit


【解决方案1】:

var ogs = require('open-graph-scraper'); var options = {'url': 'https://www.reddit.com/r/DunderMifflin/comments/6x62mz/just_michael_pouring_sugar_into_a_diet_coke/',timeout:5000}; ogs(options, function (err, results) { console.log('err:', err); // This is returns true or false. True if there was a error. The error it self is inside the results object. console.log('results:', results); });

产量:

results: { data: { ogSiteName: 'reddit', ogDescription: '13852 points and 297 comments so far on reddit', ogTitle: 'Just Michael pouring sugar into a Diet Coke • r/DunderMifflin', twitterSite: 'reddit', twitterCard: 'summary', twitterTitle: 'Just Michael pouring sugar into a Diet Coke • r/DunderMifflin', ogImage: { url: 'https://i.redditmedia.com/sxdktAlu5OzAhSED5l75lOCNH1B7K1tmgZNxgoBj2oo.jpg?w=320&s=f529abd27b72fe5a6a246cc6505fb5a4', width: '320', height: null, type: null } }, success: true }

有时重定向需要一秒钟,增加请求的超时时间可能是个好主意。这允许库在提取 og 数据之前到达正确的页面。 open-graph-scanner 使用下面的请求库,它将继续跟踪重定向,直到它到达主页。如果仍然无法正常工作,请确保您可以使用网络浏览器访问该页面。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2011-01-24
    • 2015-01-26
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多