1

I'm attempting to copy all of the actual content from my Tumblr blog using a script I wrote on a different web page, but I'm having a bit of trouble with gaining access to the content. My ajax call is as follows:

$.ajax({
     url: "http://solacingsavant.tumblr.com/",
     dataType: 'jsonp',
     success: function(data) {
          var elements = $("<div>").html(data)[0].getElementsByTagName("ul")[0].getElementsByTagName("li");
          for(var i = 0; i < elements.length; i++) {
               var theText = elements[i].firstChild.nodeValue;
               alert(theText); // Alert if I got something
              // This is where I'll strip the data for the items I want
          }
     }
});

but as it is the console gives me an error of "Resource interpreted as Script but transferred with MIME type text/html" which I looked into here and changed the corresponding meta tag in the HTML of my blog to <meta http-equiv="Content-Type" content="application/javascript; charset=utf-8" /> with no success

I also tried using dataType: 'html' (which makes more sense to me) but I was getting a console error of "Origin is not allowed by Access-Control-Allow-Origin" which I also looked into and added a meta tag to my Tumblr blog with <meta Access-Control-Allow-Origin="*" />, but again didn't succeed

Here is a jsFiddle to work with

Does my approach not work because Tumblr as a whole does not allow changes to Access-Control? If so, how might I work around the issue? If not, what am I doing wrong?

MAJOR EDIT (based on mikedidthis's helpful comments)

It seems that I am not able to do this without a Tubmlr API, so I obtained an API key and now have access to the json results that the API sends out. I am able to get a jsonp object using the API key to in the console. My javascript at the moment:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/info?api_key=APIkeyGoesHeRe",
    dataType: 'jsonp',
    success: function(results){
        console.log(results); 
        // Get data from posts here
    }
});

This SO post was helpful in understanding how I can change data on my Tubmlr page from the source and find out basic information about the site, but not about how to obtain actual data from individual posts. I tried looking through the results object and was unable to find any data related to posts, nor was I able to append the results to the jsfiddle. So my questions now are, "Can I copy data (say the written text in a post) from individual posts using this approach? If so, how? If not, what other approach should I use?"

4

1 回答 1

5

一个非常快速的答案

tumblr API 文档确实很好地涵盖了 API 的使用,但是,为了给你一个小小的开始,让我们抓取你所有的文本帖子。

首先,您需要为您的任何类型为Text的帖子查询 API 。

文档声明(http://www.tumblr.com/docs/en/api/v2#posts)我们应该使用以下 url 并指定我们将设置为的类型text

api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts[/type]

下面是一个基于 OP fiddle 的示例。

$.ajax({
    url: "http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts/text?api_key=XXXXXXX",
    dataType: 'jsonp',
    success: function(data){
        posts = data.response.posts
        $.each(posts, function(i) {
            console.log( posts[i].title, posts[i].body )
        });
    }
});

因此,对于 API 的每个查询,我们都会收到一个对象。您将需要过滤此对象以从中获取所需的数据。

在帖子查询的上下文中,您可以使用data.response.posts对象直接访问您的帖子。

要了解每种帖子类型有哪些可用数据,请参阅文档:http ://www.tumblr.com/docs/en/api/v2#text-posts

对于每个Text帖子类型的内容,您需要遍历posts对象,然后获取名为titleand的键的值body

这里的例子:http: //jsfiddle.net/ZpFwL/

奖励时间type可以通过从 URL 中删除来获取所有类型的帖子:

http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts/?api_key=XXXXXXX "

请记住,这是一个非常快速的示例,不适用于现实世界。

于 2013-10-15T17:04:02.253 回答