抓取快递100数据,进行快递查询
2014-11-06 15:17
411 查看
8月份换了份工作,是做物流行业相关系统的,有时会需要查询外部快递接口,进行快递跟踪查询,这里就抓取快递100数据进行简单介绍。
需要的朋友可以参考下。
先上效果图:
下面我们一步一步来讨论如何实现这样的功能。
由上图我们发现 快递100的查询路劲为:http://www.kuaidi100.com/query?type=yuantong&postid=5714113854&id=1&valicode=&temp=0.19689508604579842
重要的两个参数是type:快递公司代码, postid:快递单号
这样我们不用写代码,类就设计好了(有删改),我们使用了泛型类来处理Json数据。
运行效果就和上面第一张图一样了,源码都在上面了,需要的朋友可以参考下, 如果有帮助到你,请点击好文要顶按钮,谢谢支持.
需要的朋友可以参考下。
先上效果图:
下面我们一步一步来讨论如何实现这样的功能。
1、准备工作:获取快递100 Json数据
由上图我们发现 快递100的查询路劲为:http://www.kuaidi100.com/query?type=yuantong&postid=5714113854&id=1&valicode=&temp=0.19689508604579842
重要的两个参数是type:快递公司代码, postid:快递单号
2、设计代码--设计类
这里有一个小技巧,我们复制 json 数据。public class ExpressStack<T> { public string message { get; set; } public string nu { get; set; } public string ischeck { get; set; } public string com { get; set; } public string status { get; set; } public string condition { get; set; } public string state { get; set; } public List<T> data { get; set; } } public class Data { public string time { get; set; } public string context { get; set; } public string ftime { get; set; } }
这样我们不用写代码,类就设计好了(有删改),我们使用了泛型类来处理Json数据。
3、编写Action
这个就比较简单,一些json转换和输出public ActionResult Search() { return View(); } [HttpPost] public ActionResult Search(string type, string postId) { WebClient wClient = new WebClient(); wClient.Encoding = Encoding.UTF8; var response = wClient.DownloadString("http://www.kuaidi100.com/query?type=" + type + "&postid=" + postId); var oJson = JsonConvert.DeserializeObject<ExpressStack<Data>>(response); return View(oJson); }
4、View 显示数据
这里不做异常处理,大家可以自己加上@using KuaiDi100.Controllers; @model ExpressStack<Data> <h2>快递查询</h2> @using (Html.BeginForm("Search", "KuaiDi100", FormMethod.Post, new { @class = "form-inline margin-bottom-10 ", style = "margin-left:200px" })) { <div class="form-group"> <div class="col-md-10"> <label class="control-label">快递公司代码</label> <input type="text" name="type" class="form-control" /> </div> </div> <div class="form-group"> <label class="control-label">快递单号</label> <input type="text" name="postId" class="form-control" /> </div> <div class="form-group"> <div class="col-md-10"> <button class="btn btn-primary">查询</button> </div> </div> } <table class="table table-striped table-bordered table-hover table-full-width" style="overflow:scroll; margin-top:50px;"> <thead> <tr> <th>地点和跟踪进度</th> <th>时间</th> </tr> </thead> <tbody> @if (Model != null) { if (Model != null) { foreach (var item in Model.data) { <tr> <td>@item.context</td> <td>@item.time</td> </tr> } } else { <tr> <td style="text-align:center" colspan="2">暂无结果</td> </tr> } } else { <tr> <td style="text-align:center" colspan="2">暂无结果</td> </tr> } </tbody> </table>
运行效果就和上面第一张图一样了,源码都在上面了,需要的朋友可以参考下, 如果有帮助到你,请点击好文要顶按钮,谢谢支持.
相关文章推荐
- Laravel 调用快递100 进行实时快递查询和物流订阅
- Flex调用WebService进行数据查询和显示(调整)
- 数据表里面有DateTime字段, 日期和时间同时进行区间查询
- 数据表中查询日期字段为空的记录并进行赋值的方法
- MVC中使用Linq To Sql进行数据查询及分页
- 利用OpenDataSource、OPENROWSET进行分布式查询和数据的导入导出
- 【利用OpenDataSource、OPENROWSET进行分布式查询和数据的导入导出】
- 关于利用DataGrid进行数据查询、修改操作
- jqGrid与Struts2的结合应用(五) —— 结合Action类进行数据查询
- AGS进行SDE查询,大数据量白屏问题
- 关于对日期类型的数据进行模糊查询的问题
- Sqlserver 在查询分析器里如何访问远程的的数据库,进行数据查询更新等操作。
- SQL SERVER2000教程-第五章 处理数据 第十七节 使用CASE函数格式进行条件查询
- 对于百万条数据进行查询:自己对2万条数据进行的测试,答案是。。。
- Access使用查询--1.2.用选择查询进行分组数据的计算
- SQL SERVER2000教程-第五章 处理数据 第十六节 使用CHARINDEX函数代替Like进行数据查询
- 对MYSQL查询中有疑问的数据进行编码
- (转)如何有效的利用数据字典进行查询
- 用python进行分布式网页数据抓取(一)——设计
- 跨服务器进行数据查询中出现的问题