[lottery anayliser]lottery anayliser

抓取网页,获得获奖信息
#!/usr/bin/python import urllib2
import re
import time def spider(url):
"""parse the website"""
fq = urllib2.urlopen(url)
fq_cts = fq.read()
#phase date
fq_phase = re.findall("<span id=\"jq_short_openTime\">\d\d-\d\d</span>", fq_cts)
if len(fq_phase) == 1:
fq_phase_md = re.search("(\d\d-\d\d)",fq_phase[0])
fq_phase_ymd = "%d-" % time.localtime().tm_year + fq_phase_md.group(0)
else:
print "find %d fq_phase, fq_phase: %r" % (len(fq_phase),fq_phase) #number list
fq_result = re.findall("div id=\"jq_openResult\".*?\/div>", fq_cts, re.S)
if len(fq_result) == 1:
fq_list = re.findall("\d\d", fq_result[0])
fq_num = [int(x) for x in fq_list]
else:
print "find %d number_list, numberlist: %r" %(len(fq_result), fq_list) #phase issue
fq_issue = re.findall("<select id=\"jq_last10_issue_no\".*?</option>", fq_cts, re.S)
if len(fq_issue) == 1:
fq_issue_number = re.search("(\d\d\d\d\d)", fq_issue[0]).group(0)
else:
print "find %d fq_issue, fq_issue: %r" % (len(fq_issue), fq_issue) if len(fq_phase) == 1 and len(fq_result) == 1 and len(fq_issue) == 1:
return [fq_phase_ymd, fq_num, fq_issue_number] #将上面产生的数据写入数据库
def tosql(numlist):
host = '10.192.0.5'
user = 'root'
password = 'fengmao'
port = ''
database = 'cp'
db = MySQLdb.connect(host,user,password,database)
cursor = db.cursor()
sql_insert = "insert into cp.bingo ( phase,releasedate,r1,r2,r3,r4,r5,b1,b2) \
values (\"%s\",\"%s\",%s,%s,%s,%s,%s,%s,%s);" \
% (numlist[2], numlist[0], numlist[1][0], numlist[1][1], numlist[1][2], numlist[1][3], numlist[1][4], numlist[1][5], numlist[1][6])
cursor.execute('select count(*) from cp.bingo where phase=\'%s\'' % (numlist[2]))
(check_num,) = cursor.fetchone()
print str(check_num)
if int(check_num) == 0:
cursor.execute(sql_insert)
db.commit()
print "update successfully!"
else:
print "this issue is in database, no action needed!"
url = "http://sina.aicai.com/kaijiang/tcdlt/"
data_receive = spider(url)
print data_receive
扩展知识点:
a. 格式化输出,我知道在输出字符串的时候,使用%d, %s, %f 分别是输出int,string,float值,但对于原样输出list或者dict,使用%r,可以实现。
b.re , findall返回的是匹配的字符串,而search返回 MatchObject 的实例,如果没有找到匹配的位置,则返回 None,可以对search使用分组,re.search("(xxx)").group(0),第一个括号匹配中的是group(0),groups返回所有匹配的组(多个括号情况下)
c. re,search与findall的区别请看:http://www.crifan.com/python_re_search_vs_re_findall/
d. re的跨行匹配,findall(reg, str, re.S), re.M , re.S
re.M 在这种情况下,^字符不仅仅匹配string的开头,还匹配string中每一行的开头
re.S 这种情况下,点号匹配任意字符,但是没有该符号的时候,点号匹配除新行外的所有字符。(也即多行匹配)
导入历史数据到数据库
#!/usr/bin/python import MySQLdb
host = '10.192.0.5'
user = 'root'
password = 'fxxx'
port = ''
database = 'cp'
db = MySQLdb.connect(host,user,password,database)
cursor = db.cursor() path = '../hist_2007-01-01_2014-03-24.txt'
histd = open(path) for line in histd:
sql = "insert into cp.bingo ( phase,releasedate,r1,r2,r3,r4,r5,b1,b2) \
values (\"%s\",\"%s\",%s,%s,%s,%s,%s,%s,%s);" %(line.split()[0],line.split()[2], line.split()[1].split(',')[0],\
line.split()[1].split(',')[1],\
line.split()[1].split(',')[2],\
line.split()[1].split(',')[3],\
line.split()[1].split(',')[4],\
line.split()[1].split(',')[5],\
line.split()[1].split(',')[6],\
)
print sql
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
创建数据库的脚本
#name: bingo.sql
create database cp;
use cp;
create table bingo (phase char(8) not null,
releasedate date,
r1 int, r2 int, r3 int, r4 int, r5 int, b1 int, b2 int,
index_id int not null primary key auto_increment);
alter table bingo auto_increment=1;
[lottery anayliser]lottery anayliser的更多相关文章
- java-7311练习(下)
java练习,仅供参考! 欢迎同学们交流讨论. JDK 1.8 API帮助文档 JDK 1.6 API中文文档 第一次小组作业:模拟双色球彩票 第一次小组作业(一) 控制台版 游戏规则: • 双色球为 ...
- jquery——九宫格大转盘抽奖
一.用到的图片 二.代码如下,重点是js部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Con ...
- 使用 Eclipse C/C++ Development Toolkit 开发应用程序
使用 Eclipse C/C++ Development Toolkit 开发应用程序 (转) 来自http://blog.csdn.net/favory/article/details/189080 ...
- JS 事件练习
QQ拖拽及状态栏选择 HTML <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- left join 改写标量子查询
数据库环境:SQL SERVER 2005 有一博彩的赔率是1:20,它有2张业务表:smuchs(投注表),lottery(开奖表). smuchs表有3个字段,分别是sno(投注号码).smuch ...
- HW3.15
import java.util.Scanner; public class Solution { public static void main(String[] args) { int lotte ...
- 抽奖转盘(jqueryrotate.js)
jqueryrotate.js抽奖转盘,使用方便,兼容各浏览器,效果如下图 <!DOCTYPE> <head> <meta http-equiv="Conten ...
- 一个好玩的jq+php实现转盘抽奖程序
前台页面: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <met ...
- 幸运大转盘-jQuery+PHP实现的抽奖程序
目前好多网站上应用的转盘抽奖程序大多是基于flash的,而本文结合实例将使用jQuery和PHP来实现转盘抽奖程序,为了便于理解,作者分两部分来讲解,本文讲解第一部分,侧重使用jQuery实现转盘的转 ...
随机推荐
- Java学习 · 初识 多线程
多线程 1. 基础概念 a) 程序 Program i. 静态代码,指令集,应用程序执行的蓝本 b) 进程 Process i. 动态概念,正在运 ...
- github 使用“git commit -m"命令时候出现的一个小问题
git commit -m 使用问题 今天提交文件到github,步骤是: git add abc.py (abc.py是我当前随意写的一个文件名) git commit -m 'add codes ...
- Linux内核设计笔记14——块I/O层
块I/O层 基本概念 系统中可以随机访问固定大小数据片的硬件设备称做块设备,这些固定大小的数据片称之为块.还有一种基本的设备称之为字符设备,其需要按照顺序访问,比如键盘. 扇区:块设备中最小的寻址单元 ...
- 八:The YARN Timeline Server
一.Overview 介绍 yarn timeline server用于存储和检查应用程序过去和现在的信息(比如job history server).有两个功能: 1.Persisting ...
- 简单DP
1.一只小蜜蜂 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input输入数据的第一行是一个整数N,表 ...
- Red and Black(DFS深搜实现)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- Thunder团队第五周 - Scrum会议3
Scrum会议3 小组名称:Thunder 项目名称:i阅app Scrum Master:王航 工作照片: 苗威同学在拍照,所以不在照片内. 参会成员: 王航(Master):http://www. ...
- JavaScript初探系列之日期对象
时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.它是一个内置对象——而不是其它对象的属性,允许用户执行各种使用日期和时间的过程. 一 Date 日期对象 ...
- java.lang.ClassNotFoundException: com.google.gson.Gson 问题解决
我是这么解决:把gson.jar放到WEB-INF/lib目录下. 放在其他目录就会报错.
- ZOJ 1913 J-Eucild's Game
https://vjudge.net/contest/67836#problem/J Two players, Stan and Ollie, play, starting with two natu ...