使用BeautifulSoup 以抓取彩票往期数据为例
导入模块
安装模块 pip install bs4
导入 from bs4 import BeautifulSoup

data = requests.get('http://datachart.500.com/ssq/history/newinc/history.php?limit=200&sort=0')
html = BeautifulSoup(data.text,'html.parser')
list =[]
for tag in html.find_all('tr',class_='t_tr1'):
qihao = tag.find('td').get_text()
honqiu = ''
for haoma in tag.find_all('td',class_='t_cfont2'):
honqiu += haoma.get_text()+','
honqiu = honqiu[0:-1:]
lanqiu = tag.find('td',class_='t_cfont4').get_text()
list.append(Haoma(qihao=qihao,number=honqiu,lan=lanqiu))
Haoma.objects.bulk_create(list)
数据量大的时候,两种写入方式较为明显