Python Django显示不必要的信息
我让我的python脚本以我想要的方式运行,但出于某种原因,我得到了一些我不想要的信息 在这张图片中,我不想在单词“Acute”之前打印任何内容。我的python代码将获得一篇维基百科文章,对其进行总结,并打印出最好的句子。但是,我在哪里没有代码来打印显示的其他内容。单击“生成”按钮后(在本例中为) 这是我的密码: 网址: Views.pyPython Django显示不必要的信息,python,django,Python,Django,我让我的python脚本以我想要的方式运行,但出于某种原因,我得到了一些我不想要的信息 在这张图片中,我不想在单词“Acute”之前打印任何内容。我的python代码将获得一篇维基百科文章,对其进行总结,并打印出最好的句子。但是,我在哪里没有代码来打印显示的其他内容。单击“生成”按钮后(在本例中为) 这是我的密码: 网址: Views.py from django.shortcuts import render import requests from subprocess import run
from django.shortcuts import render
import requests
from subprocess import run,PIPE
import sys
def button(request):
return render(request,'index.html')
def output(request):
data=requests.get("https://regres.in/api/users")
print(data.text)
data=data.text
return render(request,'index.html',{'data':data})
def external(request):
out=run([sys.executable,'textSummary.py'],shell=False,stdout=PIPE)
print(out)
return render(request,'index.html',{'data1':out})
index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Card layout</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}"/>
<link href="https://fonts.googleapis.com/css2?family=Dancing+Script&display=swap" rel="stylesheet">
</head>
<body>
<h1 class="StorySpinner"><u>Story Spinner</u></h1>
<main>
<div class="container">
</div>
<section class="cards">
<div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<form action="/external/" method="post">
{% csrf_token %}
{{data1}}
<br><br>
<input type="submit" value="Generate">
</form>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
</div>
</section>
</main>
</main>
</body>
</html>
{%load static%}
卡片布局
故事微调器
西瓜车
{%csrf_令牌%}
{{data1}}
西瓜车
挑选
西瓜车
挑选
西瓜车
挑选
西瓜车
挑选
西瓜车
挑选
西瓜车
挑选
西瓜车
挑选
西瓜车
挑选
textSummary.py
import bs4 as bs
import urllib.request
import re
import nltk
import heapq
source = urllib.request.urlopen('https://en.wikipedia.org/wiki/Coronavirus')
soup = bs.BeautifulSoup(source,'lxml')
maxWordsAllowed=15#no sentence should have more words than this many
maxSentencesAllowed=2#no more than this many sentences allowed
#getting all paragraphs in the html
text = ""
for paragraph in soup.find_all('p'):
text += paragraph.text
#formatting the sentences
text = re.sub(r'\[[0-9]*\]',' ',text)
text = re.sub(r'\s+',' ',text)
#we want a clean text that has no periods commas etc as it would mess up our sentences
clean_text = text.lower()
clean_text = re.sub(r'\W',' ', clean_text)
clean_text = re.sub(r'\d',' ', clean_text)
clean_text = re.sub(r'\s+',' ', clean_text)
#tokenizing the text
sentences = nltk.sent_tokenize(text)
stop_words = nltk.corpus.stopwords.words('english')
word2count = {}#used for tracking score of the words
for word in nltk.word_tokenize(clean_text):#tokenize the words
if word not in stop_words:#if it is not a stop word
if word not in word2count.keys():#check to see if word is in the keys yet or not
word2count[word] = 1#new word
else:
word2count[word] += 1#inc this frequency by one
for key in word2count.keys():#going through the keys
word2count[key] = word2count[key]/max(word2count.values())#dividing this specific word's frequency by the max numbers frequency
sent2score = {}#used for sentence score
for sentence in sentences:#go through the sentences
for word in nltk.word_tokenize(sentence.lower()):#lowercase the words
if word in word2count.keys():#if the word is in
if len(sentence.split(' '))<maxWordsAllowed:#checks to see if the sentence is less than the specified
if sentence not in sent2score.keys():#if sentence is not in the list
sent2score[sentence] = word2count[word]#add the words frequency score to sent2score
else:
sent2score[sentence] += word2count[word]#sentence already in the list so we want to add the words score and update it
best_sentences = heapq.nlargest(maxSentencesAllowed,sent2score,key=sent2score.get)#this will specify how many sentences allowed, get the sentences with the best scores and use those
newString = ""
for sentence in best_sentences:#printing the best sentences
newString += sentence
print(newString)
将bs4作为bs导入
导入urllib.request
进口稀土
导入nltk
进口heapq
source=urllib.request.urlopen('https://en.wikipedia.org/wiki/Coronavirus')
soup=bs.BeautifulSoup(源,“lxml”)
maxWordsAllowed=15#任何句子的字数都不应超过这么多
maxSentencesAllowed=2#不超过允许的句子数
#获取html中的所有段落
text=“”
对于soup.find_all('p')中的段落:
text+=段落.text
#格式化句子
text=re.sub(r'\[[0-9]*\]','',text)
text=re.sub(r'\s+','',text)
#我们想要一个干净的文本,没有句号,逗号等,因为它会弄乱我们的句子
clean_text=text.lower()
clean_text=re.sub(r'\W','',clean_text)
clean_text=re.sub(r'\d','',clean_text)
clean_text=re.sub(r'\s+','',clean_text)
#标记文本
句子=nltk.sent\u标记化(文本)
stop\u words=nltk.corpus.stopwords.words('english')
word2count={}#用于跟踪单词的分数
对于nltk中的单词。单词标记化(干净的文本):#标记化单词
如果单词不在停止词中:#如果它不是停止词
如果word不在word2count.keys()中:#检查word是否在关键字中
单词2计数[单词]=1#新词
其他:
word2count[word]+=1#将此频率加一
对于word2count.keys()中的键:#检查键
word2count[key]=word2count[key]/max(word2count.values())#将此特定单词的频率除以最大数字频率
sent2score={}#用于句子得分
对于句子中的句子:#通读句子
对于nltk.word_中的单词,标记化(句子.lower()):#将单词小写
如果单词在word2count.keys()中:#如果单词在
如果len(句子分割(“”))再次出现,那不是Django的错,它是在展示你想要展示的东西。正在生成的不必要信息在out
中<代码>输出
是的一个实例。您需要返回out
的值:
def external(request):
out=run([sys.executable,'textSummary.py'],shell=False,stdout=PIPE)
return render(request,'index.html',{'data1':out.stdout})
def external(request):
out=run([sys.executable,'textSummary.py'],shell=False,stdout=PIPE)
return render(request,'index.html',{'data1':out.stdout})