python3 使用requests爬取 双色球所有中奖号码,并使用pandas+matplotlib 进行分析

首先需要写一个爬虫将双色球开奖数据抓下来,找到了这个网站
http://datachart.500.com/ssq/
默认只显示最近30期的中奖号码
这里写图片描述

找到网站的所有中奖号码接口,
http://datachart.500.com/ssq/history/newinc/history.php?start=00001&end=18081
用浏览器打开是这个样子的
这里写图片描述
中文乱码了,不过无所谓,我们只需要数字
接下来开始获取这些数据,代码如下

import requests
from lxml import etree
url = "http://datachart.500.com/ssq/history/newinc/history.php?start=00001&end=18081"
response = requests.get(url)
response = response.text
selector = etree.HTML(response)
for i in selector.xpath('//tr[@class="t_tr1"]'):
    datetime = i.xpath('td/text()')[0]
    red = i.xpath('td/text()')[1:7]
    blue = i.xpath('td/text()')[7]
    print(datetime,red,blue)

效果如下,成功获取到了中奖号码
这里写图片描述

接下来用到pandas进行数据分析,统计每一个号码出现的次数
我们首先将红球中奖号码和篮球中奖号码分别放入两个数组中,并且将它们转化为Series,代码如下

import requests
from lxml import etree
import matplotlib.pyplot as plt
from pandas import Series

url = "http://datachart.500.com/ssq/history/newinc/history.php?start=00001&end=18081"
response = requests.get(url)
response = response.text
selector = etree.HTML(response)
reds = []
blues = []
for i in selector.xpath('//tr[@class="t_tr1"]'):
    datetime = i.xpath('td/text()')[0]
    red = i.xpath('td/text()')[1:7]
    blue = i.xpath('td/text()')[7]
    for i in red:
        reds.append(i)
    blues.append(blue)

s_blues = Series(blues)
s_blues = s_blues.value_counts()
s_reds = Series(reds)
s_reds = s_reds.value_counts()
print(s_blues)

打印蓝色球测试,结果如下
这里写图片描述
左边是蓝球的号码,右边是出现的次数,红色球也同理,但是这样看起来还不够直观,我们使用matplotlib进行绘图
只需要这样一段代码

import matplotlib.pyplot as plt
labels = s_blues.index.tolist()
sizes = s_blues.values.tolist()
rect = plt.bar(range(len(sizes)) , sizes , tick_label = labels)
plt.show()

效果如下
这里写图片描述
这样就可以很直观的看出哪个号码出现的频率最高了,但是这样无法显示准确的出现次数,我们需要加一个方法来让它显示。

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x(), 1.02*height, "%s" % height)


labels = s_blues.index.tolist()
sizes = s_blues.values.tolist()
rect = plt.bar(range(len(sizes)) , sizes , tick_label = labels)
autolabel(rect)
plt.show()

效果如下图
这里写图片描述
这样就更加清晰了。红球的图如下,方法类似就不做赘述
这里写图片描述

总之,最后的结果是
中奖率最高的几个号码是

01 08 14 20 22 26 + 12

仅供参考,如果有人中奖请记得联系我。
完整的代码如下

import requests
from lxml import etree
import matplotlib.pyplot as plt
from pandas import Series

url = "http://datachart.500.com/ssq/history/newinc/history.php?start=00001&end=18081"
response = requests.get(url)
response = response.text
selector = etree.HTML(response)
reds = []
blues = []
for i in selector.xpath('//tr[@class="t_tr1"]'):
    datetime = i.xpath('td/text()')[0]
    red = i.xpath('td/text()')[1:7]
    blue = i.xpath('td/text()')[7]
    for i in red:
        reds.append(i)
    blues.append(blue)

s_blues = Series(blues)
s_blues = s_blues.value_counts()
s_reds = Series(reds)
s_reds = s_reds.value_counts()

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x(), 1.02*height, "%s" % height)


labels = s_blues.index.tolist()
sizes = s_blues.values.tolist()
rect = plt.bar(range(len(sizes)) , sizes , tick_label = labels)
autolabel(rect)
plt.show()

labels2 = s_reds.index.tolist()
sizes2 = s_reds.values.tolist()
rect2 = plt.bar(range(len(sizes2)), sizes2 , tick_label = labels2)
autolabel(rect2)
plt.show()

github地址是
https://github.com/dangsh/hive/blob/master/newSpider/lottery/double.py
对爬虫感兴趣的朋友可以看一下我的其它文章或者github上的hive项目~希望对你们有所帮助

  • 5
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,那我们开始吧。 首先,我们需要安装必要的库,包括numpy、pandasmatplotlib。你可以通过以下命令来安装: ``` pip install numpy pandas matplotlib ``` 接下来,我们需要获取数据。我们可以从58同城的房产页面上爬取数据。这里我们使用Python中的requests库和BeautifulSoup库来实现。 ```python import requests from bs4 import BeautifulSoup url = 'https://cd.58.com/ershoufang/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} r = requests.get(url, headers=headers) soup = BeautifulSoup(r.text, 'html.parser') house_list = soup.find_all('div', {'class': 'property_content'}) ``` 现在我们已经成功获取了房屋列表。接下来,我们需要将它们保存到一个数据框中,以便进行分析。 ```python import pandas as pd data = [] for house in house_list: title = house.find('h3').text.strip() info_list = house.find('div', {'class': 'property_des'}).find_all('p') area = info_list[0].text.strip() layout = info_list[1].text.strip() direction = info_list[2].text.strip() floor = info_list[3].text.strip() age = info_list[4].text.strip() price = house.find('div', {'class': 'property_price'}).find('p').text.strip() data.append({'title': title, 'area': area, 'layout': layout, 'direction': direction, 'floor': floor, 'age': age, 'price': price}) df = pd.DataFrame(data) ``` 现在我们已经成功将数据保存到一个数据框中。我们可以开始对数据进行分析了。 首先,我们可以查看数据的基本统计信息。 ```python print(df.describe()) ``` 接下来,我们可以绘制一个房价分布图,以了解房价的分布情况。 ```python import matplotlib.pyplot as plt plt.hist(df['price'], bins=20) plt.xlabel('Price') plt.ylabel('Count') plt.show() ``` 我们还可以绘制一个散点图,以了解房价与面积之间的关系。 ```python plt.scatter(df['area'], df['price']) plt.xlabel('Area') plt.ylabel('Price') plt.show() ``` 最后,我们可以绘制一个箱线图,以了解不同户型的房价分布情况。 ```python import seaborn as sns sns.boxplot(x='layout', y='price', data=df) plt.show() ``` 这样,我们就完成了58同城成都房价的数据分析

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值