2016-11-08 57 views
0

我有一个关于我最新的背心刮的问题。使用rvest来刮取网站 - 选择html节点?

我想凑这个页面(和其他一些个股也一样): http://www.finviz.com/quote.ashx?t=AA&ty=c&p=d&b=1

我需要市场资金,这是在第二行第一个框的列表。 此清单应包含约50-100个股票。

我正在使用rvest。

library(rvest) 

html = read_html("http://www.finviz.com/quote.ashx?t=A") 

cast = html_nodes(html, "table-dark-row") 

问题是,我无法绕过html_nodes。 有关如何找到html_nodes的正确节点的任何想法?

我正在使用萤火虫/火鸟检查网页。

回答

2

不知道这是你想要的,因为我找不到aprox列表。 50-100只股票。

但为了什么值得使用SelectorGadget我想出了这个节点.table-dark-row:nth-​​child(2).snapshot-td2:nth-​​child(2),选择Market Cap(first在本页第二行http://www.finviz.com/quote.ashx?t=AA&ty=c&p=d&b=1)。

> library(rvest) 
> 
> html = read_html("http://www.finviz.com/quote.ashx?t=AA&ty=c&p=d&b=1") 
> 
> cast = html_nodes(html, ".table-dark-row:nth-child(2) .snapshot-td2:nth-child(2)") 
> cast 
{xml_nodeset (1)} 
[1] <td width="8%" class="snapshot-td2" align="left">\n <b>11.58B</b>\n</td> 
> 

如果这不正是你想要的,只需使用SelectorGadget来找到你想要的。

希望这会有所帮助。

编辑:

这里完整的解决方案:

library(rvest) 

html = read_html("http://www.finviz.com/quote.ashx?t=AA&ty=c&p=d&b=1") 

cast = html_nodes(html, ".table-dark-row:nth-child(2) .snapshot-td2:nth-child(2)") 

html_text(cast) %>% 
    gsub(pattern = "B", replacement = "") %>% 
    as.numeric() 
+0

这一个似乎很合法的。我需要弄清楚如何从字符串中提取数字。 –

+0

在同一个'rvest'包中使用函数'html_text()'。 'html_text(cast)'给你“12.76B”,然后,将它转换为数字,你需要摆脱B(我不知道它是什么意思)。我编辑回答。检查那里的完整解决方案。 – elikesprogramming