javascript - 从 reddit 获取高分辨率图像

标签 javascript json api reddit

我正在尝试按新近度从 Reddit 子版 block 中获取高分辨率图像。我可以提取缩略图,但不知道如何提取真正的高分辨率图像。

这是我的代码:

fetch('https://www.reddit.com/r/cats.json')
  .then(res => res.json())
  .then(res => res.data.children)
  .then(res => res.map(post => ({
    author: post.data.author,
    link: post.data.url,
    img: post.data.thumbnail,
    title: post.data.title
  })))
  .then(res => res.map(render))
  .then(res => console.log(res))

const app = document.querySelector('#app');

const render = post => {
  const node = document.createElement('div');
  node.innerHTML = `
      <a href="${post.link}">
        <img src="${post.img}"/>
      </a>`;
  app.appendChild(node);
}

这是我的fiddle

有什么想法吗?

最佳答案

看起来您需要交换 href 和 img src

fetch('https://www.reddit.com/r/cats.json')
.then(res => res.json())
.then(res => res.data.children)
.then(res => res.map(post => ({
  author: post.data.author,
  link: post.data.url,
  img: post.data.thumbnail,
  title: post.data.title
})))
.then(res => res.map(render))
.then(res => console.log(res))

const app = document.querySelector('#app');
const render = post => {
const node = document.createElement('div');
 node.innerHTML = `
  <a href="${post.img}">
    <img src="${post.link}"/>
  </a>`;
app.appendChild(node);
}

fiddle :https://jsfiddle.net/qt7ktv4L/2/

关于javascript - 从 reddit 获取高分辨率图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381400/

相关文章:

javascript - 使用 for 循环创建 html 元素

ios - JSON 到 Realm 对象映射问题 iOS Swift

javascript - JSON.解析: unexpected end of data (javascript)

javascript - 错误 : Cannot find module 'config' at Function. Module._resolveFilename

Javascript 到 "fix"类的所有 hrefs 与正则表达式

java - 获取图片信息

php - HTTP 400 错误请求 - CURL PHP

javascript - Mocha 要求我读取 HTTP 请求正文

json - 响应中的有效 JSON

javascript - 如何考虑时区创建 JS 日期对象 Node.js/javascript