RSS JSON 显示的 Javascript 函数无法获取正确的个人资料图片

标签 javascript json rss

我想使用此 javascript 函数获取此网站 (https://medium.com/@femalefounderssg) 的每个帖子的个人资料照片,但我没有获得正确的照片。请指导我更改代码,以便我获得合适的照片。谢谢。这是代码。

$(function () {
var $content = $('#jsonContent');
var data = {
    rss_url: 'https://medium.com/feed/@femalefounderssg'
};
$.get('https://api.rss2json.com/v1/api.json', data, function (response) {
    if (response.status == 'ok') {
        var output = '';
        $.each(response.items, function (k, item) {
            var visibleSm;
            if(k < 3){
                visibleSm = '';
             } else {
                 visibleSm = ' visible-sm';
             }
            output += '<div class="col-sm-6 col-md-4' + visibleSm + '">';
            output += '<div class="blog-post"><header>';
            output += '<h4 class="date">' + $.format.date(item.pubDate, "dd<br>MMM") + "</h4>";
            var tagIndex = item.description.indexOf('<img'); // Find where the img tag starts
            var srcIndex = item.description.substring(tagIndex).indexOf('src=') + tagIndex; // Find where the src attribute starts
            var srcStart = srcIndex + 5; // Find where the actual image URL starts; 5 for the length of 'src="'
            var srcEnd = item.description.substring(srcStart).indexOf('"') + srcStart; // Find where the URL ends
            var src = item.description.substring(srcStart, srcEnd); // Extract just the URL
            output += '<div class="blog-element"><img class="img-responsive" src="' + src + '" width="360px" height="240px"></div></header>';
            output += '<div class="blog-content"><h4><a href="'+ item.link + '">' + item.title + '</a></h4>';
            output += '<div class="post-meta"><span>By ' + item.author + '</span></div>';
            var yourString = item.description.replace(/<img[^>]*>/g,""); //replace with your string.
            var maxLength = 120 // maximum number of characters to extract
            //trim the string to the maximum length
            var trimmedString = yourString.substr(0, maxLength);
            //re-trim if we are in the middle of a word
            trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))
            output += '<p>' + trimmedString + '...</p>';
            output += '</div></div></div>';
            return k < 3;
        });
        $content.html(output);
    }
  });
});

当前配置文件输出:

enter image description here

所需的配置文件输出:

enter image description here

enter image description here

最佳答案

您得到了错误的图像,因为您在搜索 <img 时得到的第一个结果是索引是作者的图片。

您需要跳过第一个结果并获取第二个结果:

Finding second occurrence of a substring in a string in Java [它是 Java,但概念仍然存在]

var tagIndex = item.description.indexOf('<img', item.description.indexOf('<img') + 1);

String#indexOf 支持第二个参数“fromIndex”。 通过将 1 添加到第一个结果的索引,我们确保第一个 <img将不匹配,而是第二个 <img会的。

<小时/>

简化的 JSFiddle:https://jsfiddle.net/yuriy636/03n1hffh/

关于RSS JSON 显示的 Javascript 函数无法获取正确的个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45801580/

相关文章:

json - 将带有副本的有效 json 插入到 postgres 表中

javascript - 如何将jquery中的迭代json对象传递给javascript函数

json - Flutter:如何下载Json文件到本地然后访问

android - 相当于 Google Play 的 rss.itunes 生成器

java - 如何在azure函数中使用rss文件

javascript - Sonarqube 6.7.5 即使项目没有 src/main/java 文件夹也计算代码覆盖率

javascript - 在远程数据源调用之前将过滤器注入(inject)数据源

javascript - 带 header 认证的跨域请求

javascript - 将带有 Angular foreach 循环的对象更改为数组

java - 在 android 中从 news.bitcoin.com 获取 RSS feed 时出现文件未找到异常