C# 微信 生活助手 空气质量 天气预报等 效果展示 数据抓取 (二)
此文主要是 中国天气网和中国环境监测总站的数据抓取 打算开放全部数据抓取源代码 已在服务器上 稳定运行半个月
webapi http://api.xuzhiheng.cn/

常量
/// <summary>
/// 环保部抓取数据链接
/// </summary>
public const string HBUrl = "http://datacenter.mep.gov.cn/report/air_daily/airDairyCityHour.jsp";
/// <summary>
/// 环境检测总站
/// </summary>
public const string HJJCZZUrl = "http://113.108.142.147:20035/emcpublish/ClientBin/Env-CnemcPublish-RiaServices-EnvCnemcPublishDomainService.svc/binary";
/// <summary>
/// 获取所有站点AQI数据
/// </summary>
public const string AQIDataUrl = HJJCZZUrl + "/GetAQIDataPublishLives";
/// <summary>
/// 获取所有城市AQI数据
/// </summary>
public const string CityDataUrl = HJJCZZUrl + "/GetCityAQIPublishLives";
/// <summary>
/// 中国天气网
/// </summary>
public const string WeatherUrl = "http://www.weather.com.cn";
/// <summary>
/// 中国天气网7天天气预报
/// </summary>
//public const string Weather7DUrl = WeatherUrl + "/weather/{0}.shtml";
public const string Weather7DUrl = "http://home.cw.uitv.com.cn/Weather.ashx?service=2&cityid={0}";
/// <summary>
/// 中国天气网实况天气预报
/// </summary>
//public const string WeatherSKUrl = "http://data.weather.com.cn/sk/{0}.html";
public const string WeatherSKUrl = "http://d1.weather.com.cn/sk_2d/{0}.html";
public const string WeatherSKDetailUrl = "http://flash.weather.com.cn/wmaps/xml/{0}.xml"; public static readonly string[] ChinaProvinces = new string[] { "heilongjiang", "jilin", "liaoning", "hainan", "neimenggu", "xinjiang", "xizang", "qinghai", "ningxia", "gansu", "hebei", "henan", "hubei", "hunan", "shandong", "jiangsu", "anhui", "shanxi", "sanxi", "sichuan", "yunnan", "guizhou", "zhejiang", "fujian", "jiangxi", "guangdong", "guangxi", "beijing", "tianjin", "shanghai", "chongqing", "xianggang", "aomen", "taiwan", "xisha", "nanshadao", "diaoyudao" }; /// <summary>
/// 风向
/// </summary>
public static readonly string[] WindDirections = new string[] { "无持续风向", "东北风", "东风", "东南风", "南风", "西南风", "西风", "西北风", "北风", "旋转风" };
/// <summary>
/// 风力
/// </summary>
public static readonly string[] WindForces = new string[] { "微风", "3-4级", "4-5级", "5-6级", "6-7级", "7-8级", "8-9级", "9-10级", "10-11级", "11-12级" };
/// <summary>
/// 天气
/// </summary>
public static readonly string[] Weathers = new string[] { "晴", "多云", "阴", "阵雨", "雷阵雨", "冰雹", "雨夹雪", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨", "阵雪", "小雪", "中雪", "大雪", "暴雪", "雾", "冻雨", "沙尘暴", "小到中雨", "中到大雨", "大到暴雨", "暴雨到大暴雨", "大暴雨到特大暴雨", "小到中雪", "中到大雪", "大到暴雪", "浮尘", "扬沙", "强沙尘暴", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "霾" };
/// <summary>
/// 质数代号
/// </summary>
public static readonly string[] Indexs = new string[] { "ag", "cl", "lk", "yh", "uv", "ls", "mf", "pj", "tr", "yd", "hc", "ac", "ct", "gj", "ys", "nl", "pk", "zs", "xc", "co", "dy", "fs", "gm", "jt", "xq", "pl", "pp" };
/// <summary>
/// 质数名称
/// </summary>
public static readonly string[] IndexNames = new string[] { "息斯敏过敏指数", "晨练指数", "路况指数", "约会指数", "紫外线强度指数", "晾晒指数", "美发指数", "啤酒指数", "旅游指数", "运动指数", "划船指数", "空调开启指数", "穿衣指数", "逛街指数", "雨伞指数", "夜生活指数", "放风筝指数", "中暑指数", "洗车指数", "舒适度指数", "钓鱼指数", "防晒指数", "感冒指数", "交通指数", "心情指数", "空气污染扩散条件指数", "化妆指数" };
中国天气网 没什么 要说的 直接抓取 就可以了 代码如下
7天天气预报 一天发布3次
foreach (int areaId in weatherAreaIds)
{
if (weather7DLastGrabTime[areaId] < DateTime.Today || ((weather7DLastGrabTime[areaId] == DateTime.Today.AddHours() && DateTime.Now.Hour > ) ||
(weather7DLastGrabTime[areaId] == DateTime.Today.AddHours() && DateTime.Now.Hour > ) ||
(weather7DLastGrabTime[areaId] == DateTime.Today.AddDays(-).AddHours() && DateTime.Now.Hour > )))
{
string res = GetRequest(string.Format(Constant.Weather7DUrl, areaId), "isexist=1");
try
{
JsonForecast jsonForecast = JsonConvert.DeserializeObject<JsonForecast>(res);
Forecast7D forecast7D = Mapper.Map<Qtyb, Forecast7D>(jsonForecast.qtyb);
forecast7D.City = jsonForecast.city;
forecast7D.CityId = areaId;
forecast7D.Time = jsonForecast.ftime;
if (jsonForecast.ftime == weather7DLastGrabTime[areaId])
{
Thread.Sleep();
continue;
}
lock (aqiEntities)
{
weather7DLastGrabTime[areaId] = forecast7D.Time;
aqiEntities.GrabDataConfig.FirstOrDefault(p => p.Key == areaId.ToString() && p.Type == ).Time = forecast7D.Time;
aqiEntities.Forecast7D.Add(forecast7D);
aqiEntities.SaveChanges();
Log(string.Format("【天气预报】 {0} 发布时间:{1} 获取时间:{2}", forecast7D.City, forecast7D.Time, DateTime.Now));
}
}
catch (Exception)
{
}
}
}
天气实况
string res = GetRequest(string.Format(Constant.WeatherSKDetailUrl, area), Constant.WeatherUrl, "").Replace(area, "JsonSKDetail");
using (StreamReader streamReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(res)), Encoding.UTF8))
{
try
{
JsonSKDetail jsonSKDetail = (JsonSKDetail)xmlSerializer.Deserialize(streamReader);
foreach (JsonSKDetailCity jsonSKDetailCity in jsonSKDetail.city)
{
WeatherSKDetail weatherSKDetail = Mapper.Map<JsonSKDetailCity, WeatherSKDetail>(jsonSKDetailCity);
weatherSKDetail.dn = jsonSKDetail.dn;
if (!weatherSKDetails.Exists(p => p.url == weatherSKDetail.url) && weatherSKDetailLastGrabTime[weatherSKDetail.url] != weatherSKDetail.time)
{
weatherSKDetails.Add(weatherSKDetail);
weatherSKDetailLastGrabTime[weatherSKDetail.url] = weatherSKDetail.time;
lock (aqiEntities)
{
aqiEntities.GrabDataConfig.FirstOrDefault(p => p.Type == && p.Key == weatherSKDetail.url.ToString()).Time = weatherSKDetail.time;
aqiEntities.SaveChanges();
}
}
if (!string.IsNullOrEmpty(jsonSKDetailCity.pyName))
{
GetSKDetail(jsonSKDetailCity.pyName);
}
}
}
catch (Exception ex)
{
string s = ex.Message;
}
}
中国环境检测总站
通过抓包发现用的是wcf 返回的是二进制xml文档 需要用到 一个 https://github.com/waf/WCF-Binary-Message-Inspector 把数据解析出来 同时他也是fiddler的一个插件 可以在里面直接看出

