【发布时间】:2019-06-07 16:49:03
【问题描述】:
我得到了带有 BeautifulSoup 的基本 Django 应用程序,它获取有关作者和内容的数据,然后将其保存到数据库中。我需要从该内容模型中获取前 10 个最常用的词。我知道如何从 url 源中获得前 10 名,但我必须从 Model 中获得,谁能帮助我了解这背后的想法?
views.py
.............
for i in posts:
link = i.find_all('a', {'class': 'blog-button post-read-button post-button-animation'})[0]
url = link.get('href') # getting the url of each post
fixed_url = '######' + url
content = session.get(fixed_url, verify=False).content
soup = BeautifulSoup(content, "lxml")
author = soup.find_all('span', {'class': 'author-name'})[0].text # getting the author name
description = soup.find_all('div', {'class': 'post-content'})[0].text # getting the content of post
try:
a = Author.objects.get(name=author)
except Author.DoesNotExist:
author_name = author
author = Author.objects.create(name=author_name)
author.save()
Content.objects.get_or_create(description=description, author=a)
..............
models.py
class Author(models.Model):
name = models.CharField(max_length=300)
def __str__(self):
return self.name
class Content(models.Model):
description = models.TextField()
author = models.ForeignKey(Author, on_delete=models.CASCADE)
def __str__(self):
return self.description
【问题讨论】:
-
先写一些代码,如果不能解决就贴出来
-
你能展示一下模型的样子吗?
-
添加了代码。
-
内容模型描述中最常用的10个词?
-
是的,描述中最常用的 10 个词 @hancho
标签: python django beautifulsoup