【问题标题】:AppleScript parse HTMLAppleScript 解析 HTML
【发布时间】:2014-02-02 18:03:11
【问题描述】:

目标:如果订单中包含“发件人:”一词,我需要退款的一长串订单,并且每次购买都有一个订单号,我一直在尝试解析或循环,但不确定单词是否正确 搜索 HTML doc for单词“From:”的实例,如果找到,则获取其他数字并打印到文本文件。

到目前为止,我已经学会了如何使用文本项目分隔符,但是当我尝试让它循环或重复时,我只是一遍又一遍地得到相同的订单号。这是在尝试实现如果订单详细信息包含单词“from”然后获取订单号的部分之前。

set astid to AppleScript's text item delimiters
set x to 2

set startHere to "p class=\"webOrderNumber\">"
set stopHere to "</p>"

set entireText to (do shell script "curl     file:///Users/Michael/Desktop/StorePurchases.webarchive")
set AppleScript's text item delimiters to startHere

set blurb1 to text item x of entireText
repeat with i from 2 to (count of blurb1)
   set thisItem to item i of blurb1

   set AppleScript's text item delimiters to stopHere
   set blurb2 to text item 1 of blurb1

   set AppleScript's text item delimiters to astid



   set writeFile to ((path to desktop as text) & "test_output") -- 
   set writeData to blurb2

   set success to appendDataToFile(writeData, writeFile)
end repeat


    on appendDataToFile(myData, aFile)
       set OA to (open for access aFile with write permission)
   try
       write (myData & (ASCII character 10)) as text to OA starting at eof
       close access OA
       return true
   on error
       try
           close access OA
       end try
       return false
   end try
end appendDataToFile

这里是 HTML 的摘录:

</td>
  <td class="OrderNumber">
<p class="webNumber">BHWL123456</p>
 </p>
    <td class="OrderNumber">

    <p class="details">
Full Version: Get all Activities &amp; Remove ads! [From: Pony Resort: MakeverMagic]     </p>
</td>

当有数百个订单需要很长时间才能实际搜索 FROM 一词并写入订单号​​时,将使用此选项。

【问题讨论】:

  • 您想要达到的目标在 AppleScript 中非常困难。如果真的没有必要使用 AppleScript 并且您有一些其他语言的经验,您应该考虑使用其他东西。

标签: html parsing applescript delimiter


【解决方案1】:

试试:

set resultNumbers to {}

set myText to read file ((path to temporary items as text) & "test.txt") as «class utf8»

set {TID, text item delimiters} to {text item delimiters, "<p class=\"webNumber\">"}
set webOrderNumbers to text items 2 thru -1 of myText

set AppleScript's text item delimiters to "</p>"
repeat with wNumber in webOrderNumbers
    set end of resultNumbers to text item 1 of wNumber
end repeat

set text item delimiters to TID
return resultNumbers

【讨论】:

  • 感谢您的想法。 Mark 的代码运行良好。我还在学习,所以这里的建议很有帮助,因为它让我可以和他们“玩耍”,并自我发现更多关于 applescript 的信息。
【解决方案2】:

在 Macscripter.net 上使用 Applescript 脚本库和我在 thread 中提供的其他 Applescript 提出并解决了这个问题。

脚本库使用 NSRegularExpresion 在两个文本目标之间进行搜索,并一次性返回所有匹配项。

如果有人感兴趣,我可以在这里发布代码,或者您可以访问该线程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-30
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多