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

问题描述 投票:1回答:2

我能够通过第二个链接从网站上读取动态信息(在下面的代码中注释掉)。如果我取消注释第二行它工作正常,我得到我想要的信息。如果我使用第一个链接它不起作用;生成的文件是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
2个回答
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)是针对单线的is。日期值周围可能存在一些空格,\s*用于匹配它们。 :是日期值的一部分并且是匹配的。这允许(.*?)组获得ETA日期。

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


1
投票

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

列出<description> -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

© www.soinside.com 2019 - 2024. All rights reserved.