I can't learn anything unless I keep making it, so I'm going to use Django to get RSS that I've done once before. It seems that you can throw it into the database with reference to past articles, get only the text and chain it with Markov.
I tried it as a simple configuration as usual.
myapp/views.py
import feedparser
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
from django.http import (HttpResponse, HttpResponseRedirect,)
from django.shortcuts import (render, redirect,)
from django.core.mail import (send_mail, BadHeaderError,)
def index(request):
url = 'http://news.yahoo.co.jp/pickup/local/rss.xml'
feeder = feedparser.parse(url)
for entry in feeder['entries']:
title = entry['title']
link = entry['link']
context = {
'title':title,
'link':link,
}
return render(request,'index.html',context)
It is a simple template that displays only one item.
templates/index.html
{% extends "base.html" %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-6">
{{ title }}
{{ link }}
</div>
</div>
</div>
{% endblock %}
I have an interesting feeling that it will be interesting to put it in the database and process it hard. Well, here is the production. Let's save it in the database.
Get the adult video RSS of DMM and save it in the database.
views.py
import feedparser
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
from django.http import HttpResponse
from django.shortcuts import (render, redirect,)
from myapp.models import Feedmodel
def index(request):
url = 'http://www.dmm.co.jp/digital/videoa/-/list/rss/=/sort=date/'
feeder = feedparser.parse(url)
for entry in feeder['entries']:
data = Feedmodel()
data.title_name = entry['title']
data.url_link = entry['link']
data.package_img = entry['package']#Get package thumbnail
data.save()
all_data = Feedmodel.objects.order_by('-id')
context = {
'all_data': all_data,
}
return render(request,'index.html',context)
models.py
from django.db import models
class Feedmodel(models.Model):
title_name = models.CharField(max_length=140)
url_link = models.CharField(max_length=140)
package_img = models.TextField(null=True)
Whether or not it is there is subtle, but it is for the management screen.
admin.py
from django.contrib import admin
from myapp.models import Feedmodel
class FeedmodelAdmin(admin.ModelAdmin):
list_display = ('id','title_name','url_link','package_img')
admin.site.register(Feedmodel,FeedmodelAdmin)
index.html
{% extends "base.html" %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-12">
{% for i in all_data %}
<div class="col-md-4 clearfix">
<p><img src="{{ i.package_img }}"><a href="{{ i.url_link }}">{{ i.title_name }}</a></p>
</div>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
It is unclear what it can be used for and whether it is practical.
Recommended Posts