【问题标题】:rvest web scraping from html pagervest 从 html 页面抓取网页
【发布时间】:2018-03-20 20:24:45
【问题描述】:

我正在尝试从该页面中获取博彩公司赔率:

https://www.interwetten.com/en/sportsbook/top-leagues?topLinkId=1

所以到目前为止我写了以下代码

interwetten <- read_html("https://www.interwetten.com/en/sportsbook/top-leagues?topLinkId=1") 
bundesliga <- html_nodes(interwetten, xpath = '//*[@id="TBL_Content_1019"]')  
bundesliga_teams <- html_nodes(bundesliga, "span")

现在我得到的输出是:

[1] <span id="ctl00_cphMain_UCOffer_LeagueList_rptLeague_ctl00_ucBettingContainer_lblClose" clas ...
[2] <span itemscope="itemscope" itemprop="location" itemtype="http://schema.org/Place"><meta ite ...
[3] <span itemprop="name">VfB Stuttgart</span>
[4] <span>X</span>

现在我想提取 every &lt;span itemprop="name"&gt;&lt;/span&gt; 中的团队名称,但我不知道如何提取它。我尝试使用节点或属性,但没有成功。

【问题讨论】:

    标签: r web-scraping html-parsing rvest


    【解决方案1】:

    您可以使 XPath 选择器更具体,然后使用html_text,例如

    library(rvest)
    
    interwetten <- 'https://www.interwetten.com/en/sportsbook/top-leagues?topLinkId=1' %>% 
        read_html() 
    
    teams <- interwetten %>% 
        html_nodes(xpath = '//*[@id="TBL_Content_1019"]//span[@itemprop="name"]') %>% 
        html_text()
    
    teams
    #>  [1] "VfB Stuttgart"   "1. FC Cologne"   "Mainz 05"       
    #>  [4] "Hamburger SV"    "Hertha BSC"      "Schalke 04"     
    #>  [7] "Hannover 96"     "Frankfurt"       "Hoffenheim"     
    #> [10] "Augsburg"        "Bayern Munich"   "Freiburg"       
    #> [13] "Dortmund"        "RB Leipzig"      "Leverkusen"     
    #> [16] "Wolfsburg"       "Werder Bremen"   "Monchengladbach"
    

    【讨论】:

    • 非常感谢!您介意指导我如何将这些团队名称插入到两列的数据框中吗?我希望每个具有奇数索引号(1、3、5..)的团队都插入第一列,而索引在偶数位置的每个团队都将在第二列中?
    • 您可以在事后使用tibble::as_data_frame(matrix(teams, ncol = 2, byrow = TRUE))。将每个都刮成一个单独的向量会更健壮,但工作量更大。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2019-02-17
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多