首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

记开发北京小汽车摇号中签查询小程序

记开发北京小汽车摇号中签查询小程序

背景

- 北京摇号中签概率越来越低,要了五年没中,6月份排的新能源,查询只能看到注册日期,看不到预计购买日期,索性自己写个程序看看能不能帮跟我一样想法的人查询到预计购买日期。

于是就有了这个文章。

准备工作

- 注册一个微信小程序

- 开发小程序

- 准备数据

github

本文中的程序等整理之后会上传到github,供学习交流。

本人不是python和nodejs的专职开发者,代码质量就不要提出来了。

国庆期间花了几天时间做出来,主要目的是能运行。

需求分析

- 输入申请编号,查询新能源预计购买日期

- 输入申请编号,查询是否中签

- 输入申请编号,可以收藏,以便查询

开始开发

对于开发人员,开发小程序不是什么难题,但是数据该怎么弄呢?

没有开发数据接口,只能从官网爬数据。

官网:https://apply.bjhjyd.gov.cn/(北京市小客车指标调控管理信息系统)

开始写爬虫

要说写爬虫,肯定是先模拟用户行为,找出规律,获取登录态,获取cookie,用爬虫程序模拟用户行为。

1. 新能源排队数据

- 2018年的新能源指标共计60000个,个人54000个指标,单位6000个指标。2018年的一月份已经发放完。按照目前的政策,个人指标每年有54000个,当前排队355283人,所以理论上已经排到2024年了。

模拟正常用户查询行为

- 登陆北京市小客车指标调控管理信息系统

- 手机号、密码登录

- 进入“新能源审核通过编码”查询页面,并且打开开发者工具

- 输入验证码,点击查询,可以看到,到2018/08/26为止,已经有35w多人排队。

- 查看network部分,把请求和返回结果分析

- 请求URL是 https://apply.bjhjyd.gov.cn/apply/pool/personQuery.do

- method是post

- 请求参数: pageNo=1®Type=XNY&issueNumber=201804&applyCode=&validCode=m6wn

- pageNo: 分页

- regType: XNY(新能源)

- issueNumber:201804(摇号期数)

- applyCode:查询的申请编码(这里不输入则查询全部)

- validCode:图片验证码

- 把cookie部分记录好,准备爬虫用

- 返回结果是页面,不是JSON,所以需要html解析

- 通过上面分析,可以开始写爬虫了。

爬虫程序

写爬虫,python是不二的选择,httpclient就不多说了,页面解析部分用BeautifulSoup,注意,python版本是3.

- 每页返回17条数据,计算好totalPage,依次循环。

- 把爬过来的页面中的数据保存到指定文件中。

```

class Beijing:

empCount = 0

def __init__(self, pageNo, total):

self.pageNo = pageNo

self.total = total

def has_next(self):

if self.pageNo

return 1

return 0

def get_next(self):

self.pageNo = self.pageNo + 1

reqdata['pageNo'] = self.pageNo

print(data)

conn = http.client.HTTPSConnection('apply.bjhjyd.gov.cn')

conn.request('POST', '/apply/pool/personQuery.do', data, reqheaders)

res = conn.getresponse()

print("page:", self.pageNo, res.status)

return res.read()

def displayStatus(self):

print( "pageNo : ", self.pageNo, ", total: ", self.total)

def print_text(fo, html):

soup = BeautifulSoup(html,features="lxml")

global total_count

trs = soup.find(id="resulttable").find_all("tr")

fo.write("====================\n")

count = 0

for tr in trs:

tds = tr.find_all("td")

count = count + 1

total_count = total_count + 1

for td in tds:

print(td.text,"\t",end='')

fo.write(td.text+ "\t")

fo.write("\n")

print()

print("=======",count,"=======",total_count,"=======")

```

2. 普通指标查询

- 同理,regType=PTC,接着爬普通指标摇号历史数据。

- 在爬数据的过程中发现以下好玩的事情

-- 11-13年是每个月摇一次

-- 每个月指标大概2w左右

-- 14年之前,中签人的姓名是公开的

-- 14年开始2个月摇一次

-- 14年开始指标一直在递减

-- 到2018年每次摇号指标只有6333个。中签概率1/2000+

-- 在北京要中签,还是建议一起买彩票吧,不中签,万一彩票中了呢。。。

- 爬2011年的数据,需要十来分钟,无聊中去看了一下官网。

- 瞬间一万个XXX飞奔。

- 原来这些历史数据是官网直接提供下载的。

- 开始下载PDF

- 前功浪费一半,还得做PDF解析。

- 上网找了PDF解析库,顺便改了程序,开始运行。

- 不用一会的功夫,就把历史数据解析完了。

- 得到的是非结构化数据,我是要倒入到mysql的好不好?

- 再写个转换程序吧,每种类型的数据格式都还不一致,需要多次调整。

折腾了大半天,终于把历史数据整理出来了。

- 单位的

- 个人的

- 新能源

- 普通指标

小程序

- 按照需求开发页面和功能

- 导入wx_app目录到微信开发者工具,即可运行

服务器搭建

- 如果用java感觉有点笨重,最近在看nodejs,就选择用node来搭建API。

- 网上找到了一个不错的项目:https://github.com/wangweianger/web-performance-monitoring-system.git

- clone一下,把没必要的先删除,本人不太懂node,就以能运行为目的来开发。

- 建立数据库,插入清洗过的数据。

- 到server目录下 npm install

- npm run dev

- 看到如下启动日志即可说明启动成功

```

[01:31:57] Using gulpfile ~/apps/pek_car_lottery/server/gulpfile.js

[01:31:57] Starting 'nodemon'...

[01:31:57] Finished 'nodemon' after 192 ms

[01:31:57] Starting 'default'...

[01:31:57] Finished 'default' after 70 μs

[01:31:57] [nodemon] 1.18.4

[01:31:57] [nodemon] to restart at any time, enter `rs`

[01:31:57] [nodemon] ignoring: build/*.js dist/*.js src/assets/** nodemon.json .git node_modules/**/node_modules gulpfile.js

[01:31:57] [nodemon] watching: *.*

[01:31:57] [nodemon] watching extensions: js,json,html

[01:31:57] [nodemon] starting `node build/server.js`

[01:31:57] [nodemon] forking

[01:31:57] [nodemon] child pid: 5236

[01:31:57] [nodemon] watching 16 files

Router {

opts: {},

methods:

[ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ],

params: {},

stack:

[ Layer {

opts: [Object],

name: null,

methods: [Array],

paramNames: [],

stack: [Array],

path: '/api/check/xnw',

regexp: /^\/api\/check\/xnw(?:\/(?=$))?$/i },

Layer {

opts: [Object],

name: null,

methods: [Array],

paramNames: [],

stack: [Array],

path: '/api/check/lucky',

regexp: /^\/api\/check\/lucky(?:\/(?=$))?$/i } ] }

koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md src/app.js:83:73

koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md src/app.js:90:38

服务启动了:路径为:127.0.0.1:8098 orgin:https://servicewechat.com/

```

数据分析

- 这部分感觉比较敏感,而且包含了用户数据,暂不做分析结果分享。

- 有兴趣的同学可以自己分析。

体验小程序

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181017G00Y5G00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券