【问题标题】:Reading web content (may be RSS)阅读网页内容(可能是 RSS)
【发布时间】:2019-03-04 22:02:22
【问题描述】:

我能够通过第二个链接(在下面的代码中注释掉)从网站读取动态信息。如果我取消注释第二行,它工作正常,我得到我想要的信息。如果我使用第一个链接,它就不起作用;生成的文件是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)

【问题讨论】:

    标签: xml rss autoit


    【解决方案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* 用于匹配它们。 : 是日期值的一部分并且是匹配的。 这允许(.*?) 组仅获取 ETA 日期。

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

    【讨论】:

      【解决方案2】:

      源数据使用转义序列而不是 CDATA 封装。正则表达式是特定于格式的;以下处理任一格式:

      示例列表 &lt;description&gt; -tags(使用 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 2022-06-18
        • 2020-11-24
        • 2018-06-18
        • 2016-07-06
        • 2012-06-19
        相关资源
        最近更新 更多