UPS:通过网络抓取进行AWB跟踪

时间:2013-08-20 19:07:51

标签: c# excel httpwebrequest web-crawler webclient

问题

在工作中,人们花费大量时间来跟踪来自不同来源(UPS,FedEx,DHL,......)的AWB(航空法案)。因此,为了节省宝贵的时间,我被要求改进过程,我正在考虑使用Excel作为Excel-DNA& amp;平台来实现这一目标。 C#但我一直在尝试一些测试(爬行UPS)但没有成功。

测试

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=es_MX&Requester=UPSHome&WBPM_lid=homepage%2Fct1.html_pnl_trk&trackNums=5007052424&track.x=Rastrear");

request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add("Accept-Language: es-ES,es;q=0.8");
request.Headers.Add("Accept-Encoding: gzip,deflate,sdch");
request.KeepAlive = false;
request.Referer = @"http://www.ups.com/";
request.ContentType = "text/html; charset=utf-8";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());

或者...

using (var client = new WebClient())
{
    var values = new NameValueCollection();

    values.Add("HTMLVersion", "5.0");
    values.Add("loc", "es_MX");
    values.Add("Requester", "UPSHome");
    values.Add("WBPM_lid", "homepage/ct1.html_pnl_trk");
    values.Add("trackNums", "5007052424");
    values.Add("track.x", "Rastrear");

    client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate,sdch";
    client.Headers[HttpRequestHeader.AcceptLanguage] = "es-ES,es;q=0.8";
    client.Headers[HttpRequestHeader.Referer] = @"http://www.ups.com/";
    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36";

    string url = @"https://wwwapps.ups.com/WebTracking/track?";
    byte[] result = client.UploadValues(url, values);

    System.IO.File.WriteAllText(@"C:\UPSText.txt", Encoding.UTF8.GetString(result));
}

但上述例子都没有按预期工作。

问题

是否可以通过网络抓取UPS来跟踪AWB?

注意

目前,我无法访问UPS API。

1 个答案:

答案 0 :(得分:-1)

我刚刚为它编写了我的脚本。诀窍是,有另一个网址,您可以在网址中包含跟踪号码并直接登陆页面。然后,您必须解析表格,因为xml标签不起作用。只是从标题偏移。