用rvest刮擦选定的Drop Down项目的文本 [英] Scrape the text of a selected Drop Down item with rvest

查看:34
本文介绍了用rvest刮擦选定的Drop Down项目的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rselenium rvest 抓取某些网站.因此,我循环浏览下拉菜单中的项目以更改javascript表.下拉菜单中的表格名称应成为我在已抓取表格中的标识符列.我设法刮擦了桌子,但在刮擦一个选定的菜单项时却卡住了.这是html代码的几行:

I am scraping some website using Rselenium and rvest. Therefore I am cycling through items of a drop down menu to change the javascript table. The table name from the drop down menu should become my identifier column in the scraped table. I managed to scrape the table, but I am stuck when scraping just the one selected menu entry. Here are some lines of the html code:

<select>
<option value="5823">2010/2011</option>
<option value="7094">2011/2012</option>
<option value="9024">2012/2013</option>
<option value="11976">2013/2014</option>
<option value="15388">2014/2015</option>
<option value="18336" selected="selected">2015/2016</option>
</select>

如何获取所选列的html_text?css选择器:checked不起作用.我试过了:

How do I get the html_text of the selected column? The css selector :checked doesn't work. I tried:

 html_nodes("option") %>% html_attrs()

哪个正确地给了我:

 [[1]]
 value 
"5823" 

[[2]]
 value 
"7094" 

[[3]]
 value 
"9024" 

[[4]]
  value 
"11976" 

[[5]]
  value 
"15388" 

[[6]]
  selected      value 
"selected"    "18336" 

read_html(wData) %>% html_nodes("option") %>% html_text()
[1] "2010/2011" "2011/2012" "2012/2013" "2013/2014" "2014/2015" "2015/2016"

但是我不知道如何将两者结合在一起.我只会得到:

But I don't know how to bring the two together. I only get:

[1] "2015/2016"

因为我接下来要遍历所有选项,所以我需要一个通用的解决方案.谢谢.

Since I am then cycling through the options I need a general solution. Thanks.

推荐答案

您可以使用 xpath 选择器而不是CSS选择器.

You can use an xpath selector rather than a css selector.

read_html(wData) %>% html_nodes(xpath="//option[@selected]")  %>% html_text()

即使:checked css伪类不起作用,这也允许您搜索属性.

This allows you to search for attributes even when the :checked css pseudo class doesn't work.

这篇关于用rvest刮擦选定的Drop Down项目的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