写个九价疫苗消息提醒,这适用于知苗易约
才分享不久,还有所欠缺,自己的代码知识函数很差 请大家多多关照。
这个代码仅能提醒
这段代码是 每个城市对应的代码 用字典的形式存储的
citycode = {"重庆市": "500000","成都市": '510100',
"自贡市": "510300", "攀枝花市": "510400",
"泸州市": "510500", "德阳市": "510600",
"绵阳市": "510700", "广元市": "510800",
"遂宁市": "510900", "内江市": "511000",
"乐山市": "511100", "南充市": "511300",
"眉山市": "511400", "宜宾市": "511500",
"广安市": "511600", "达州市": "511700",
"雅安市": "511800", "巴中市": "511900",
"资阳市": "512000", "阿坝藏族羌族自治州": "513200",
"甘孜藏族自治州": "513300", "凉山彝族自治州": "513400"}
这段代码需要修改一下 用于自己的钉钉机器人进行提醒 我这里的代码是 九价 关键词
DWebHook = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx' # 需要自己修改【不会的在CSDN上查找很简单的】
总的代码如下:
#-*- coding: utf-8 -*-
# text:疫苗名称
# id : 医院名称(用于获取)
# date : 预约时间
import json
import time
import requests
import urllib.parse
import urllib.request
from dingtalkchatbot.chatbot import DingtalkChatbot
# 每个省每个城市的医院({province_url}: 省 {citycode_url} : 城市 {city_code} : 城市代码) 这些必须转换为url可识别的
url = "https://api.cn2030.com//sc/wx/HandlerSubscribe.ashx?act=CustomerList&city=%5B%22" \
"{province_url}" \
"%22%2C%22" \
"{citycode_url}" \
"%22%2C%22%22%5D&lat=30.83664&lng=106.97299&id=0&cityCode=" \
"{city_code}&product=0"
# 每个城市中医院的地址,{city_id}为城市的id通过 replace进行替换
url2 = "https://api.cn2030.com//sc/wx/HandlerSubscribe.ashx?act=CustomerProduct&id={city_id}&lat=30.83664&lng=106.97299"
# 钉钉机器人的钉钉WebHook地址
DWebHook = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx' # 需要自己修改【不会的在CSDN上查找很简单的】
list = [] # 存储城市id
list2 = [] # 存储含有预约信息的字典
dict2 = {} # 以字典的形式存储有九价的信息
new_str = '' # 存储转换为字符串形式的九价信息 目的是钉钉机器人只能发送字符串
# 爬取网页
def get_id(url_province):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
ret = requests.get(url=url_province, headers=headers) # 请求网页
b = ret.text # 转换为文本形式
dict1 = json.loads(b) # 转换为json格式
city_list = dict1['list'] # 查找字典key为list的变量
return city_list
# 钉钉机器人发送消息的代码
def Dingding(text):
bot = DingtalkChatbot(webhook=DWebHook)
bot.send_text("九价消息!\n" + text,is_at_all=True)
print('*'*10,"开始爬取九价消息!",'*'*10)
# 设置字典对应每个城市的代码
citycode = {"重庆市": "500000","成都市": '510100',
"自贡市": "510300", "攀枝花市": "510400",
"泸州市": "510500", "德阳市": "510600",
"绵阳市": "510700", "广元市": "510800",
"遂宁市": "510900", "内江市": "511000",
"乐山市": "511100", "南充市": "511300",
"眉山市": "511400", "宜宾市": "511500",
"广安市": "511600", "达州市": "511700",
"雅安市": "511800", "巴中市": "511900",
"资阳市": "512000", "阿坝藏族羌族自治州": "513200",
"甘孜藏族自治州": "513300", "凉山彝族自治州": "513400"}
# 循环遍历每个省的城市代码
for t,citycode in citycode.items():
if t == "重庆市":
province = t
province_url = urllib.parse.quote(province) # 将汉字转换成url可识别的
citycode_url = urllib.parse.quote(t)
url_procince = url.replace('{province_url}',province_url) # 将省替换成URL可识别的
url_procince = url_procince.replace('{citycode_url}',citycode_url) # 将城市替换成URL可识别的
url_procince = url_procince.replace('{city_code}', citycode) # 将城市代码替换成URL可识别的
for i in get_id(url_procince):
city_id = i['id']
list.append(city_id)
else:
province = "四川省"
province_url = urllib.parse.quote(province) # 将汉字转换成url可识别的
citycode_url = urllib.parse.quote(t)
url_procince = url.replace('{province_url}', province_url) # 将省替换成URL可识别的
url_procince = url_procince.replace('{citycode_url}', citycode_url) # 将城市替换成URL可识别的
url_procince = url_procince.replace('{city_code}', citycode) # 将城市代码替换成URL可识别的
# print(url_procince)
for i in get_id(url_procince): # 循环遍历 每个城市中医院的id
city_id = i['id']
print(i)
list.append(city_id) # 将医院的ID 加入列表中
time.sleep(0.9) # 休眠0.9秒 太快了 会报错
print(list)
# 遍历城市中含有的九价疫苗发放的地方
for i in list:
url_city = url2.replace('{city_id}',str(i))
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
ret = requests.get(url=url_city, headers=headers)
dict1 = json.loads(ret.text)
city_list = dict1['list']
for i in city_list: # 循环遍历每个城市
# city_id = i['id']
print(i)
if i['text'] == '九价人乳头瘤病毒疫苗':
if i['date'] == '暂无': # 若九价不含有就不行查找
False
else: # 含有九价则开始进行处理
name = dict1['cname']
date = i['date']
dict2['医院名字:'] = name
dict2['预约日期:'] = date
dict2['医院地址:'] = dict1['addr']
list2.append(dict2) # 通过列表的形式进行存储
time.sleep(0.9)
print(list2)
# 将含有字典的列表转换为 字符串的形式
for infos in list2:
for key,value in infos.items():
new_str = new_str + str(key) + str(value) + '; \n'
new_str=new_str.strip(',')
new_str += '\n'
# 以钉钉机器人的消息进行推送
Dingding(new_str)
print('*'*10,"爬取结束!---消息推送成功",'*'*10)
# f = urllib.parse.unquote(s) # 将url可识别的编码转换成汉字
代码写的没有多好请大家见谅! 可以多多交流!