private void GetCityAQI()
{
bool isWriteFile = false;
if (DateTime.Now >= lastGrabCityAQITime.AddHours())
{
#region 获取数据
while (true)
{
try
{
XmlElement xmlElement = GetXmlElement(Constant.CityDataUrl);
XmlNode xn = xmlElement.LastChild.LastChild;
XmlNodeList xmlNodeList = xn.ChildNodes;
DateTime TimePoint = DateTime.Now;
if (xmlNodeList.Count > )
{
XmlElement xe = (XmlElement)xmlNodeList[];
TimePoint = DateTime.Parse(xe.GetElementsByTagName("b:TimePoint")[].InnerText);
if (TimePoint == lastGrabCityAQITime)
{
isWriteFile = false;
}
else
{
lastGrabCityAQITime = TimePoint;
isWriteFile = true;
}
}
#region 数据操作
if (isWriteFile)
{
foreach (XmlElement xe in xmlNodeList)
{
string Area = xe.GetElementsByTagName("b:Area")[].InnerText;
string PrimaryPollutant = xe.GetElementsByTagName("b:PrimaryPollutant")[].InnerText;
if (PrimaryPollutant == "—")
PrimaryPollutant = null;
int? AQI = xe.GetElementsByTagName("b:AQI")[].InnerText.ToNullable<int>();
int? SO2 = xe.GetElementsByTagName("b:SO2")[].InnerText.ToNullable<int>();
int? PM2_5 = xe.GetElementsByTagName("b:PM2_5")[].InnerText.ToNullable<int>();
int? PM10 = xe.GetElementsByTagName("b:PM10")[].InnerText.ToNullable<int>();
int? O3 = xe.GetElementsByTagName("b:O3")[].InnerText.ToNullable<int>();
int? NO2 = xe.GetElementsByTagName("b:NO2")[].InnerText.ToNullable<int>();
float? CO = xe.GetElementsByTagName("b:CO")[].InnerText.ToNullable<float>();
int CityCode = int.Parse(xe.GetElementsByTagName("b:CityCode")[].InnerText);
var model = new CityAQI()
{
CityCode = CityCode,
Area = Area,
O3 = O3,
CO = CO,
TimePoint = TimePoint,
AQI = AQI,
PrimaryPollutant = PrimaryPollutant,
NO2 = NO2,
PM10 = PM10,
PM2_5 = PM2_5,
SO2 = SO2
};
cityAQIs.Add(model);
}
lock (aqiEntities)
{
aqiEntities.CityAQI.AddRange(cityAQIs);
aqiEntities.GrabDataConfig.FirstOrDefault(p => p.Key == "CityAQI").Time = lastGrabCityAQITime;
aqiEntities.SaveChanges();
cityAQIs.Clear();
}
lb_log.Invoke((EventHandler)delegate
{
if (lb_log.Items.Count > )
{
lb_log.Items.Clear();
}
lb_log.Items.Add("中国环境检测总站 城市" + " " + TimePoint.ToString("yyyy-MM-dd HH:mm:ss") + "数据 " + DateTime.Now);
lb_log.SelectedIndex = lb_log.Items.Count - ;
});
Thread.Sleep( * );
}
#endregion
break;
}
catch (Exception)
{
Thread.Sleep( * );
}
}
#endregion
}
}
关注微信公众帐号获取最新更新微信号kxshzs
QQ:519872449 QQ群:77877965
C# 微信 生活助手 空气质量 天气预报等 效果展示 数据抓取 (二)的更多相关文章
- 微信运动数据抓取(Python)
"微信运动"能够向朋友分享一个包含有运动数据的网页,网页中就有我们需要的数据.url类似于:http://hw.weixin.qq.com/steprank/step/person ...
- 微信运动数据抓取(PHP)
“微信运动”能够向朋友分享一个包含有运动数据的网页,网页中就有我们需要的数据.url类似于:http://hw.weixin.qq.com/steprank/step/personal?openid= ...
- 中国空气质量在线监测分析平台之JS加密、JS混淆处理
中国空气质量在线监测分析平台数据爬取分析 页面分析:确定url.请求方式.请求参数.响应数据 1.访问网站首页:https://www.aqistudy.cn/html/city_detail.htm ...
- python微信聊天机器人改进版,定时或触发抓取天气预报、励志语录等,向好友推送
最近想着做一个微信机器人,主要想要实现能够每天定时推送天气预报或励志语录,励志语录要每天有自动更新,定时或当有好友回复时,能够随机推送不同的内容.于是开始了分析思路.博主是采用了多线程群发,因为微信对 ...
- scrapy结合selenium抓取武汉市环保局空气质量日报
1.前言 目标网站:武汉市环境保护局(http://hbj.wuhan.gov.cn/viewAirDarlyForestWaterInfo.jspx).scrapy对接selenium模块抓取空气质 ...
- 如何利用Python网络爬虫抓取微信朋友圈的动态(上)
今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...
- 《吐血整理》高级系列教程-吃透Fiddler抓包教程(34)-Fiddler如何抓取微信小程序的包-上篇
1.简介 有些小伙伴或者是童鞋们说小程序抓不到包,该怎么办了???其实苹果手机如果按照宏哥前边的抓取APP包的设置方式设置好了,应该可以轻松就抓到包了.那么安卓手机小程序就比较困难,不是那么友好了.所 ...
- 天气类API调用的代码示例合集:全国天气预报、实时空气质量数据查询、PM2.5空气质量指数等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 全国天气预报:数据来自国家气象局,可根据地名.经纬度GPS.IP查 ...
- 用python自制微信机器人,定时发送天气预报
0 引言 前段时间找到了一个免费的天气预报API,费了好段时间把这个API解析并组装成自己想用的格式了,就想着如何实现每天发送天气信息给自己.最近无意中发现了wxpy库,用它来做再合适不过了.以下是w ...
随机推荐
- UnitTest测试框架-操作步骤
一.UnitTest 1. TestCase 说明:测试用例 1.新建类并集成unittest.TestCase 2. TestSuite 说明:测试套件(多条用例) 方法: 1. 实例化 suite ...
- vue watch和computed的使用场景
watch 监听某个数据的变化(监听完调用什么函数) 一个数据影响多个数据 (比如:浏览器自适应.监控路由对象.监控自身属性变化) computed 计算后返回新 一个数据受多个数据影响(比如:计算总 ...
- python基础局部变量、全局变量
局部变量的作用域只作用与当前函数块(或代码块)中,对函数块(或代码块)之外的重名变量,没有任何影响. 在函数块(或代码块)中,局部变量可用通过global关键字声明变量来改变在函数块(或代码块)之外对 ...
- react 踩坑记
yarn node-sass 安装失败 yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass yarn i ...
- 详解服务器性能测试的全生命周期?——从测试、结果分析到优化策略(转载)
服务器性能测试是一项非常重要而且必要的工作,本文是作者Micheal在对服务器进行性能测试的过程中不断摸索出来的一些实用策略,通过定位问题,分析原因以及解决问题,实现对服务器进行更有针对性的优化,提升 ...
- 我的python面试简历
分享前一段我的python面试简历,自我介绍这些根据你自己的来写就行,这里着重分享下我的项目经验.公司职责情况(时间倒序),不一定对每个人适用,但是有适合你的点可以借鉴 我的真实经验:(14年毕业,化 ...
- MFC中文件对话框类CFileDialog详解及文件过滤器说明
当前位置 : 首页 » 文章分类 : 开发 » MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...
- MySQL 的 RowNum 实现(排行榜计算用户排名)
1. 计算用户排名最高效的方法 例如:通过用户分享个数排名,那么自己的排名就是:比自己分享数多的用户个数 + 1 ' and `count` > '自己分享个数' 缺点:当多个用户分享个数相同的 ...
- 关于PHPExcel的一些资料
下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...
- 5种方法获取url中文件的扩展名
/** * strrchr - 查找指定字符在字符串中的最后一次出现 * strrpos — 计算指定字符串在目标字符串中最后一次出现的位置 * end — 将数组的内部指针指向最后一个单元 * pa ...