如何爬取每天的彩票数

import lxml.html
import requests
import pymongo

client=pymongo.MongoClient(host=‘localhost’,port=27017)#连接mongodb
db=client.caipiao
collection=db.SSQ

def parse_xpath(html_str):
html=lxml.html.fromstring(html_str)
#拿到每天日期的彩票
tr_list = html.xpath(’//tbody[@id=“cpdata”]//tr[@class=""] | //tr[@class=“lastRow”]’)
#对对象进行遍历
for tr in tr_list:
#拿到每天的彩票日期
data=tr.xpath(’.//td[1]/text()’)[0]
#红色球
red_temp = tr.xpath(’.//td[@class=“ball_red”]/text()’)
red=’,’.join(red_temp)
#橘色球
orange_temp = tr.xpath(’.//td[@class=“ball_brown”]/text()’)
orange = ‘,’.join(orange_temp)
#蓝色球
blue_temp = tr.xpath(’.//td[@class=“ball_blue js-fold”]/text()’)
blue = ‘,’.join(blue_temp)
#将每天的球添加到一个字典中
value={
“red”:red,
“orange”:orange,
“blue”:blue
}
print(value)
#将字典中的数据添加到mongodb中,无则添加,有则更新
collection.update({’_id’: data}, {’$set’: value}, upsert=True)

result=requests.get(‘http://trend.caipiao.163.com/ssq/’)
result=parse_xpath(result.text)
print(result)

猜你喜欢

转载自blog.csdn.net/qq_39095977/article/details/85242273