利用Python获取历史双色球开奖结果

import requests
import pymssql
from lxml import etree

#配置连接数据库信息
server = "-------------" #数据库连接
user = "sa" #用户名
password = "-------------" #密码
conn = pymssql.connect(server, user, password, "test")
cursor = conn.cursor()

url = "http://datachart.500.com/ssq/history/newinc/history.php?start=19096&end=19098" #双色球开奖结果查询页面,start和end对应的是期数
response = requests.get(url)
response = response.text
selector = etree.HTML(response)
count=0
for i in selector.xpath('//tr[@class="t_tr1"]'):
    count=count+1
    datetime = i.xpath('td/text()')[0] #期数
    a = i.xpath('td/text()')[1]#红球1
    b = i.xpath('td/text()')[2]#红球2
    c = i.xpath('td/text()')[3]#红球3
    d = i.xpath('td/text()')[4]#红球4
    e = i.xpath('td/text()')[5]#红球5
    f = i.xpath('td/text()')[6]#红球6
    g = i.xpath('td/text()')[7]#篮球
    print(datetime,a,b,c,d,e,f,g) 
    sql = "insert into DichroicBall (Phase,A,B,C,D,E,F,G) values("+datetime+","+a+","+b+","+c+","+d+","+e+","+f+","+g+");COMMIT"
    print("sql:"+sql)
    try:
       cursor.execute(sql)
       print("插入成功")
    except:
       conn.rollback()
       print("插入失败")
print("总共:"+str(count))
cursor.close()
conn.close()

Demo连接:https://code.aliyun.com/OneGongshen/Python.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小目标一个亿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值