资讯专栏INFORMATION COLUMN

[No.003-6]爬虫网易赔率数据并导入到mysql数据库

lbool / 604人阅读

摘要:文件的全路径返回对象获取所有即将读取的的全路径获取场次获取赛事类型获取比赛日期获取主队客队名称获取胜平负赔率删除首尾两个无效数据比分结果以及比分结果赔率更新胜平负胜其他平其他负其他更新总进球胜其他负其他场次赛事类型比赛日期比

#encoding:utf-8
#!/usr/local/bin/python2.7
import urllib2
from bs4 import BeautifulSoup
import MySQLdb as mdb
import sys
import re
import os

#URL:HTML文件的全路径
#返回:BeautifulSoup对象
def getSoup(url):
    return BeautifulSoup(open(url))

#获取所有即将读取的HTML的全路径
def fileNames():
    temp_f = open("/root/bet/names.txt")
    temp = []
    for line in temp_f:
        temp.append("/root/bet/urls/"+line.strip())
    temp_f.close()
    return temp

#获取场次
def getScr(soup):
    scr = []
    temp =[]
    for trs in soup.findAll("tr"):
        for tds in trs.findAll("td",{"width":"50"}):
            temp.append(tds.string)
    lt = len(temp)/4
    for i in range(lt):
        scr.append(temp[4*i+3])
    return scr

#获取赛事类型
def getLea(soup):
    league = []
    for item in soup.findAll("tr"):
        for item1 in item.findAll("td",{"width":"70"}):
            league.append(item1.string.encode("utf-8"))
    return league

#获取比赛日期
def getGmdate(soup):
    temp = []
    gmdate = []
    for item in soup.findAll("tr"):
        for item1 in item.findAll("td",{"width":"61"}):
            temp.append(item1)
    temp =temp[3:]
    for item in temp:
        gmdate.append(re.search("w{4}-w{2}-w{2}",str(item)).group())
    return gmdate

#获取主队客队名称
def getTeam(soup):
    team=[]
    for item in soup.findAll("tr"):
        for teams in item.findAll("a",{"class":"dui"}):
            team.append(teams.string.strip().encode("utf-8"))
    return team

#获取胜平负赔率
def getSpfpl(soup):
    spfpl =[]
    temp = []
    for trs in soup.findAll("tr"):
        for tds in trs.findAll("span"):
            temp.append(tds.string)
#删除首尾两个无效数据
    temp =temp[1:-1]
    for i in range(len(temp)):
        for item in temp[7*i+4:7*i+7]:
            spfpl.append(item)
    return spfpl

#6.比分结果以及比分结果赔率
def getResult(soup):
    bfjg = []
    temp = []
    for trs in soup.findAll("tr"):
        for item in trs.findAll("div",{"align":"center"}):
            for item1 in item.findAll("strong"):
                bfjg.append(item1.string.encode("utf-8"))
    return bfjg



if __name__=="__main__":
    reload(sys)
    sys.setdefaultencoding("utf-8")
    names = fileNames()
    conn=mdb.connect(host="localhost",user="root",passwd="oracle",db="betdb",port=3306)
    cur = conn.cursor()
    SQL="insert into results(id,lea,gmd,hos,gue,win,dog,los,res,odd) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    #更新胜平负
    sql_update3="update results set spf=3 where res="1:0" or res="2:0" or res="2:1" or res="3:0" or res="3:1" or res="3:2" or res="4:0" or res="4:1" or res="4:2" or res="5:0" or res="5:1" or res="5:2" or res="胜其他";"
    sql_update1="update results set spf=1 where res="0:0" or res="1:1" or res="2:2" or res="3:3" or res="平其他";"
    sql_update0="update results set spf=0 where res="0:1" or res="0:2" or res="1:2" or res="0:3" or res="1:3" or res="2:3" or res="0:4" or res="1:4" or res="2:4" or res="0:5" or res="1:5" or res="2:5" or res="负其他";"
    #更新总进球
    sql_updatezjq0="update results set zjq=0 where res="0:0""
    sql_updatezjq1="update results set zjq=1 where res="1:0" or res="0:1""
    sql_updatezjq2="update results set zjq=2 where res="2:0" or res="1:1" or res="0:2""
    sql_updatezjq3="update results set zjq=3 where res="3:0" or res="2:1" or res="1:2" or res="0:3""
    sql_updatezjq4="update results set zjq=4 where res="4:0" or res="3:1" or res="2:2" or res="1:3" or res="0:4""
    sql_updatezjq5="update results set zjq=5 where res="5:0" or res="4:1" or res="3:2" or res="2:3" or res="0:5" or res="1:4""
    sql_updatezjq6="update results set zjq=6 where res="5:1" or res="3:3" or res="4:2" or res="2:4" or res="1:5""
    sql_updatezjq7="update results set zjq=7 where res="胜其他" or res="负其他""
    for htmlfilename in names:
        soup = getSoup(htmlfilename)
                print "Reading %s now ..." % htmlfilename
        #1.场次
        scr = getScr(soup)
        #2.赛事类型
        lea = getLea(soup)
        #3.比赛日期
        gmd = getGmdate(soup)
        #4.比赛队伍
        tea = getTeam(soup)
        #5.胜平负赔率
        spf = getSpfpl(soup)
        #6.比赛结果
        bfj = getResult(soup)
        #7.形成场次日期唯一id
        ids=[]
        for i in range(len(scr)):
            ids.append(gmd[i]+"-"+scr[i])
        #装配结果集
        res =[]
        for i in range(len(scr)):
            res.append(ids[i])
            res.append(lea[i])
            res.append(gmd[i])
            res.append(tea[2*i])
            res.append(tea[2*i+1])
            res.append(spf[3*i])
            res.append(spf[3*i+1])
            res.append(spf[3*i+2])
            res.append(bfj[2*i])
            res.append(bfj[2*i+1])
        l =len(res)/10
        for i in range(l):
            for item in res[10*i:10*i+1]:
                cur.execute(SQL,res[10*i:10*i+10])
                i+=1
        cur.execute(sql_update3)
        cur.execute(sql_update1)
        cur.execute(sql_update0)    
        cur.execute(sql_updatezjq0)
        cur.execute(sql_updatezjq1)
        cur.execute(sql_updatezjq2)
        cur.execute(sql_updatezjq3)
        cur.execute(sql_updatezjq4)
        cur.execute(sql_updatezjq5)
        cur.execute(sql_updatezjq6)
        cur.execute(sql_updatezjq7)
