阅读网络内容(可能是RSS)

时间:2019-03-04 22:02:23

标签: xml rss autoit

我能够通过第二个链接从网站上读取动态信息(在下面的代码中进行了注释)。如果我取消注释第二行,则可以正常工作,并且可以获得所需的信息。如果我使用第一个链接,它将无法正常工作;生成的文件为0字节。

第一次,您可能必须按一些按钮,然后再次运行脚本(取决于浏览器)。那么如何获得理想的结果呢?我需要最上面的ETA:Arrival">ETA</a> : March 23, 2019 </td></tr> </table>

_RSSGetInfo()也不起作用(托运人指出这是XML RSS提要,但我不知道这是否适合使用;它给了我空白)。

#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <Array.au3>
#include <Date.au3>
#include <String.au3>
#include <Excel.au3>
#include <WinAPIFiles.au3>

$Link = "http://wcf.mscgva.ch/publicasmx/Tracking.asmx/GetRSSTrackingByContainerNumber?ContainerNumber=MSCU4727397"
;$Link   = "https://www.hapag-lloyd.com/en/online-business/tracing/tracing-by-booking.html?blno=HLCUEUR1810BCLY1"

$file   = fileopen(@ScriptDir & "\XYZ.txt", 2 + 8)
$IE     = _IECreate($Link, 0, 1, 1, 1)

Sleep(2000)
$source = _IEDocReadHTML($IE)
FileWrite($file, $source)

MsgBox(0, "Source", $source)

这是RSSInfo版本,它给了我一个空白(我found this on the internet,并对其进行了编辑):

#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <Array.au3>
#include <Date.au3>
#include <String.au3>
#include <Excel.au3>
#include <WinAPIFiles.au3>

#include-once
#region _RSS
; RSS Reader
; Created By: Frostfel
#include <INet.au3>
#include <Array.au3>

; ============================================================================
; Function:        _RSSGetInfo($RSS, $RSS_InfoS, $RSS_InfoE[, $RSS_Info_ = 1])
; Description:     Gets RSS Info
; Parameter(s):    $RSS =  RSS Feed Example: "http://feed.com/index.xml"
;                  $RSS_InfoS = String to find for info start Example: <title>
;                  $RSS_InfoE = String to find for info end Example: </title>
;                  $RSS_Info_Start = [optional] <info>/</info> To start at
;                                   Some RSS feeds will have page titles
;                                   you dont want Defualt = 0
; Requirement(s):  None
; Return Value(s): On Success - Returns RSS Info in Array Starting at 1
;                  On Failure - Returns 0
;                               @Error = 1 - Failed to get RSS Feed
; Author(s):       Frostfel
; ============================================================================
Func _RSSGetInfo($RSS, $RSS_InfoS, $RSS_InfoE, $RSS_Info_Start = 0)
    $RSSFile = _INetGetSource($RSS)
    If @Error Then
        SetError(1)
        Return -1
    EndIf
    Dim $InfoSearchS = 1
    Dim $Info[1000]
    Dim $InfoNumA
    $InfoNum = $RSS_Info_Start
    While $InfoSearchS <> 6
        $InfoNum += 1
        $InfoNumA += 1
        $InfoSearchS = StringInStr($RSSFile, $RSS_InfoS, 0, $InfoNum)
        $InfoSearchE = StringInStr($RSSFile, $RSS_InfoE, 0, $InfoNum)
        $InfoSearchS += 6
        $InfoSS = StringTrimLeft($RSSFile, $InfoSearchS)
        $InfoSearchE -= 1
        $InfoSE_Len = StringLen(StringTrimLeft($RSSFile, $InfoSearchE))
        $InfoSE = StringTrimRight($InfoSS, $InfoSE_Len)
        _ArrayInsert($Info, $InfoNumA, $InfoSE)
    WEnd
    Return $Info
EndFunc
#endregion

