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

时间:2019-10-18 11:14:37

标签: instagram-api

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

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

如果您使用的是.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()

答案 1 :(得分:0)

如果其他任何人遇到相同的问题,这就是我所做的:

在instagram主题标签的链接末尾添加“?__ a = 1”,为我提供了一些我使用json.net反序列化为C#对象的json。其中是每个图像的链接,然后我将其用作网站上图像的来源。