【发布时间】:2019-12-05 20:07:25
【问题描述】:
我想从给定的标签中获取 50 张最近的图片,并将它们显示在 MVC 网站上。我查看了官方 instagram API,找不到任何关于从主题标签获取多张图片的信息。
有什么想法吗?
【问题讨论】:
标签: instagram-api
我想从给定的标签中获取 50 张最近的图片,并将它们显示在 MVC 网站上。我查看了官方 instagram API,找不到任何关于从主题标签获取多张图片的信息。
有什么想法吗?
【问题讨论】:
标签: 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 主题标签的链接末尾添加“?__a=1”给了我一些我使用 json.net 反序列化为 C# 对象的 json。其中是每张图片的链接,然后我将其用作我网站上图片的来源。
【讨论】: