Python requests、bs4 查询地区停电信息

夏天停电实在是太烦了,供电局只会在停电之后发来一条讯息:“很抱歉。。。”
有什么用呢?为什么不提前通知我呢?这么热的天,你不热的吗?
算了,不抱怨了,自己动手吧,写一个查询的程序放服务器,这样停电前几天就可以得知信息了,然后停电的时候就提前去 蹭 蹭 蹭 蹭空调!!!动手吧!!!!
需要注意的是,代码有些繁杂、冗余。。。另外,只限于河南地区 我不是有偏见,因为这个公众号它只能查河南。恰好我也是河南。

查询接口以及运行环境

接口:微信 河南停电信息查询 公众号
运行环境:python 3.5.4
所需库:requests bs4

三大模块

选择查询地区

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1
def cout_message(text = ''):
	# 说明:该函数为一个被调用的函数,单独来看可能并没有什么意义
    os.system('cls')

	# 众字典的说明,字典中保存的编号则为调用接口时,需要提交的参数。
    if text == '安阳':
        datas = {"安阳":"41405","内黄县":"4140522","汤阴县":"4140522","滑县":"4140523","安阳县":"4140524","林州市":"4140525",}
    
    elif text == '鹤壁':
        datas = {"鹤壁":"41413","淇县":"4141322","浚县":"4141322",}
    
    elif text == '济源':
        datas = {"济源":"41418",}
    
    elif text == '焦作':
        datas = {"焦作":"41408","温县":"4140822","孟州市":"4140822","修武县":"4140823","博爱县":"4140824","沁阳市":"4140825","武陟县":"4140826",}
    
    elif text == '开封':
        datas = {"开封":"41406","兰考县":"4140622","杞县":"4140622","通许县":"4140623","尉氏县":"4140624","开封县":"4140625",}
 
    elif text == '洛阳':
        datas = {"洛阳":"41404","栾川县":"4140422","洛宁县":"4140422","孟津县":"4140423","新安县":"4140424","偃师市":"4140425","宜阳县":"4140426","嵩县":"4140428","汝阳县":"4140429",}
        
    elif text == '南阳':
        datas = {"南阳":"41403","邓州市":"4140322","方城县":"4140322","南召县":"4140324","社旗县":"4140325","唐河县":"4140326","桐柏县":"4140327","西峡县":"4140328","淅川县":"4140329","新野县":"4140330","镇平县":"4140331",}
        
    elif text == '平顶山':
        datas = {"平顶山":"41402","叶县":"4140222","汝州市":"4140222","宝丰县":"4140223","郏县":"4140224","鲁山县":"4140225","平顶山市华辰":"4140226","华辰石龙区":"4140227",}
        
    elif text == '濮阳':
        datas = {"濮阳":"41410","濮阳县":"4141022","清丰县":"4141022","范县":"4141023","台前县":"4141024","南乐县":"4141025",}
        
    elif text == '三门峡':
        datas = {"三门峡":"41409","陕县":"4140922","渑池县":"4140922","灵宝市":"4140923","卢氏县":"4140924",}
        
    elif text == '商丘':
        datas = {"商丘":"41412","永城":"4141222","宁陵县":"4141222","民权县":"4141223","夏邑县":"4141224","睢县":"4141225","虞城县":"4141226","柘城县":"4141227",}
        
    elif text == '信阳':
        datas = {"信阳":"41416","固始县":"4141622","新县":"4141622","商城县":"4141623","息县":"4141624","淮滨县":"4141625","罗山县":"4141626","潢川县":"4141627","光山县":"4141628",}
        
    elif text == '新乡':
        datas = {"新乡":"41407","卫辉市":"4140722","获嘉县":"4140722","辉县市":"4140723","长垣县":"4140724","新乡县":"4140725","原阳县":"4140726","封丘县":"4140727","延津县":"4140728",}
        
    elif text == '许昌':
        datas = {"许昌":"41414","长葛市":"4141422","鄢陵县":"4141422","禹州市":"4141423","许昌县":"4141424","襄城县":"4141425",}
        
    elif text == '郑州':
        datas = {"郑州":"41401","登封市":"4140122","巩义市":"4140122","荥阳市":"4140123","新郑市":"4140124","新密":"4140125","航空港区":"4140127",}
        
    elif text == '周口':
        datas = {"周口":"41417","郸城县":"4141722","扶沟县":"4141722","淮阳县":"4141723","鹿邑县":"4141724","沈丘县":"4141725","项城市":"4141726","商水县":"4141727","太康县":"4141728","西华县":"4141729","泛区局":"4141730",}
        
    elif text == '驻马店':
        datas = {"驻马店":"41415","泌阳县":"4141522","平舆县":"4141522","汝南县":"4141523","上蔡县":"4141524","遂平县":"4141525","西平县":"4141526","新蔡县":"4141527","确山县":"4141528","正阳县":"4141529",}
        
    else:
        return 'False','None'
		
	# .keys()  字典中的操作函数,返回所有的键名称
    for data in datas.keys():
        print(data)

	# 循环目的,检查查询地区是否包含在所选地区的可查询范围内
    while True:
        keyword = input('要查询的地区:')
        if keyword in datas.keys():
            break
        else:
            print('输入错误,检查后重新输入')
        
    # 最后返回查询地区名字以及该地区的编号
    return keyword,datas[keyword]

调用接口,得到信息

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1
def get_weather(organ = '',organText = '',location = ''):
    os.system('cls')
    
    # 调用的接口,得到的方式,从搜狗搜索引擎,找到 微信 这一项,搜索 河南停电信息查询 公众号,会得到该查询入口,点击进入即可得到此接口
    url = 'http://hndlwx.ha.sgcc.com.cn/powerwx/StopPowerServlet'

	# 请求头应该就不用说了,大家都会的就不需要啰嗦了吧,如有疑问,微信 Be_a_lucky_dog 一起交流吧
    headers = {
        'Referer': 'http://hndlwx.ha.sgcc.com.cn/powerwx/stopPower.jsp',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
        'Cookie': 'JSESSIONID=bOyCipAFT-aoHdd3dEXgFc3Ik2q2x6x8axO5aqQE4HqeolOi1NdQ!-36260657',
        'Host': 'hndlwx.ha.sgcc.com.cn',
        'Origin': 'http://hndlwx.ha.sgcc.com.cn',
        'X-Requested-With': 'XMLHttpRequest'
    }

	# 需要提交的表单  organ:查询地区的编号  organText:查询地区的名字
    params = {
        'organ': organ,
        'organText': organText
    }

	# 如果自己去亲手实验一番,自己会得知该请求为 post 请求
    response = requests.post(url = url,headers = headers,params = params).text
    data_1 = bp(response, 'lxml')
    data_2 = data_1.find_all(class_ = 'pw')
    if len(data_2) == 0:
        print('暂无停电信息')
        return None

	# 从此处开始筛选并且输出得到的信息
    print('停电信息如下,若无输出,则表明该地区不会停电\n')
    for data in data_2:
        data_tolocationANDtime = data.find_all(class_ = 'text')
	
		# 此处 if 判断 是为了区分是否填入了详细的查询地址,如果填写,则只输出该地区的信息
        if location == '':
        	# 这里建议自己从 data_tolocationANDtime[1] 开始,一步步看看到底输出的是什么
        	# 然后按照自己的思路去选择自己想要的信息,说白了,这里就是一个字符串的处理
            s_location = data_tolocationANDtime[1].text[4:].strip()
            s_time = data_tolocationANDtime[0].text[4:].strip()

			# 有心人可能会发现,这里调用了一个函数,此函数的作用,在下方会解释
            data_reason = deal_number(text=data.find(class_='resson').text)
            print('停电地区:' + s_location + '\n停电时段:' + s_time + '\n停电原因:' + data_reason + '\n')
        else:
        	# 参考上方的注释理解
            if location in str(data_tolocationANDtime):
                s_location = data_tolocationANDtime[1].text[4:].strip()
                s_time = data_tolocationANDtime[0].text[4:].strip()
                data_reason = deal_number(text = data.find(class_ = 'resson').text)
                print('停电地区:' + s_location + '\n停电时段:' + s_time + '\n停电原因:' + data_reason + '\n')

