如何使用 API 从 # 获取多张 Instagram 图片

How to get multiple instagram pictures from a # using the API

我想从给定的主题标签中获取 50 张最新图片,并将它们显示在 MVC 网站上。我查看了官方 instagram API,但找不到任何有关从主题标签获取多张图片的信息。

有什么想法吗?

如果您使用的是 .net 框架,这里有一些 python 代码可以在您的项目中实施:

import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json


class Insta_Image_Links_Scraper:

    def getlinks(self, hashtag, url):

        html = urllib.request.urlopen(url, context=self.ctx).read()
        soup = BeautifulSoup(html, 'html.parser')
        script = soup.find('script', text=lambda t: \
                           t.startswith('window._sharedData'))
        page_json = script.text.split(' = ', 1)[1].rstrip(';')
        data = json.loads(page_json)
        print ('Scraping links with #' + hashtag+"...........")
        for post in data['entry_data']['TagPage'][0]['graphql'
                ]['hashtag']['edge_hashtag_to_media']['edges']:
            image_src = post['node']['thumbnail_resources'][1]['src']
            hs = open(hashtag + '.txt', 'a')
            hs.write(image_src + '\n')
            hs.close()

    def main(self):
        self.ctx = ssl.create_default_context()
        self.ctx.check_hostname = False
        self.ctx.verify_mode = ssl.CERT_NONE

        with open('hashtag_list.txt') as f:
            self.content = f.readlines()
        self.content = [x.strip() for x in self.content]
        for hashtag in self.content:
            self.getlinks(hashtag,
                          'https://www.instagram.com/explore/tags/'
                          + hashtag + '/')


if __name__ == '__main__':
    obj = Insta_Image_Links_Scraper()
    obj.main()

如果其他人遇到同样的问题,我就是这样做的:

在 Instagram 标签的 link 末尾添加“?__a=1”给了我一些 json,我用 json.net 反序列化为C# 对象。其中是每张图片的 link,然后我将其用作我网站上图片的来源。