#coding=utf-8
import urllib
import re
import MySQLdb
dbnumber = MySQLdb.connect('localhost', 'root', '*******', 'dbname') #连接本地数据库
cursor = dbnumber.cursor()
def getHtml(url):
page=urllib.urlopen(url)
html=page.read()
return html
def getnumber(html):
reg=r'm>(d+)<' #通过正则表达抓取网站数字
number=re.compile(reg)
numberlist=re.findall(number,html)
return numberlist
html=getHtml("http://baidu.lecai.com/lottery/draw/list/50?type=range_date&start=2005-06-08&end=2003-02-23") #目标网站页面
for i in range(10000): #设置一个循环
try:
n = 7 * i
sql_insert = "INSERT INTO dbnumber(red_1) VALUES(%s)" %getnumber(html)[n] #将获取的数据存入本地数据库
cursor.execute(sql_insert)
except Exception:
break
dbnumber.commit()
dbnumber.close() #最后关闭数据库连接