python - Django: Auto Populate CharField using Urllib2 -
python - Django: Auto Populate CharField using Urllib2 -
i working on djnago project , wondering if utilize info parsed using urllib2 auto-populate feilds big amount of info quickly. here models
from django.db import models class stocks(models.model): ticker = models.charfield(max_length=250) name = models.charfield(max_length=250) exchange = models.charfield(max_length=250) industry = models.charfield(max_length=250) = models.textfield() class meta: verbose_name_plural = "stocks" def __unicode__(self): homecoming self.ticker
so far have done using info csv populate the fields, "ticker," "name," , "exchange" (uisng "python manage.py shell"):
import csv stocks.models import stocks fields = ["ticker", "name", "exchange"] row in csv.reader(open('nasdaq.csv', 'ru'), dialect='excel'): stocks.objects.create(**dict(zip(fields, row)))
i wondering if auto-populate "industry" field same way pulling info urllib2. here relevany urllib2 code:
induscode = urllib2.urlopen("http://finance.yahoo.com/q/in?s="+t).read() industry = induscode.split('<b>industry: ')[1].split('</b>')[0] industry = industry.replace("&", "&")
anyone know if can utilize info pulled urllib2 populate "industry" field? thanks
sure.
import csv stocks.models import stocks fields = ["ticker", "name", "exchange"] row in csv.reader(open('nasdaq.csv', 'ru'), dialect='excel'): row_dict = dict(zip(fields, row)) induscode = urllib2.urlopen("http://finance.yahoo.com/q/in?s=" + row_dict['ticker']).read() industry = induscode.split('<b>industry: ')[1].split('</b>')[0] industry = industry.replace("&", "&") row_dict['industry'] = industry stocks.objects.create(**row_dict)
haven't tested. believe similar should work.
python django django-models django-admin urllib2
Comments
Post a Comment