Python Django:在我的Django应用程序中使用JSON和请求

Python Django:在我的Django应用程序中使用JSON和请求,python,json,django,request,Python,Json,Django,Request,我使用JSON、re和请求从instagram页面获取关注者数量: import json import re import requests PROFILE = 'espn' response = requests.get('https://www.instagram.com/' + PROFILE) json_match = re.search(r'window\._sharedData = (.*);</script>', response.text) profile_js

我使用JSON、re和请求从instagram页面获取关注者数量:

import json
import re
import requests

PROFILE = 'espn'

response = requests.get('https://www.instagram.com/' + PROFILE)
json_match = re.search(r'window\._sharedData = (.*);</script>', response.text)
profile_json = json.loads(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']

print(profile_json['edge_followed_by']['count'])
还有一个额外的问题-如果我使用基于类的通用视图和基于函数的视图,流程会有什么不同


非常感谢。

请按原样获取数据,但不要打印数字,而是将其放入变量中,并将其包含在模板上下文中:

def index(request):
    post = Post.objects.all()[0]
    recent_posts = Post.objects.all()[1:4]
    latest_posts = Post.objects.all()[:5]

    PROFILE = 'espn'

    response = requests.get('https://www.instagram.com/' + PROFILE)
    json_match = re.search(r'window\._sharedData = (.*);</script>', response.text)
    profile_json = json.loads(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']

    page_visitors = profile_json['edge_followed_by']['count']

    data = {"post":post, "recent_posts":recent_posts, "latest_posts":latest posts, "page_visitors":page_visitors}
    return render(request, 'index.html', data)
def索引(请求):
post=post.objects.all()[0]
最近发布的文章=Post.objects.all()[1:4]
最新发布=Post.objects.all()[:5]
个人资料='espn'
response=requests.get('https://www.instagram.com/"(简况)
json_match=re.search(r'window\.\u sharedData=(.*);',response.text)
profile_json=json.load(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
page_visitors=profile_json['edge\u后跟']['count']
数据={“post”:post,“recent_posts”:recent_posts,“latest_posts”:latest posts,“page_visitors”:page_visitors}
返回呈现(请求'index.html',数据)
在基于类的视图中,您可以将此代码添加到
get\u context\u data
方法中


请注意,尽管这种方法确实不太好——你似乎在从Instagram网页上抓取内容,这意味着你严重依赖于它的工作方式,你的代码基本上随时都可能意外中断。我不知道Instagram是否有允许访问此信息的API,但他们可能有,我建议您提出请求。

直接获取数据,而不是打印数字,将其放入变量中,并将其包含在模板上下文中:

def index(request):
    post = Post.objects.all()[0]
    recent_posts = Post.objects.all()[1:4]
    latest_posts = Post.objects.all()[:5]

    PROFILE = 'espn'

    response = requests.get('https://www.instagram.com/' + PROFILE)
    json_match = re.search(r'window\._sharedData = (.*);</script>', response.text)
    profile_json = json.loads(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']

    page_visitors = profile_json['edge_followed_by']['count']

    data = {"post":post, "recent_posts":recent_posts, "latest_posts":latest posts, "page_visitors":page_visitors}
    return render(request, 'index.html', data)
def索引(请求):
post=post.objects.all()[0]
最近发布的文章=Post.objects.all()[1:4]
最新发布=Post.objects.all()[:5]
个人资料='espn'
response=requests.get('https://www.instagram.com/"(简况)
json_match=re.search(r'window\.\u sharedData=(.*);',response.text)
profile_json=json.load(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
page_visitors=profile_json['edge\u后跟']['count']
数据={“post”:post,“recent_posts”:recent_posts,“latest_posts”:latest posts,“page_visitors”:page_visitors}
返回呈现(请求'index.html',数据)
在基于类的视图中,您可以将此代码添加到
get\u context\u data
方法中


请注意,尽管这种方法确实不太好——你似乎在从Instagram网页上抓取内容,这意味着你严重依赖于它的工作方式,你的代码基本上随时都可能意外中断。我不知道Instagram是否有允许访问此信息的API,但他们可能有,我建议您提出请求。

您可以将JSON
profile\u JSON
作为函数视图的上下文,只需访问Django模板中JSON的每个元素即可

导入json
进口稀土
导入请求
def索引(请求):
个人资料='espn'
response=requests.get('https://www.instagram.com/"(简况)
json_match=re.search(r'window\.\u sharedData=(.*);',response.text)
profile_json=json.load(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
上下文={'profile':profile_json}
返回呈现(请求'index.html',上下文)
现在,假设
profile_json
的结构是
{'followers':2000}
,您可以在Django模板中访问它,如下所示:
{{{profile.followers}
,因为json是在视图上下文的名称
profile
下给出的

如果要使用基于类的通用视图,我建议使用TemplateView

来自django.views.generic import TemplateView的

类配置文件TemplateView(TemplateView):
模板名称='index.html'
def获取上下文数据(自身,**kwargs):
context=super()。获取上下文数据(**kwargs)
个人资料='espn'
response=requests.get('https://www.instagram.com/"(简况)
json_match=re.search(r'window\.\u sharedData=(.*);',response.text)
profile_json=json.load(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
上下文['profile']=profile\u json
返回上下文

接下来,您可以在Django模板中以与之前相同的方式访问概要文件上下文。

您可以将JSON
profile\u JSON
作为函数视图的上下文,只需访问Django模板中JSON的每个元素

导入json
进口稀土
导入请求
def索引(请求):
个人资料='espn'
response=requests.get('https://www.instagram.com/"(简况)
json_match=re.search(r'window\.\u sharedData=(.*);',response.text)
profile_json=json.load(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
上下文={'profile':profile_json}
返回呈现(请求'index.html',上下文)
现在,假设
profile_json
的结构是
{'followers':2000}
,您可以在Django模板中访问它,如下所示:
{{{profile.followers}
,因为json是在视图上下文的名称
profile
下给出的

如果要使用基于类的通用视图,我建议使用TemplateView

来自django.views.generic import TemplateView的

类配置文件TemplateView(TemplateView):
模板名称='index.html'
def获取上下文数据(自身,**kwargs):
context=super()。获取上下文数据(**kwargs)
个人资料='espn'
response=requests.get('https://www.instagram.com/"(简况)
json_match=re.search(r'window\.\u sharedData=(.*);',response.text)
profile_json=json.load(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
上下文['profile']=profile\u json
返回上下文
接下来,您可以访问