#SQL创建语句
CREATE TABLE `results` (
  `id` char(20) NOT NULL,
  `lea` char(100) DEFAULT NULL,
  `gmd` date DEFAULT NULL,
  `hos` char(100) DEFAULT NULL,
  `gue` char(100) DEFAULT NULL,
  `win` float(5,2) DEFAULT NULL,
  `dog` float(5,2) DEFAULT NULL,
  `los` float(5,2) DEFAULT NULL,
  `res` char(10) DEFAULT NULL,
  `odd` float(5,2) DEFAULT NULL,
  `zjq` tinyint(4) DEFAULT NULL,
  `spf` tinyint(4) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/37361.html

相关文章

  • [No.003-0]爬虫网易赔率数据导入mysql数据

    摘要:所以我不得不做的就是把每天的赔率数据导入到我自己的数据库,根据一定的运算法则,将可能性筛选出来,然后我再做进一步的判断。 首先,我拥有这个导入赔率的需求,并且,我需要的是所有的数据赔率,我需要把某些脏赔率(极有可能不会出现的赔率,误导彩迷的赔率)删除,并且我需要知道这些赔率的比分,删除这些赔率,可以逆推正确比分以及赔率的区间。 所以我不得不做的就是把每天的赔率数据导入到...

    stefanieliang 评论0 收藏0
  • [No.003-0]爬虫网易赔率数据导入mysql数据

    摘要:所以我不得不做的就是把每天的赔率数据导入到我自己的数据库,根据一定的运算法则,将可能性筛选出来,然后我再做进一步的判断。 首先,我拥有这个导入赔率的需求,并且,我需要的是所有的数据赔率,我需要把某些脏赔率(极有可能不会出现的赔率,误导彩迷的赔率)删除,并且我需要知道这些赔率的比分,删除这些赔率,可以逆推正确比分以及赔率的区间。 所以我不得不做的就是把每天的赔率数据导入到...

    cikenerd 评论0 收藏0
  • [No.003-1]爬虫网易赔率数据导入mysql数据

    摘要:获取场次以及分数合集比如比分对应代码表对应对应胜其他对应平其他对应负其他因此场次和比分结合为,场次信息比分标题胜其他平其他负其他场次比分之后遍历得到结果集如场次为位数字,第一个为主场比分,中间为冒号,最后一个为客场比分平其他 #encoding:utf-8 import urllib2 from bs4 import BeautifulSoup website = http://ca...

    Warren 评论0 收藏0
  • [No.003-5]爬虫网易赔率数据导入mysql数据

    摘要:创建比赛结果数据库导入结果到导入到数据库中更新胜负数据插入更新胜场胜其他更新负场负其他更新平局平其他查看更新结果 创建比赛结果数据库 CREATE TABLE `results` ( `id` char(14) NOT NULL, `scr` char(3) NULL , `lea` char(100) NULL , `gmd` date NULL , `hos` char...

    ybak 评论0 收藏0
  • [No.003-3]爬虫网易赔率数据导入mysql数据库--MySQL

    摘要:创建数据库以及表,并导入数据创建数据库创建表胜分数比分平负导入数据导入到数据库中 创建数据库以及表,并导入数据 --创建数据库 CREATE DATABASE `data` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; --创建表 --win w 胜 0 分数 1 比分 --draw d 平 --lose l 负 C...

    ztyzz 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<