Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

html - Facebook scraper doesn't load dynamic meta-tags

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...