使用url可靠地从facebook graph api获取报废数据

时间:2015-10-06 11:57:19

标签: facebook facebook-graph-api web-scraping

我的应用需要从网址获取数据facebook碎片。到目前为止,我们正在使用

POST /?id={object-instance-id or object-url}&scrape=true

https://developers.facebook.com/docs/sharing/opengraph/using-objects

中更新对象部分详细说明了这一点

例如

POST /?id=http://google.com
{
  "url": "http://www.google.com/",
  "type": "website",
  "title": "Google",
  "image": [
  {
  "url": "http://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png"
    }
  ],
  "description": "Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.",
  "updated_time": "2015-10-06T11:34:58+0000",
  "id": "381702034999"
}

注意图像部分。

不幸的是,如果在服务器上错误地配置了og标签

POST /?id=http://some.page.with.bad.tags.com
{
  "error": {
    "message": "Invalid parameter",
    "type": "FacebookApiException",
    "code": 100,
    "error_subcode": 1611016,
    "is_transient": false,
    "error_user_title": "Object Invalid Value",
    "error_user_msg": "Object at URL 'http://some.page.with.bad.tags' of type '' is invalid because the given value '/some-bad-value' for property 'og:url' could not be parsed as type 'url'.",
    "fbtrace_id": "abcabcabc"
  }

}

没有什么有趣的回报。

尝试获取网址返回:

{
  "og_object": {
    "id": "381702034999",
    "description": "Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.",
    "title": "Google",
    "type": "website",
    "updated_time": "2015-10-06T11:40:04+0000",
    "url": "http://www.google.com/"
  },
  "share": {
    "comment_count": 2,
    "share_count": 13494003
  },
  "id": "http://www.google.com"
}

哪个错过了图像部分。我没有在文档中找到任何方法来检索带有图像的结果而不使用POST /?id = {url},但是在og标签中的任何错误都失败了。

GET /{ObjectId}

仅返回type和created_time

https://developers.facebook.com/tools/debug/中输入相同的已损坏的链接会生成包含该页面的图像,说明,标题和标题的页面。我需要的。所以它意味着facebook存储它们,即使页面有错误的标签,但我需要一种方法来获取它们。不幸的是,由于NDA,我无法提供破坏网址的链接,而且我找不到其他包含破损标签的网页。

1 个答案:

答案 0 :(得分:0)

如果页面包含无效的Open Graph标记,这似乎是预期的,也不要将Graph API与某种数据源或可用于生成Web内容预览的抓取服务混淆。

如果Facebook无论出于何种原因无法解析URL的Open Graph标签,它将尝试根据页面内容做出很好的猜测(文本大片,找到的图像,标题标签等)。 )来构建预览,这样你就可以从GET /{object-id}获得某种数据,这可能只是猜测而不是实际的og:..数据。

如果您真的需要一个或多或少的故障保护解决方案,您可以构建自己的刮刀来查找Open Graph标签。