如何从网址获取特定内容

时间:2019-02-13 15:01:21

标签: shell unix wget

我想递归地使用不同的跟踪编号打入下面的URL,并仅从URL获取货运状态

UPS URL: https://www.ups.com/track?loc=en_US&tracknum=123456789012345678&requester=WT/trackdetails

Sample Status of Tracking Number

我尝试了wget命令,但是它无法加载整个文件,并且对我没有用,curl在我的系统中不起作用。

wget --no-check-certificate --output-document ttt.htm "$UPS_URL"

任何其他解决方法都将非常有帮助。

1 个答案:

答案 0 :(得分:0)

您使用错误的页面和请求方法来获取数据。我访问了您链接到的页面,它仅包含一个文本框,您可以在其中添加货件号,然后将您重定向到包含结果的页面。

因此,仅在此页面上调用Wget不会执行任何操作。您需要知道要获取详细信息的精确URI。我在浏览器上打开了网络工具,并看到了网络请求。单击提交后,它将发送一个POST请求,并且对它的响应包含您需要的所有数据。从那里提取,我建立了这个请求:

$ cat ups-request
{
  "Locale": "en_US",
  "TrackingNumber": [
    "45234534263"
  ]
}

这是一个JSON字符串,其中包含您要跟踪的所有包裹号。您甚至可以添加更多数字(该网站声称它最多可以处理25个数字)。文件准备好后,调用Wget:

$ wget --method=POST --body-file=ups-request --header="Content-Type: application/json" "https://www.ups.com/track/api/Track/GetStatus?loc=en_US"

在这种情况下,我们通过要求WGET命令发送POST请求(--method=POST,发送在主体中创建的JSON请求(--body-file=ups-request)并告诉Wget来构建Wget命令该请求的类型为JSON(--header="Content-Type: application/json")。然后,服务器会以请求的完整详细信息进行回复:

{
  "statusCode": "200",
  "statusText": "Successful",
  "isLoggedInUser": false,
  "trackedDateTime": "02/14/2019 7:53 A.M. EST",
  "isBcdnMultiView": false,
  "trackDetails": [
    {
      "errorCode": "504",
      "errorText": "Tracking number not found in database",
      "requestedTrackingNumber": "45234534263",
      "trackingNumber": "45234534263",
      "isMobileDevice": false,
      "packageStatus": null,
      "packageStatusType": null,
      "packageStatusCode": null,
      "progressBarType": null,
      "progressBarPercentage": null,
      "simplifiedText": null,
      "scheduledDeliveryDayCMSKey": null,
      "scheduledDeliveryDate": null,
      "noEstimatedDeliveryDateLabel": null,
      "scheduledDeliveryTime": null,
      "scheduledDeliveryTimeEODLabel": null,
      "packageCommitedTime": null,
      "endOfDayResCMSKey": null,
      "deliveredDayCMSKey": null,
      "deliveredDate": null,
      "deliveredTime": null,
      "receivedBy": null,
      "leaveAt": null,
      "leftAt": null,
      "shipToAddress": null,
      "shipFromAddress": null,
      "consigneeAddress": null,
      "signatureTrackingUrl": null,
      "trackHistoryDescription": null,
      "additionalInformation": null,
      "specialInstructions": null,
      "proofOfDeliveryUrl": null,
      "upsAccessPoint": null,
      "additionalPackagesCount": null,
      "attentionNeeded": null,
      "shipmentProgressActivities": null,
      "trackingNumberType": null,
      "preAuthorizedForReturnData": null,
      "shipToAddressLblKey": null,
      "trackSummaryView": null,
      "senderShipperNumber": null,
      "internalKey": null,
      "userOptions": null,
      "sendUpdatesOptions": null,
      "myChoiceUpSellLink": null,
      "bcdnNumber": null,
      "promo": null,
      "whatsNextText": null,
      "packageStatusTimeLbl": null,
      "packageStatusTime": null,
      "myChoiceToken": null,
      "showMycTerms": false,
      "showConfirmWindow": false,
      "confirmWindowLbl": null,
      "confirmWindowLink": null,
      "followMyDelivery": null,
      "fileClaim": null,
      "viewClaim": null,
      "flightInformation": null,
      "voyageInformation": null,
      "viewDeliveryReceipt": null
    }
  ]
}