特定元素的Xpath

时间:2019-04-04 17:39:35

标签: r xpath web-scraping rselenium

我在从此网页上抓取特定元素文本时遇到问题:

https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/benin-togo-IsfnZDFd/

这是存档结果中特定匹配项的网址,我需要在此页面上抓取4家博彩公司的赔率。我有数千个要抓取的匹配网址。代码如下:

这是我试图找到庄家赔率的方法,但是它不起作用:

pjs <- wdman::phantomjs()

eCap <- list(phantomjs.page.settings.userAgent 
             = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 
  Firefox/29.0", phantomjs.page.settings.loadImages = FALSE, phantomjs.phantom.cookiesEnabled = TRUE, phantomjs.phantom.javascriptEnabled = TRUE)


remDr <- remoteDriver(browserName = "phantomjs", port = 4567L, extraCapabilities = eCap)

remDr$open()



remDr$navigate("https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/benin-togo-IsfnZDFd/")
match<-remDr$findElement('xpath','//*[@id="col-content"]/h1')
result<-remDr$findElement('xpath', '//*[@id="event-status"]/p/strong')
odds<-remDr$findElements('xpath', '//*[@class="name" and contains(text(), "18Bet")]')

odds1 <- data.frame(odds = unlist(sapply(odds, function(x){x$getElementText()})))

pjs$stop()

我想要的是最后一个div的3个赔率,但是页面上有很多不同的庄家,我只能选择所有庄家的赔率,我的目标是选择确切的庄家的赔率,但是我不确定如何之所以要实现这一目标,是因为在div的赔率中没有关于庄家的信息。

<tr class="lo odd">
    <td>
        <div class="l">
            <a class="name2" title="Go to 18bet website!" onclick="return !window.open(this.href)" href="/bookmaker/18bet/link/"><span class="blogos l416"></span></a>&nbsp;
            <a class="name" title="Go to 18bet website!" onclick="return !window.open(this.href)" href="/bookmaker/18bet/link/">18bet</a>&nbsp;&nbsp;
        </div>
        <span class="ico-bookmarker-info ico-bookmaker-detail">
          <a title="Show more details about 18bet" href="/bookmaker/18bet/"></a>
            </span>
        <span class="ico-bookmarker-info ico-bookmaker-bonus">
          <a onmouseout="globals.getBookmaker(416).cancelBonusOver();" xparam="<div class=&quot;bold&quot;>100% Bonus up to 100€!</div><div>100% first deposit bonus up to 100€! Promocode: WSB100</div>~3" onmouseover="globals.getBookmaker(416).trackBonusOver()" onclick="globals.getBookmaker(416).trackBonusClick();return !window.open(this.href);" href="/bookmaker/18bet/bonus/252"></a>
            </span>
    </td>
    <td class="right odds">
        <div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','2mlnbxv464x0x65lst',416,event,0,1)">2.05</div>
    </td>
    <td class="right odds up">
        <div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','2mlnbxv498x0x0',416,event,0,1)">3.20</div>
    </td>
    <td class="right odds">
        <div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','2mlnbxv464x0x65lsu',416,event,0,1)">3.50</div>
    </td>
    <td class="center info-value"><span>92.1%</span></td>
    <td onmouseout="delayHideTip()" class="check ch3" xparam="The match has already started~2"></td>
</tr>

谢谢您的答复。

1 个答案:

答案 0 :(得分:0)

这里的示例说明了如何由庄家选择tr- 18bet

1 。用aclass=name查找text="18bet",用tr获取父项class=lo

 //a[@class="name" and .="18bet"]/ancestor::tr[contains(@class, "lo")]

2 。用tr找到class=lo,用aclass=name找到子text="18bet"

//tr[contains(@class, "lo") and .//a[@class="name" and .="18bet"]]

1奇数://a[@class="name" and .="18bet"]/ancestor::tr[contains(@class, "lo")]//td[2]

X奇数://a[@class="name" and .="18bet"]/ancestor::tr[contains(@class, "lo")]//td[3]

2奇数://a[@class="name" and .="18bet"]/ancestor::tr[contains(@class, "lo")]//td[4]

Payout奇数://a[@class="name" and .="18bet"]/ancestor::tr[contains(@class, "lo")]//td[5]


Python代码示例:

row = driver.find_element_by_xpath('//a[@class="name" and .="18bet"]/ancestor::tr[contains(@class, "lo")]')

odd_1 = row.find_element_by_xpath('.//td[2]')
odd_x = row.find_element_by_xpath('.//td[3]')
odd_2 = row.find_element_by_xpath('.//td[4]')
odd_payout = row.find_element_by_xpath('.//td[5]')