Facebook scraper doesn't load dynamic meta-tags

只谈情不闲聊 提交于 2019-12-18 07:39:42

问题


I am creating the HTML meta-tags dynamically using the function below (GWT). It takes 1 second to have this on the DOM. It is working fine except for Facebook. When I share a link from my web, the scraper gets the meta-tags that are in the HTML: none. How can I fix this?

/**
* Include the HTML attributes: title, description and keywords (meta tags)
*/
private void createHTMLheader(MyClass thing) {

    String title=thing.getTitle();
    String description=thing.getDescription();

    Document.get().setTitle(title);

    MetaElement metaDesc = Document.get().createMetaElement();
    metaDesc.setName("description");
    metaDesc.setContent(description);
    NodeList<Element> nodes = Document.get().getElementsByTagName("head");
    nodes.getItem(0).appendChild(metaDesc);
}

This is the resulting HEAD on the DOM. The title aaaa and meta-description has been loaded dynamically. (Thanks CBroe for the tip). In the "view source" functionality, these dynamic tags are not displayed (only on developer tools - view dom).

<head>
    <title>aaaa</title>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
    <meta name="description" content="My description">

    <script language="javascript" type="text/javascript" src="dialective/dialective.nocache.js"></script><script defer="defer">dialective.onInjectionDone('dialective')</script>

</head>

The original HTML doesn't have a TITLE or META-DESCRIPTION tags.


回答1:


The Facebook scraper can only see <meta> tags included in the original HTML response from the server. It's not "smart enough" to run any JavaScript code, Flash plugins, Java applets, or anything else that a full-fledged browser might run.

You'll need to generate these <meta> tags on the server using a server side framework.

Also, Facebook provides a handy testing tool to make sure your page exposes the appropriate meta tags. You might need to add OpenGraph tags as well, such as og:title and og:description.




回答2:


For some reason, the meta description has not been loaded with the function above...

That’s because you are only creating a new MetaElement – but you do nothing with it.

You have to append it to the document, more specific to its head element.

Something like

getHead().appendChild(metaDesc)

is missing.



来源:https://stackoverflow.com/questions/14898673/facebook-scraper-doesnt-load-dynamic-meta-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!