当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R rvest session 在网络浏览器中模拟会话


这组函数允许您模拟用户与网站交互、使用表单以及从一个页面导航到另一个页面。

  • 使用 session(url) 创建会话

  • 使用 session_jump_to() 导航到指定的网址,或使用 session_follow_link() 访问页面上的链接。

  • 提交 html_formsession_submit()

  • 使用 session_history() 查看历史记录,并使用 session_back()session_forward() 前后导航。

  • 使用 html_element()html_elements() 提取页面内容,或使用 read_html() 获取完整的 HTML 文档。

  • 使用 httr::cookies()httr::headers()httr::status_code() 检查 HTTP 响应。

用法

session(url, ...)

is.session(x)

session_jump_to(x, url, ...)

session_follow_link(x, i, css, xpath, ...)

session_back(x)

session_forward(x)

session_history(x)

session_submit(x, form, submit = NULL, ...)

参数

url

要导航到的 URL(相对或绝对)。

...

在整个会话中使用的任何其他 httr 配置。

x

一次会议。

i

用于选择第 i 个链接的整数或用于匹配包含该文本的第一个链接的字符串(区分大小写)。

css, xpath

要选择的元素。根据您要使用 CSS 选择器还是 XPath 1.0 表达式,提供 cssxpath 之一。

form

要提交的html_form

submit

应该使用哪个按钮来提交表单?

  • NULL 默认使用第一个按钮。

  • 字符串通过名称选择按钮。

  • 数字使用其相对位置来选择按钮。

例子

s <- session("http://hadley.nz")
s %>%
  session_jump_to("hadley-wickham.jpg") %>%
  session_jump_to("/") %>%
  session_history()
#> Warning: Not Found (HTTP 404).
#>   https://hadley.nz/
#>   https://hadley.nz/hadley-wickham.jpg
#> - https://hadley.nz/

s %>%
  session_jump_to("hadley-wickham.jpg") %>%
  session_back() %>%
  session_history()
#> Warning: Not Found (HTTP 404).
#> - https://hadley.nz/
#>   https://hadley.nz/hadley-wickham.jpg

# \donttest{
s %>%
  session_follow_link(css = "p a") %>%
  html_elements("p")
#> Navigating to http://rstudio.com
#> {xml_nodeset (68)}
#>  [1] <p class="d-inline">\n            <b><a style="color: #ffffff; fon ...
#>  [2] <p class="d-inline pl-0 pt-1 pr-3">\n            <b class="pr-3">< ...
#>  [3] <p><a style="color: #ffffff; font-size: .9em;" href="https://posit ...
#>  [4] <p>The premier IDE for R</p>
#>  [5] <p>RStudio anywhere using a web browser</p>
#>  [6] <p>Put Shiny applications online</p>
#>  [7] <p>Shiny, R Markdown, Tidyverse and more</p>
#>  [8] <p>Next level training for you and your team</p>
#>  [9] <p>Do, share, teach and learn data science</p>
#> [10] <p>An easy way to access R packages</p>
#> [11] <p>Let us host your Shiny applications</p>
#> [12] <p>A single home for R &amp; Python Data Science Teams</p>
#> [13] <p>Scale, develop, and collaborate across R &amp; Python</p>
#> [14] <p>Easily share your insights</p>
#> [15] <p>Control and distribute packages</p>
#> [16] <p>RStudio</p>
#> [17] <p>RStudio Server</p>
#> [18] <p>Shiny Server</p>
#> [19] <p>R Packages</p>
#> [20] <p>RStudio Academy</p>
#> ...
# }
源代码:R/session.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Simulate a session in web browser。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。