首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阅读网页内容(可能是RSS)

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

Stack Overflow用户
提问于 2019-03-05 06:02:23
回答 2查看 182关注 0票数 1

我能够通过第二个链接(在下面的代码中注释掉)从网站上读取动态信息。如果我取消对第二行的注释,它就能正常工作,并且我会得到我想要的信息。如果我使用第一个链接,它就不起作用;生成的文件是0字节。

第一次,您可能需要按下某个按钮,然后再次运行脚本(取决于浏览器)。那么怎样才能得到想要的结果呢?我需要最上面的那个:Arrival">ETA</a> : March 23, 2019 </td></tr> </table>

_RSSGetInfo()也不起作用(发送者指出它是一个XML提要,但我不知道这是否正确;它给了我一个空白)。

代码语言:javascript
复制
#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,并编辑了它):

代码语言:javascript
复制
#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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-07 12:39:27

代码语言:javascript
复制
; 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在一个数组中包含所有的预计到达日期,虽然第一个是Msgbox中显示的$aETA[0],但这是您在注释顶部提到的预计到达日期。

票数 1
EN

Stack Overflow用户

发布于 2019-03-06 23:31:45

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

示例列出了<description> -tags (使用XML.au3),从包含的HTML中提取(使用IE.au3):

代码语言:javascript
复制
#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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54992246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档