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

#!/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;

最新文章

  1. Android获取服务器Json字符串并显示在ListView上面
  2. 【转】4G内存下MySQL修改配置文件以优化效率(来自discuz)
  3. 一个App完成入门篇(三)-完善主框架
  4. JS根据身份证号码算年龄
  5. Qt Designer 修改窗体大小改变控件位置
  6. Linux CP文件夹略过目录的解决
  7. css透明度的设置 (兼容所有浏览器)
  8. jquery 鼠标经过放大图片
  9. YIi 使用 beginContent() 和 endContent() 设定 Yii 的 layouts
  10. cocos2d-x笔记(十一)Lua发展飞机战争-5- 让飞机动
  11. thinkphp整合系列之phpexcel生成生成excel文件
  12. Tab 菜单切换
  13. python 读取csv 数据并画图分析
  14. JS_高程5.引用类型(3)Array类型-检测数组
  15. JAVA中初始化ArrayList的三种方式
  16. BZOJ.2287.[POJ Challenge]消失之物(退背包)
  17. topcoder srm 695 div1 -3
  18. 预防和避免死锁的方法及银行家算法的java简单实现
  19. 5G的作业- 云计算
  20. 关于Dijkstra算法

热门文章

  1. AVL树和伸展树 -数据结构(C语言实现)
  2. CSP201412-2:Z字形扫描
  3. OpenMPI运行问题:enough slots available in the system
  4. APUE学习笔记3_文件IO
  5. fragment的介绍与使用
  6. Check the string
  7. BluetoothAdapter解析
  8. python学习摘要(3)--字符串处理函数
  9. PokeCats开发者日志(十三)
  10. VM新安装centos7无法连接网络的问题