$Link = "http://wcf.mscgva.ch/publicasmx/Tracking.asmx/GetRSSTrackingByContainerNumber?ContainerNumber=MSCU4727397"
$Test1 = _RSSGetInfo($Link, "<channel>", "</channel>", 1)
MsgBox(0, "Test", $Test1)

2 个答案:

答案 0 :(得分:1)

示例清单<description>-标签(使用XML.au3),从包含的HTML中提取(使用IE.au3):

#include <Array.au3>
#include <IE.au3>
#include "XML.au3"

Global Const $g_iElement = 1, _; n-th XML <description> -tag.
             $g_iColCont = 3, _; n-th XML property column (tag content).
             $g_iTblCol  = 4, _; n-th HTML table col ("Final Discharge Port").
             $g_iTblRow  = 2;    n-th HTML table row (column's content).
Global Const $g_sFileTmp = @ScriptDir & '\rss_extract.html', _
             $g_sTplHtml = '<html>\n<head>\n<title>%s</title>\n</head>\n<body>\n%s\n</body>\n</html>\n', _
             $g_sRssUrl  = 'http://wcf.mscgva.ch/publicasmx/Tracking.asmx/GetRSSTrackingByContainerNumber?ContainerNumber=MSCU4727397', _
             $g_sRssXpt  = '//channel/item/description'

Global       $g_sRssTxt  = InetRead($g_sRssUrl)
Global       $g_aXmlNode, $g_aTable
Global       $g_oXmlDoc, $g_oXmlNode, $g_oIE, $g_oTable

$g_sRssTxt  = BinaryToString($g_sRssTxt)
$g_oXmlDoc  = _XML_CreateDOMDocument()
$g_oXmlDoc  = _XML_LoadXML($g_oXmlDoc, $g_sRssTxt)
$g_oXmlNode = _XML_SelectNodes($g_oXmlDoc, $g_sRssXpt)
$g_aXmlNode = _XML_Array_GetNodesProperties($g_oXmlNode)

_ArrayDisplay($g_aXmlNode)
FileWrite($g_sFileTmp, StringFormat($g_sTplHtml, $g_sFileTmp, $g_aXmlNode[$g_iElement][$g_iColCont]))

$g_oIE    = _IECreate($g_sFileTmp)
$g_oTable = _IETableGetCollection($g_oIE, 1)
$g_aTable = _IETableWriteToArray($g_oTable)

_ArrayDisplay($g_aTable)
MsgBox(Default, @ScriptName, $g_aTable[0][$g_iTblCol] & ' =' & @CRLF & @CRLF & $g_aTable[$g_iTblRow][$g_iTblCol])
_IEQuit($g_oIE)
FileDelete($g_sFileTmp)

$g_aTable[$g_iTblRow][$g_iTblCol]包含HOUSTON US ETA : March 23, 2019

答案 1 :(得分:1)

; Url to get the source from.
$sUrl = 'http://wcf.mscgva.ch/publicasmx/Tracking.asmx/GetRSSTrackingByContainerNumber?ContainerNumber=MSCU4727397'

; Read source from the url.
$sSource = InetRead($sUrl)

; Convert source to utf-8 string (8 = utf-8).
$sSource = BinaryToString($sSource, 8)

; Get the eta dates (3 = return array of global matches).
$aETA = StringRegExp($sSource, '(?is)title="Estimated Time of Arrival"&gt;ETA&lt;/a&gt;\s*:\s*(.*?)\s*&lt;', 3)

; Display the eta date.
If UBound($aETA) Then
    MsgBox(0, @ScriptName, $aETA[0])
EndIf

这使用正则表达式从RSS / XML源中的HTML获取ETA日期。

StringRegExp选项(?is)i代表敏感,s代表单行。 日期值周围可能存在一些空格,\s*用于匹配它们。 :是日期值的一部分,并且已匹配。 这使(.*?)组可以仅获取预计到达时间。

$aETA包含数组中的所有ETA日期,尽管第一个是$aETA[0] Msgbox中显示的日期,这是您提到的预计到达时间 是评论顶部的那个。