[selenium]chromedriverを使って指定URLのタイトルを取得する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # chromedriverを使ってURLのタイトルを取得する |
005 | (* |
006 | 事前準備 |
007 | 1:ChromeDriverの取得 |
008 | 2:Google Chrome for Testing.appの取得 |
009 | https://quicktimer.cocolog-nifty.com/icefloe/2024/11/post-4a5ddb.html |
010 | 3:seleniumセットアップ |
011 | 同封の『setupPy.applescript』の実行 |
012 | 4:ChromeDriverの使用の許可 |
013 | 実行時にアプリケーションの実行許可が必要になる場合がある |
014 | この場合はシステム設定のセキュリティとプライバシーで許可を与える必要がある |
015 | これには管理者権限が必要 |
016 | 5:Google Chrome for Testing.appの拡張属性の削除 |
017 | https://quicktimer.cocolog-nifty.com/icefloe/2024/11/post-98ec7b.html |
018 | *) |
019 | |
020 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
021 | use AppleScript version "2.8" |
022 | use framework "Foundation" |
023 | use framework "UniformTypeIdentifiers" |
024 | use framework "AppKit" |
025 | use scripting additions |
026 | property refMe : a reference to current application |
027 | |
028 | set strGetTitleURL to ("https://news.yahoo.co.jp/") as text |
029 | |
030 | #設定項目chromedriverまでのパス |
031 | set strChromeDriverPath to ("~/bin/chromedriver/chromedriver") as text |
032 | set ocidChromeDriverPathStr to refMe's NSString's stringWithString:(strChromeDriverPath) |
033 | set ocidChromeDriverPath to ocidChromeDriverPathStr's stringByStandardizingPath() |
034 | set ocidChromeDriverPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidChromeDriverPath) isDirectory:(false) |
035 | set ocidChromeDriverDirPathURL to ocidChromeDriverPathURL's URLByDeletingLastPathComponent() |
036 | set strChromeDriverPath to ocidChromeDriverPath as text |
037 | |
038 | #pyまでのパス |
039 | set aliasPathToMe to (path to me) as alias |
040 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
041 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
042 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
043 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
044 | set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
045 | set ocidBinDirURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("bin") isDirectory:(true) |
046 | set ocidPyFilePathURL to ocidBinDirURL's URLByAppendingPathComponent:("getTitlePy.py") isDirectory:(false) |
047 | set strPyFilePath to ocidPyFilePathURL's |path| as text |
048 | # |
049 | ################## |
050 | #コマンド実行 |
051 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
052 | ocidTermTask's setLaunchPath:("/usr/bin/python3") |
053 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
054 | ocidArgumentsArray's addObject:(strPyFilePath) |
055 | ocidArgumentsArray's addObject:(strGetTitleURL) |
056 | ocidArgumentsArray's addObject:(strChromeDriverPath) |
057 | ocidTermTask's setArguments:(ocidArgumentsArray) |
058 | set ocidOutPut to refMe's NSPipe's pipe() |
059 | set ocidError to refMe's NSPipe's pipe() |
060 | ocidTermTask's setStandardOutput:(ocidOutPut) |
061 | ocidTermTask's setStandardError:(ocidError) |
062 | ocidTermTask's setCurrentDirectoryURL:(ocidChromeDriverDirPathURL) |
063 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
064 | if (item 1 of listDoneReturn) is (false) then |
065 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
066 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
067 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
068 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
069 | end if |
070 | ################## |
071 | #終了待ち |
072 | ocidTermTask's waitUntilExit() |
073 | |
074 | ################## |
075 | #標準出力をログに |
076 | set ocidOutPutData to ocidOutPut's fileHandleForReading() |
077 | set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference) |
078 | set ocidStdOut to (item 1 of listResponse) |
079 | set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding) |
080 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\n") withString:("")) |
081 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\r") withString:("")) |
082 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("Page Title: ") withString:("")) |
083 | log ocidStdOut as text |
084 | return ocidStdOut as text |
AppleScriptで生成しました |
| 固定リンク