[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实现转盘的转 ...
随机推荐
- Python基础框架和工具
最近在学Python金融大数据分析,在安装Python进行大数据分析的环境时遇到很多问题,例如:在安装pandas包时候就要到各种错误,总是缺少很多安装包,最后发现利用Python的Anaconda进 ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
- Javascript闭包演示【转】
文章出自http://www.cnblogs.com/snandy/archive/2011/03/01/1967628.html 有个网友问了个问题,如下的html,为什么点击所有的段落p输出都是5 ...
- Centos7 下nginx nginx-1.13.4 安装
环境:CentOS Linux release 7.3.1611 (Core) Linux localhost.localdomain 3.10.0-514.26.2.el7.x86_64 #1 S ...
- Java package和import语句
Java中的package和import语句 如果你想让其他人访问你的类,你一定要把你写的类放到正确的子目录下. 在Java里,对于位于包中的类是这样管理的: Java编译器把包对应于文件系统的目录管 ...
- 导弹拦截与Dilworth定理
这两天被Dilworth.链和反链搞到头昏脑胀,终于有点眉目,现在来总结一下. Dilworth定理说的是:对于一个偏序集,其最少链划分数等于其最长反链的长度. Dilworth定理的对偶定理说的是: ...
- VS2013 “未找到与约束 ContractName Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionService RequiredTypeIdentity Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionService 匹配的导出”
下面是我出错误的附加图片 这个错误导致无法打开项目. 解决方法: 解: C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\12.0 ...
- 201621044079 week13 网络
作业13-网络 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以 ...
- netbeans调试配置
apache端口8050,xdebug端口9000 1.把项目放到apache的htdocs下(一定要放在htdocs上,要么调试的时候xdebug会一直卡在“等待连接中”) 2.把php_xdebu ...
- 第69天:jQuery入口函数
一.jQuery入口函数 1.$(document).ready(function(){}); 2.$(function(){}); 二.事件处理程序 1.事件源 Js方式:document.get ...