以下是一个使用AppleScript从网页中提取文本字段并将它们编译在一个文本文档中的示例代码:
-- 设置要提取的网页URL
set webpageURL to "https://example.com"
-- 使用“curl”命令获取网页内容并将其保存到临时文件中
set tempFilePath to "/tmp/webpage.txt"
do shell script "curl " & quoted form of webpageURL & " > " & quoted form of tempFilePath
-- 打开临时文件并读取其内容
set fileContents to read file tempFilePath
-- 使用正则表达式提取所需的文本字段
set extractedText to paragraphs of (do shell script "egrep -o 'regex_pattern' " & quoted form of tempFilePath)
--将'regex_pattern'替换为您要提取的文本字段的正则表达式模式
-- 将提取的文本字段编译到一个文本文档中
set compiledText to ""
repeat with i from 1 to count of extractedText
set compiledText to compiledText & item i of extractedText & return
end repeat
-- 保存编译的文本到一个文本文件中
set compiledTextFilePath to (path to desktop folder as text) & "compiled_text.txt"
do shell script "echo " & quoted form of compiledText & " > " & quoted form of compiledTextFilePath
-- 打开编译的文本文件
tell application "Finder"
open file compiledTextFilePath
end tell
在上面的代码中,您需要将webpageURL
替换为您要提取文本字段的网页的URL。您还需要将regex_pattern
替换为提取您要提取的文本字段的正则表达式模式。
请注意,这个示例使用了curl
命令来获取网页内容,并使用egrep
命令来使用正则表达式提取文本字段。如果您的系统上没有安装这些命令,您需要安装它们或使用其他适合您的环境的方法来获取网页内容和提取文本字段。
最后,编译的文本将保存在您的桌面上的一个名为compiled_text.txt
的文本文件中,并且会自动在脚本运行后打开此文件。