• 抓取网页,获得获奖信息

#!/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的更多相关文章

  1. java-7311练习(下)

    java练习,仅供参考! 欢迎同学们交流讨论. JDK 1.8 API帮助文档 JDK 1.6 API中文文档 第一次小组作业:模拟双色球彩票 第一次小组作业(一) 控制台版 游戏规则: • 双色球为 ...

  2. jquery——九宫格大转盘抽奖

    一.用到的图片 二.代码如下,重点是js部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Con ...

  3. 使用 Eclipse C/C++ Development Toolkit 开发应用程序

    使用 Eclipse C/C++ Development Toolkit 开发应用程序 (转) 来自http://blog.csdn.net/favory/article/details/189080 ...

  4. JS 事件练习

    QQ拖拽及状态栏选择 HTML <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  5. left join 改写标量子查询

    数据库环境:SQL SERVER 2005 有一博彩的赔率是1:20,它有2张业务表:smuchs(投注表),lottery(开奖表). smuchs表有3个字段,分别是sno(投注号码).smuch ...

  6. HW3.15

    import java.util.Scanner; public class Solution { public static void main(String[] args) { int lotte ...

  7. 抽奖转盘(jqueryrotate.js)

    jqueryrotate.js抽奖转盘,使用方便,兼容各浏览器,效果如下图 <!DOCTYPE> <head> <meta http-equiv="Conten ...

  8. 一个好玩的jq+php实现转盘抽奖程序

    前台页面: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <met ...

  9. 幸运大转盘-jQuery+PHP实现的抽奖程序

    目前好多网站上应用的转盘抽奖程序大多是基于flash的,而本文结合实例将使用jQuery和PHP来实现转盘抽奖程序,为了便于理解,作者分两部分来讲解,本文讲解第一部分,侧重使用jQuery实现转盘的转 ...

随机推荐

  1. [SHELL]退出脚本

    一,退出状态码 1,范围:0~255 2,查看退出状态码:必须在命令执行之后立即执行 ,显示的是脚本最后一条命令的退出状态码 echo $? 若f返回值为0,则表示正常 有异常为正值 二,exit 脚 ...

  2. 树和二叉树 -数据结构(C语言实现)

    读数据结构与算法分析 树的概念 一棵树是一些节点的集合,可以为空 由称做根(root)的节点以及0个或多个非空子树组成,子树都被一条来自根的有向边相连 树的实现 思路 孩子兄弟表示法:树中的每个节点中 ...

  3. spark-shell解析

    spark-shell 作用: 调用spark-submit脚本,如下参数 --classorg.apache.spark.repl.Main --name "Spark shell&quo ...

  4. [Clr via C#读书笔记]Cp1CLR执行模型

    Cp1CLR执行模型 本章的概念点 CLR=Common Language Runtime 内存管理,程序集加载,安全性,异常处理和线程同步.CLR是基础,支持着面向它的各种语言.各种语言会被对应的编 ...

  5. LeetCode 96——不同的二叉搜索树

    1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...

  6. BZOJ 3924 ZJOI2015 幻想乡战略游戏 树链剖分

    题目链接:https://www.luogu.org/problemnew/show/P3345(bzoj权限题) 题意概述:动态维护树的上所有点到这棵树的带权重心的距离和.N,Q<=10000 ...

  7. C语言中的字符串分割函数

    char *strtok(char *s, const char *delim); 分解字符串为一组字符串.s为要分解的字符串,delim为分隔符字符串. 从s开头开始的一个个被分割的串.当没有被分割 ...

  8. Sqoop使用笔记(转载)

    Sqoop是Apache顶级项目,主要用来在Hadoop和关系数据库中传递数据.通过sqoop,可以方便的将数据从关系数据库导入到HDFS,或将数据从HDFS导出到关系数据库. 关于Sqoop 官网S ...

  9. 【Docker 命令】- run命令

    docker run :创建一个新的容器并运行一个命令 语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: -a stdin: 指 ...

  10. SQL SERVER技术内幕之6 集合查询

    1.定义 集合运算会对两个输入查询的结果集进行逐行比较,根据比较结果和所使用的集合运算来确定某一行是否应该包含在集合运算的结果中.因为集合运算是针对集合之间进行的计算,所以集合运算涉及的两个查询不能包 ...