字符串的处理

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1
def deal_number(text = ''):
	# 这个函数实现的内容就是将字符串中的数字以及部分符号去除
    number = ['1','2','3','4','5','6','7','8','9','0','#','!','@','#','$','%','^','&','(',')',';']
    text_new = ''

	# 用到了字符串的切片处理以及索引搜索函数 .find() 
	# 如果自己去网站动手看了,会发现在停电原因会有(),()中填写的是一些推迟信息,其中包含日期
	# 所以不能将这些日期也去除数字,所以去除只进行到 () 之前
    for i in text[:text.find('(')]:
        if i in number:
            continue
        text_new += i
    text_new += text[text.find('('):]

    return text_new

完整源码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1
import requests
from bs4 import BeautifulSoup as bp
import os

def cout_message(text = ''):
    os.system('cls')
    if text == '安阳':
        datas = {"安阳":"41405","内黄县":"4140522","汤阴县":"4140522","滑县":"4140523","安阳县":"4140524","林州市":"4140525",}
    
    elif text == '鹤壁':
        datas = {"鹤壁":"41413","淇县":"4141322","浚县":"4141322",}
    
    elif text == '济源':
        datas = {"济源":"41418",}
    
    elif text == '焦作':
        datas = {"焦作":"41408","温县":"4140822","孟州市":"4140822","修武县":"4140823","博爱县":"4140824","沁阳市":"4140825","武陟县":"4140826",}
    
    elif text == '开封':
        datas = {"开封":"41406","兰考县":"4140622","杞县":"4140622","通许县":"4140623","尉氏县":"4140624","开封县":"4140625",}
 
    elif text == '洛阳':
        datas = {"洛阳":"41404","栾川县":"4140422","洛宁县":"4140422","孟津县":"4140423","新安县":"4140424","偃师市":"4140425","宜阳县":"4140426","嵩县":"4140428","汝阳县":"4140429",}
        
    elif text == '南阳':
        datas = {"南阳":"41403","邓州市":"4140322","方城县":"4140322","南召县":"4140324","社旗县":"4140325","唐河县":"4140326","桐柏县":"4140327","西峡县":"4140328","淅川县":"4140329","新野县":"4140330","镇平县":"4140331",}
        
    elif text == '平顶山':
        datas = {"平顶山":"41402","叶县":"4140222","汝州市":"4140222","宝丰县":"4140223","郏县":"4140224","鲁山县":"4140225","平顶山市华辰":"4140226","华辰石龙区":"4140227",}
        
    elif text == '濮阳':
        datas = {"濮阳":"41410","濮阳县":"4141022","清丰县":"4141022","范县":"4141023","台前县":"4141024","南乐县":"4141025",}
        
    elif text == '三门峡':
        datas = {"三门峡":"41409","陕县":"4140922","渑池县":"4140922","灵宝市":"4140923","卢氏县":"4140924",}
        
    elif text == '商丘':
        datas = {"商丘":"41412","永城":"4141222","宁陵县":"4141222","民权县":"4141223","夏邑县":"4141224","睢县":"4141225","虞城县":"4141226","柘城县":"4141227",}
        
    elif text == '信阳':
        datas = {"信阳":"41416","固始县":"4141622","新县":"4141622","商城县":"4141623","息县":"4141624","淮滨县":"4141625","罗山县":"4141626","潢川县":"4141627","光山县":"4141628",}
        
    elif text == '新乡':
        datas = {"新乡":"41407","卫辉市":"4140722","获嘉县":"4140722","辉县市":"4140723","长垣县":"4140724","新乡县":"4140725","原阳县":"4140726","封丘县":"4140727","延津县":"4140728",}
        
    elif text == '许昌':
        datas = {"许昌":"41414","长葛市":"4141422","鄢陵县":"4141422","禹州市":"4141423","许昌县":"4141424","襄城县":"4141425",}
        
    elif text == '郑州':
        datas = {"郑州":"41401","登封市":"4140122","巩义市":"4140122","荥阳市":"4140123","新郑市":"4140124","新密":"4140125","航空港区":"4140127",}
        
    elif text == '周口':
        datas = {"周口":"41417","郸城县":"4141722","扶沟县":"4141722","淮阳县":"4141723","鹿邑县":"4141724","沈丘县":"4141725","项城市":"4141726","商水县":"4141727","太康县":"4141728","西华县":"4141729","泛区局":"4141730",}
        
    elif text == '驻马店':
        datas = {"驻马店":"41415","泌阳县":"4141522","平舆县":"4141522","汝南县":"4141523","上蔡县":"4141524","遂平县":"4141525","西平县":"4141526","新蔡县":"4141527","确山县":"4141528","正阳县":"4141529",}
        
    else:
        return 'False','None'
		
    for data in datas.keys():
        print(data)
    while True:
        keyword = input('要查询的地区:')
        if keyword in datas.keys():
            break
        else:
            print('输入错误,检查后重新输入')
    return keyword,datas[keyword]


