美文网首页
python爬虫:通过喜付编写爬虫查寻电费

python爬虫:通过喜付编写爬虫查寻电费

作者: FireKingY | 来源:发表于2017-08-29 14:59 被阅读0次

效果

剩余电费:17.85元
剩余电量:38.81度

第一步:准备工作

1:下载fiddler
 2:利用fiddler监视手机
   参考:利用Fiddler抓取APP数据

第二步:抓包

用手机打开喜付并登录,点击查看电费一栏。通过观察分析fiddler中的数据,我们可以找到查询电费的请求:

QQ截图20170829143543.jpg
如果response中有乱码请参考: Fiddler中response乱码的解决方案
于是发现其实查询电费的界面就是一个网页,url:https://www.xifuapp.com/school/h5/electricity/info.action?access_token=xxxxxxxxxxxxxxxxxx,access_token不同的人不一样。

第三步:code

剩下的简单了,用requests库get一下,再用正则表达式提取一下数据就好了。
完整代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import re


def query():
    text = []
    #你得到的url
    url = 'https://www.xifuapp.com/school/h5/electricity/info.action?access_token=xxxxxxxx'
    try:
        r = requests.get(url, timeout=10)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        text = r.text
    except:
        print('查寻出错')
    ele = re.findall(r'(?<=nowE = ").{0,5}(?=";)', text)[0]
    fee = re.findall(r'(?<=nowM = ").{0,5}(?=";)', text)[0]
    return (float(fee), float(ele))


if __name__ == '__main__':
    res = query()
    print('剩余电费:%.2f元\n剩余电量:%.2f度' % (res[0], res[1]))

相关文章

网友评论

      本文标题:python爬虫:通过喜付编写爬虫查寻电费

      本文链接:https://www.haomeiwen.com/subject/fquodxtx.html