2

いくつかの調査を行うために、YouTube から自動キャプションを取得しようとしています。Selenium でスクレイピングする代わりに、パッケージ「tuber」で Youtube Data API を使用してキャプションを抽出しました。これは、ユーザーが提供するキャプション トラックがある場合にのみ機能します。残念ながら、分析する必要がある動画のキャプションがアップロードされていません。

私の考えでは、特定のコンテンツにアクセスするために Selenium を使用していました。html コードは次のようになります。

<div class="caption-window ytp-caption-window-bottom ytp-caption-window-rollup" id="caption-window-1" dir="ltr" tabindex="0" aria-live="assertive" style="touch-action: none; text-align: left; left: 21.2%; height: 40px; width: 287px; bottom: 2%;" data-layer="4" lang="en"><span class="captions-text"><span style="background: rgba(8, 8, 8, 0.75) none repeat scroll 0% 0%; box-decoration-break: clone; border-radius: 2px; font-size: 16px; color: rgb(255, 255, 255); fill: rgb(255, 255, 255); font-family: &quot;YouTube Noto&quot;,Roboto,&quot;Arial Unicode Ms&quot;,Arial,Helvetica,Verdana,&quot;PT Sans Caption&quot;,sans-serif;">&nbsp;load the our selenium package into this<span style="color: rgb(204, 204, 204); fill: rgb(204, 204, 204);">&nbsp;<br>&nbsp;session</span> so it's loaded now&nbsp;</span></span></div>

ご覧のとおり、プレーン キャプション テキストが<span></span>要素に埋め込まれています。このコードを使用して、キャプション テキストを取得しました。

install.packages("RSelenium")

require(RSelenium)

# starting driver on port/browser
rD <- rsDriver(port = 4555L, browser = "firefox")
# remote driver client-side
remDr <- rD[["client"]]
# navigate to web page
remDr$navigate("https://www.youtube.com/watch?v=qUKEPurS6-s")

# stop autoplay
play_button <- remDr$findElement(using = 'class', value = "ytp-play-button")
play_button$clickElement()

# activate subtitles
subtitle_button <- remDr$findElement(using = "class", value = "ytp-subtitles-button")
subtitle_button$clickElement()



# captions text element
caption_window <- remDr$findElement(using = "class", value = "captions-text")
# retrieve plain text
text <- caption_window$getElementText()

今私の質問に:

dom 要素に加えられた変更をキャプチャし、新しい単語が出現するたびにテキストを取得するにはどうすればよいですか? AJAX 呼び出しが要素を更新していると思いますが、正確にはわかりません。

ありがとう :)

4

0 に答える 0