Applescript 删除不在两个字符串之间的所有文本 [英] Applescript to remove all text not between two strings

查看:22
本文介绍了Applescript 删除不在两个字符串之间的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个脚本,显示当前 iTunes 前 20 名的对话框.我打算如何从前 100 名网站获取 html 代码,然后提取两个字符串之间的文本以获取名称的歌曲.对于第一首歌来说,这是非常成功的.

I'm trying to make a script that displays a dialog of the current iTunes Top 20. How I intend to do this is get the html code from the top 100 website and then extract for text between two strings to get the name of the song. For the first song, this is extremely successful.

然而,它每次只对第一首歌有效.我能想到的解决这个问题的唯一方法是 get 两个字符串之间的所有内容,我可以删除它们之间的所有内容而不是.这有望将所有歌曲名称作为字符串提供给我.

However, it only works for the first song each time. The only way I can think of to fix this is rather then get everything between the two strings, I could delete everything not between them. This would hopefully then give me all the song names as a string.

有人知道怎么做吗?

获取 HTML:

set curlcommand to "curl https://www.apple.com/itunes/charts/songs/"
set html to (do shell script curlcommand) 

获取歌曲名称:

set AppleScript's text item delimiters to "width=\"100\" height=\"100\" alt=\""
set theText to item 2 of every text item of html
set AppleScript's text item delimiters to "\"></a>"
set theText to item 1 of every text item of theText
set AppleScript's text item delimiters to ""

推荐答案

您可以使用 shell 逐行获取包含所有歌曲名称的列表(这不是 AppleScript 列表).

You could use the shell to get a list with all song names line by line (this is not a AppleScript list).

set charts to (do shell script "
curl -s 'https://www.apple.com/itunes/charts/songs/'| tr '\"' '\n' |awk '/^ alt=$/ {getline;print}'")

tr '\"' '\n' 用新行替换双引号 (tr 手册页)

tr '\"' '\n' substitutes the double quotes with new lines (tr manual page)

awk '/^ alt=$/{getline;print}' 打印歌曲名称所在行 alt= 之后的行(awk 手册页)

awk '/^ alt=$/ {getline;print}' prints the line after the line alt= where the song name is written (awk manual page)

这篇关于Applescript 删除不在两个字符串之间的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