我很难从网站的源代码中提取特定的文本选择。我可以提取整个列表,但我只需要一个国家,例如阿根廷。
源代码是:
<div class="article-content">
<div class="RichTextElement">
<div><h3 style="background-color: transparent; color: rgb(51, 51, 51);"><span style="font-weight: normal; font-family: Verdana;">Afghanistan - </span><span style="background-color: transparent; font-weight: normal; font-family: Verdana;"><a title="Tax Authority in Afganistan" href="http://mof.gov.af/en" style="background-color: transparent; color: rgb(51, 51, 51);">Ministry of Finance</a><br />Argentina - <a title="Tax Authority in Argentina" href="http://www.afip.gob.ar/english/" style="background-color: transparent; color: rgb(51, 51, 51);">Federal Administration of Public Revenues</a><br />
我只需要&#34;联邦公共收入管理局&#34;和&#34; http://www.afip.gob.ar/english/&#34;
到目前为止,我有:
argurl <- readLines("http://oceantax.co.uk/links/tax-authorities-worldwide.html")
strong <-as.matrix(grep("<br//>",argurl))
strong1starts <- grep("<br //>Argentina",argurl)
rowst1st <- which(grepl(strong1starts, strong))
strong1ends <- strong[rowst1st + 1 ,]-1
data1 <- as.matrix(argurl[strong1starts:strong1ends])
答案 0 :(得分:4)
library(rvest)
url <- "http://oceantax.co.uk/links/tax-authorities-worldwide.html"
pg <- html(url)
# get the country node
# XPath version
country <- pg %>% html_nodes(xpath="//a[contains(@title, 'Argentina')]")
# CSS Selector version
country <- pg %>% html_nodes("a[title~=Argentina]")
# use one of the above then:
country %>% html_text() # get the text of the anchor
country %>% html_attr("href") # get the URL of the anchor