2👍
I find it easiest to start with how the data should be stored in a db. Start with a Article model:
def Article(models.Model):
url = models.UrlField()
title = models.CharField(max_length=200, blank=True)
html = models.TextField(blank=True)
Than we want title and html be filled if url is given. Write a signal receiver to handle scraping:
@receiver(pre_save, instance, sender=Article)
def scrape(sender, **kwargs):
if instance.url and not instance.title and not instance.html:
data = requests.get(instance.url)
instance.html = BeautifulSoup(data)
instance.title = instance.html.title.name
If you add a admin, you’re done. But views to display and update the data are also easy to create:
class ArticleDetailView(DetailView):
model = Article
class ArticleUpdate(UpdateView):
model = Article
Note: The code needs some work. You also need to write imports, urls and templates.
Finally: there is nothing wrong with third party apps. I don’t own Python, Django, Requests, Beautiful Soup, etc… Even if this is a learning project, it a good thing to NOT reinvent the wheel. Be as lazy as possible. Use tools that do the job for you.
0👍
- find wysiwyg editor like froala, modify field
- update with ajax.
- check permissions on server side.
- [Answered ]-Output the values of the variables of django rest framework
- [Answered ]-Using easy-thumbnails (or other 3rd party library) with jinja2 in Django
- [Answered ]-Rounding a timedelta to the nearest 15 minutes
- [Answered ]-When should the hostname be in ALLOWED_HOSTS
- [Answered ]-Query to retrieve neighbors is too slow
Source:stackexchange.com