返回的网页抓取元素不是我要寻找的值

时间:2019-07-19 23:10:42

标签: python selenium webdriver driver

使用硒和phantomJS我正在尝试从这个赛马统计网站上抓取赛道名称。我似乎无法在我要寻找的元素中获得实际名称。任何帮助将不胜感激。

site = "https://www.drf.com/live_odds"

driver = webdriver.PhantomJS(my_directory)
driver.get(site)

trackname = driver.find_elements_by_xpath('//span[@class="titleTrack"]')

num_of_tracks = len(trackname)
print(num_of_tracks)

for i in range(num_of_tracks):
    print(trackname[i])

driver.close()

返回此:

<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345793")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345794")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345795")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345796")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345797")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345798")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345799")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345800")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345801")>
<selenium.webdriver.remote.webelement.WebElement (session="4286dc50-aa79-11e9-80c7-21f36addf5ef", element=":wdc:1563577345802")>

其中有10个,我认为其中有10个曲目名称。

2 个答案:

答案 0 :(得分:0)

您已经找到了Web元素。现在,您需要从这些元素中获取文本。试试这个:

for i in range(num_of_tracks):
    print(trackname[i].text)

答案 1 :(得分:0)

<span class="titleTrack">
    <a href="/live_odds/winodds/track/AUS-AUC/AUS/5/D">AUS-Flemington</a>
</span>

标题包含在<a>标记中。所以我用'//span[@class="titleTrack"]/a'

尝试了xpath

然后输入文本值element.text

for i in range(num_of_tracks):
    print(trackname[i].text)