今日の新聞のFinancialTimesのURLのリストを取得しようとしています。これを行うには、FT.comからソースコードを取得し、区切り文字を使用してhtmlソースコードを解析します。次に、各urlをコンマ区切りの値ファイル(.txt拡張子)として保存しようとしています。
私はhtmlソースコードを解析するところまで到達することができました。しかし、私の問題は、URLをcsvファイル(または段落で区切られたURLのリスト)として保存することにあります。
これが私のアップルスクリプトです:
on run
set query_url to "http://www.ft.com/uk-edition"
set query_url_source to do shell script "/usr/bin/curl " & quoted form of query_url
set p to query_url_source
set ex to extractBetweenLong(p, "><a href=\"/cms/s", ".html")
return ex
end run
--delimiters subroutine:
to extractBetweenLong(SearchText, startText, endText)
set tid to AppleScript's text item delimiters -- save them for later.
set AppleScript's text item delimiters to startText -- find the first one.
set liste to text items of SearchText
set AppleScript's text item delimiters to endText -- find the end one.
set extracts to {}
repeat with subText in liste
if subText contains endText then
copy text item 1 of subText to end of extracts
end if
end repeat
set AppleScript's text item delimiters to tid -- back to original values.
return extracts
end extractBetweenLong
私の出力は次のとおりです。
{"^!DOCTYPE html ... subs5"、 "/ 0 / 0130d092-c473-11e1-9c1e-00144feabdc0"、 "/ 0 / cb8a70a0-c469-11e1-a98c-00144feabdc0"、...、 "/ 0 / 02eaa328-c468-11e1-9c1e-00144feabdc0 "、}
私の最初の質問は、なぜ最初の文字列(^!DOCTYPE html ...)が存在するのかということです。最初の「startText」区切り文字に「DOCTYPE...subs5」(subs5 HTMLタブは.htmlで終わる)を含めるべきではないため、区切り文字サブルーチンに何か問題があるはずです。これは、サブルーチンがスターであることを示している可能性があります。
2番目各URLをコンマまたは改行で区切ってリストとして保存するにはどうすればよいですか?まず、各URLの前に「www.ft.com/cms/s」という文字列を付けたいと思いますが、自分で理解できると思います。
よろしくお願いします。