python爬取双色球每期中奖号码(原创)

#-*—coding:utf8-*-
import re
import requests
import mysql.connector
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

url1= "http://kaijiang.500.com/shtml/ssq/"
url2=".shtml?0_ala_baidu"
print "數據庫開始連接"
conn=mysql.connector.connect(host='127.0.0.1',
                                   user='root',
                                   passwd='123456',
                                   db='test')
cursor = conn.cursor()
print "數據庫已經連接"
num=0
for j in range (3000,19000,1000):
    for i in range(j+1,j+1000,1):
        url=url1+bytes(i)+url2
        # print url
        html = requests.get(url)
        # print html.text
        title = re.findall('
  • (.*?)
  • ', html.text, re.S) # print "第",i,"期" # print "红球是:" for each in title: print each, title1 = re.findall('
  • (.*?)
  • ', html.text, re.S) # print "篮球是:", # for each in title1: # print each insert_ssq = ("INSERT INTO ssq(rq ,hq1,hq2,hq3,hq4,hq5,hq6,lq) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)") data_ssq = None if len(title)<6: print "第",i,"条数据开始插入失败" break data_ssq = [(i,title[0], title[1], title[2],title[3],title[4],title[5],title1[0])] # cursor.execute(insert_ssq, data_ssq) # num += 1 # conn.commit() try: cursor.executemany(insert_ssq, data_ssq) num+=1 conn.commit() print "成功插入第",i,"期" except: conn.rollback() print "第",i,"期,插入失败" # 使用execute方法执行SQL语句 # cursor.execute('select * from student where 1=1') # 表查询 # 使用 fetchone() 方法获取一条数据库。 # values = cursor.fetchall() # print(values) # 关闭数据库连接 print "关闭数据库连接" print "插入了",num,"条数据" cursor.close()

    刚学习python,这个蛮有趣。

    本人不研究彩票,只是别人所托。。。

    你可能感兴趣的:(程序代码,python)