簡體   English   中英

如何在軌道上使用 ruby 獲取 Instagram 照片?

[英]how to get instagram photos with ruby on rails?

我正在嘗試在我的 Ruby on Rails 應用程序中顯示 Instagram 照片。 以下代碼適用於我的本地計算機,但在我的 AWS 服務器上它不顯示圖像。

在制作data = JSON.parse file.read似乎沒有在服務器上返回任何值,但在我的電腦上它顯示JSON 我會錯過什么?

<%
require "open-uri"
photo_count = 10
begin
  file = open("https://www.instagram.com/#{@user.instagram}/?__a=1")
  data = JSON.parse file.read
%>
  <% data['graphql']['user']['edge_owner_to_timeline_media']['edges'].each do |instagram| %>
    <div class="col-xl-6 xd">
      <!-- Photo Box -->
      <% @caption = instagram['node']['edge_media_to_caption']['edges'].count > 0 ? instagram['node']['edge_media_to_caption']['edges'][0]['node']['text'] : "" %>
      <a href="<%=instagram['node']['display_url']%>" class="photo-box mfp-gallery" data-background-image="<%=instagram['node']['display_url']%>" data-toggle="lightbox" title="Instagram">
        <div class="photo-box-content">
          <span>Instagram</span>
        </div>
      </a>
    </div>
  <% end %>
<% rescue => ex %>
<% end %>

更新:

在我的本地機器上執行 file.read 時,我可以看到它以這種方式向我顯示所有 instagram 用戶數據:

{"logging_page_id":"profilePage_31229027","show_suggested_profiles":false,"show_follow_dialog":false,"graphql":{"user":{"biography":"Inquiries to email@gmail.com\nAll images are under artist copyrights","blocked_by_viewer":false,"country_block":false,"external_url":"http://www.website.com/","external_url_linkshimmed":"https://l.instagram.com/?u=http%3A%2F%2Fwww.wrl ...

但在服務器上我得到了這個:

<!DOCTYPE html> <html lang="en" class="no-js not-logged-in client-root"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title> Login • Instagram </title> <meta name="robots" content="noimageindex, noarchive"> <meta name="apple-mobile-web-app-status-bar-style" content="default"> <meta name="mobile-web-app-capable" content="yes"> <meta name="theme-color" content="#ffffff"> <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover"> <link rel="manifest" href="/data/manifest.json">

有什么問題?

如果您發出大量請求,可能 Instagram 已阻止您的服務器獲取圖像,您的來源正在工作,但 Instagram API 有保護。

您可以嘗試使用 IP 代理來獲取圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM