import requests
from fack_useragent import UserAgent
from lxml import etree
import pymysql
url = ‘http://datachart.500.com/ssq/’
提取数据
response = requests.get(url, headers={“UserAgent”:UserAgent().chrome})
e = etree.HTML(response.text)
data_times = e.xpath(‘//tbody[@id=“tdata”/tr/td[1]/text))
trs = e.xpath(’//tbody[@id=“tdata”/tr[not(@class)]')
链接数据库
client = pymysql.connect(host=‘localhost’,port=3306,user=‘root’,password=‘123’,charset=‘utf-8’,db=‘ball’)
cursor =client.cursor()
sql = ‘insert into t_ball values (0,%s,%s,%s)’
select_new_sql = “select * from t_ball where data_time=%s”
for data_time in data_times.reverse:
result = cursor.execute(selete_new_sql,[data_time])
break
for data_time , tr in zip(data_times , trs):
if count % 5 ==0:
continue
count+=1
red_ball = “_”.join(tr.xpath(‘./td[@class=“chartBa1101”]/text()’))
bule_ball = tr.xpath(‘./td[@class=“chartBa1102”]/text()’)[0]
print(“第” + data_time +“红球是” + red_ball + "绿球是” + bule_ball)