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

#encoding:utf-8
import urllib2
from bs4 import BeautifulSoup

website = "http://caipiao.163.com/order/jczq-hunhe/#from=leftnav"
page = urllib2.urlopen(website)
soup = BeautifulSoup(page)


'''
获取场次以及分数合集比如
比分对应代码表:
11对应:1:1
70对应:胜其他
77对应:平其他
07对应:负其他
因此场次和比分结合为,017-10,017-20,017-21
'''
#场次信息 screening
i = 1
screening = []
for item in soup.findAll("span",{"class":"co1"}):
    screening.append(item.i.string+'\n')
    i+=1

sc = open('sc.txt','w')
sc.writelines(screening)
sc.close()

#比分标题 bifen
bifen=["1:0","2:0","2:1","3:0","3:1","3:2","4:0","4:1","4:2","5:0","5:1","5:2","胜其他","0:0","1:1","2:2","3:3","平其他","0:1","0:2","1:2","0:3","1:3","2:3","0:4","1:4","2:4","0:5","1:5","2:5","负其他"]

#场次+比分:ccbf
ccbf = []
for item_jtip in screening:
    for item_bifen in bifen:
        ccbf.append(item_jtip+item_bifen)

#之后遍历ccbf
for item in ccbf:
    print item

#得到结果集如(场次为3位数字,第一个为主场比分,中间为冒号,最后一个为客场比分):
0281:1
0282:2
0283:3
028平其他
0280:1
0280:2
0281:2
0280:3
0281:3
0282:3
0280:4
0281:4
0282:4
0280:5
0281:5

#----------------
'''
这里因为使用的是python2,所以需要将str->Unicode
具体参考:
http://blog.csdn.net/mindmb/article/details/7898528
'''

#建立比分赔率字典bfpl
#获取比分赔率
bfpl = []
for item in soup.findAll("td",{"gametype":"bf"}):
    bfpl.append(item.find("div").string+'\n')

#写入到文件bf.txt
bf = open('bf.txt','w')
bf.writelines(bfpl)
bf.close()

#组合
bfdata = {}
bf = dict(zip(ccbf,bfpl))
#--------------------
#出现错误!!!
#bfpl获取的数量和ccbf数量不一致,重新使用一个队列,同时获取场次和比分的赔率数据放置在一个队列中


    原文作者:Profeel
    原文地址: https://segmentfault.com/a/1190000000651601
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