def deal_number(text = ''):
    number = ['1','2','3','4','5','6','7','8','9','0','#','!','@','#','$','%','^','&','(',')',';']
    text_new = ''
    for i in text[:text.find('(')]:
        if i in number:
            continue
        text_new += i
    text_new += text[text.find('('):]

    return text_new


def get_weather(organ = '',organText = '',location = ''):
    os.system('cls')
    url = 'http://hndlwx.ha.sgcc.com.cn/powerwx/StopPowerServlet'
    headers = {
        'Referer': 'http://hndlwx.ha.sgcc.com.cn/powerwx/stopPower.jsp',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
        'Cookie': 'JSESSIONID=bOyCipAFT-aoHdd3dEXgFc3Ik2q2x6x8axO5aqQE4HqeolOi1NdQ!-36260657',
        'Host': 'hndlwx.ha.sgcc.com.cn',
        'Origin': 'http://hndlwx.ha.sgcc.com.cn',
        'X-Requested-With': 'XMLHttpRequest'
    }
    params = {
        'organ': organ,
        'organText': organText
    }

    response = requests.post(url = url,headers = headers,params = params).text
    data_1 = bp(response, 'lxml')
    data_2 = data_1.find_all(class_ = 'pw')
    if len(data_2) == 0:
        print('暂无停电信息')
        return

    print('停电信息如下,若无输出,则表明该地区不会停电\n')
    for data in data_2:
        data_tolocationANDtime = data.find_all(class_ = 'text')
        if location == '':
            s_location = data_tolocationANDtime[1].text[4:].strip()
            s_time = data_tolocationANDtime[0].text[4:].strip()
            data_reason = deal_number(text=data.find(class_='resson').text)
            print('停电地区:' + s_location + '\n停电时段:' + s_time + '\n停电原因:' + data_reason + '\n')
        else:
            if location in str(data_tolocationANDtime):
                s_location = data_tolocationANDtime[1].text[4:].strip()
                s_time = data_tolocationANDtime[0].text[4:].strip()
                data_reason = deal_number(text = data.find(class_ = 'resson').text)
                print('停电地区:' + s_location + '\n停电时段:' + s_time + '\n停电原因:' + data_reason + '\n')


if __name__ == '__main__':
	# os.system('cls') 的作用是将控制台输出清空
    while True:
        keyword,keynumber = cout_message(input('欲查询市区(仅限河南,如:郑州):'))
        os.system('cls')
        if keyword == 'False':
            print('没有该城市信息或检查是否有错字')
        else:
            os.system('cls')
            print('可以不填次信息,直接回车即可。\n若不填写此信息,则搜索整个地区停电信息')
            location = input('输入查询小区或村庄(如:警安花园小区、安乐村):')
            get_weather(organ = keynumber,organText = keyword,location = location)

获取打包程序链接

点击这个链接: 就能下载, 提取码: csdn

如何挂在服务器

说明,我也是摸索阶段,可能并不是最好的方法

Linux:
自己在Linux写下程序 或 用 Winscp 上传到 服务器,利用contrab,创建定时任务,详细操作可以参考这篇文章

Windows server:
我不知道怎么创建定时任务,只会从程序的角度出发
这个不建议用,原因我也说不上来,大概是因为我菜所以找不到原因
方法就是:一天不停的运行,然后用 time.sleep() 停顿一定的时间开始一次任务,但是就有些鸡肋

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值