Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用R抓取SoFifa.com时,为玩家的国籍选择什么css元素?_R_Web Scraping_Rvest - Fatal编程技术网

在使用R抓取SoFifa.com时,为玩家的国籍选择什么css元素?

在使用R抓取SoFifa.com时,为玩家的国籍选择什么css元素?,r,web-scraping,rvest,R,Web Scraping,Rvest,所以我一直在尝试使用rvest软件包从SoFifa.com上获取玩家的详细信息。多花了一英里从桌子上一列一列地刮下来。这就是我被卡住的地方。我不能得到运动员的国籍。也许我选择了错误的CSS元素。尝试使用选择器小工具,但仍然没有成功。下面是代码。任何帮助都将不胜感激 #Website link to be scraped with selected columns. link= "https://sofifa.com/" #Name of each player. This works pe

所以我一直在尝试使用rvest软件包从SoFifa.com上获取玩家的详细信息。多花了一英里从桌子上一列一列地刮下来。这就是我被卡住的地方。我不能得到运动员的国籍。也许我选择了错误的CSS元素。尝试使用选择器小工具,但仍然没有成功。下面是代码。任何帮助都将不胜感激

#Website link to be scraped with selected columns.

link= "https://sofifa.com/"


#Name of each player. This works perfectly fine as all names are retrived

Name <- link %>% read_html() %>% 
        html_nodes(".nowrap") %>% 
        html_text() 


#Nationality is not displayed. While inspecting this section, I observed that title of the element < a rel=nofollow> under <div class="bp3-text-overflow-ellipsis">
needs to be selected. Need help to how to do that!!

Nationality <- link %>% read_html() %>% 
        html_nodes(".flag") %>% 
        html_text() 

#Tried .flag as the selector gadget suggested it but still it doesnt retrieve the Nationality for a player 






您可以将两个属性组合起来以获得所需的内容。 尝试:


你比我快到了:-。我和href有问题。你能告诉我为什么它需要是*=?@ahmed.soli好的,我知道需要提取title属性。你能帮我查一下密码吗?国籍%read_html%>%html_nodes.bp3-text-overflow-ellipsis%>%html_attra[rel=nofollow][href*=players?}对此不是100%确定。请纠正我,我知道它错了@艾哈迈德·索利完美!!
#<a rel="nofollow" href="/players?na=14" title="England">...</a>
# the *= in css selectors means that attribute contains a certain text
# here is the css selecot     
#.bp3-text-overflow-ellipsis a[rel="nofollow"][href*="players?"]

page <- read_html(link)
Nationality  <- page %>% html_nodes('.bp3-text-overflow-ellipsis a[rel="nofollow"][href*="players?"]') %>% html_attr('title')
print(Nationality )
[1] "Italy"          "England"        "Togo"           "France"
 [5] "Ghana"          "Brazil"         "Norway"         "Spain"
 [9] "Nigeria"        "Argentina"      "Spain"          "England"
[13] "Portugal"       "England"        "Denmark"        "England"
[17] "Italy"          "Argentina"      "England"        "Portugal"
[21] "Argentina"      "Norway"         "Brazil"         "Norway"
[25] "Netherlands"    "Germany"        "England"        "Uruguay"
[29] "United States"  "Argentina"      "Netherlands"    "Czech Republic"
[33] "Brazil"         "France"         "Argentina"      "Brazil"
[37] "Poland"         "Brazil"         "Italy"          "Portugal"
[41] "Netherlands"    "Netherlands"    "Netherlands"    "Morocco"
[45] "Argentina"      "Spain"          "Argentina"      "France"
[49] "Netherlands"    "Brazil"         "Argentina"      "France"
[53] "Canada"         "Canada"         "Switzerland"    "Brazil"
[57] "Germany"        "Netherlands"    "Jamaica"        "France"