我正在研究一个Djnago项目,并想知道我是否可以使用我使用Urllib2解析的信息来快速自动填充大量数据的数据。这是我的模特
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)
About = models.TextField()
class Meta:
verbose_name_plural = "Stocks"
def __unicode__(self):
return self.Ticker
到目前为止,我已经使用CSV中的数据来填充字段," Ticker," "名称"和"交换"像这样(uisng" python manage.py shell"):
import csv
from stocks.models import Stocks
fields = ["Ticker", "Name", "Exchange"]
for row in csv.reader(open('NASDAQ.csv', 'rU'), dialect='excel'):
Stocks.objects.create(**dict(zip(fields, row)))
我想知道我是否可以自动填充"行业"字段以同样的方式从urllib2中提取数据。这是我的相关urllib2代码:
indusCode = urllib2.urlopen("http://finance.yahoo.com/q/in?s="+t).read()
industry = indusCode.split('<b>Industry: ')[1].split('</b>')[0]
industry = industry.replace("&", "&")
任何人都知道我是否可以使用从urllib2中提取的数据来填充&#34; Industry&#34;领域?感谢
答案 0 :(得分:2)
不确定
import csv
from stocks.models import Stocks
fields = ["Ticker", "Name", "Exchange"]
for 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未经测试。但我相信类似的东西应该有用。