您如何使用rvest刮擦标题和视图数量

发布于 2025-01-26 13:01:06 字数 244 浏览 2 评论 0原文

我想在不使用YouTube API的情况下在YouTube上刮擦信息。我尝试使用以下代码使用XPATH,但它返回字符(空)。

html <- read_html("https://www.youtube.com/watch?v=WRz2MxhAdJo")
title <- html_nodes(html,xpath = '//*[@id="container"]/h1') %>% html_text()

I want to scrape information on Youtube without using YouTube api. I tried using the code below using xpath but it returns character(empty).

html <- read_html("https://www.youtube.com/watch?v=WRz2MxhAdJo")
title <- html_nodes(html,xpath = '//*[@id="container"]/h1') %>% html_text()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2025-02-02 13:01:06

您可以从网页上从meta标签中提取标题和视图。

标题在&lt; meta name =“ title”标签中。

示例:

<meta name="title" content="Avicii - Without You “Audio” ft. Sandro Cavazza">

视图(称为互动)在&lt; meta itemprop =“ InteractionCount” tag中。

示例:

<meta itemprop="interactionCount" content="160908331">

为此,您可以使用

html %>% html_nodes("[name='title']") %>% html_attr("content") 
html %>% html_nodes("[itemprop='interactionCount']") %>% html_attr("content") 

You can extract the title and views from the meta tags from the webpage.

The title is in the <meta name="title" tag.

Example:

<meta name="title" content="Avicii - Without You “Audio” ft. Sandro Cavazza">

The views (called interaction) is in the <meta itemprop="interactionCount" tag.

Example:

<meta itemprop="interactionCount" content="160908331">

For this, you can use something like shown in this answer:

html %>% html_nodes("[name='title']") %>% html_attr("content") 
html %>% html_nodes("[itemprop='interactionCount']") %>% html_attr("content") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文