Я хотел бы сохранить ссылки, очищенные, чтобы быть сохраненными в моей базе данных, чтобы при последующем запуске он только сбрасывал и добавлял только новые ссылки на список.Python/Django Сохранять скребковые данные автоматически в моей базе данных
Это мой код ниже, но в конце дня моя база данных пуста. Какие изменения я могу сделать, чтобы преодолеть это? Заранее спасибо
from django.template.loader import get_template
from django.shortcuts import render_to_response
from bs4 import BeautifulSoup
import urllib2, sys
import urlparse
import re
from listing.models import jobLinks
#this function extract the links
def businessghana():
site = "http://www.businessghana.com/portal/jobs"
hdr = {'User-Agent' : 'Mozilla/5.0'}
req = urllib2.Request(site, headers=hdr)
jobpass = urllib2.urlopen(req)
soup = BeautifulSoup(jobpass)
for tag in soup.find_all('a', href = True):
tag['href'] = urlparse.urljoin('http://www.businessghana.com/portal/', tag['href'])
return map(str, soup.find_all('a', href = re.compile('.getJobInfo')))
# result from businssghana() saved to a variable to make them iterable as a list
all_links = businessghana()
#this function should be saving the links to the database unless the link already exist
def save_new_links(all_links):
current_links = jobLinks.objects.all()
for i in all_links:
if i not in current_links:
jobLinks.objects.create(url=i)
# I called the above function here hoping that it will save to database
save_new_links(all_links)
# return my httpResponse with this function
def display_links(request):
name = all_links()
return render_to_response('jobs.html', {'name' : name})
Мой Джанго models.py выглядит следующим образом:
from django.db import models
class jobLinks(models.Model):
links = models.URLField()
pub_date = models.DateTimeField('date retrieved')
def __unicode__(self):
return self.links
У вас есть debug = True? Вы получаете какие-либо ошибки? – sundance