Python爬虫没有找到特定的Xpath [英] Python crawler not finding specific Xpath

查看:287
本文介绍了Python爬虫没有找到特定的Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了我以前的问题:

必须指向元素,而不是文本节点而不是属性。这是一个关键的事情。



最简单的方法是:


  • 获取 td 的文本(父母)

  • 获取 span 's text(child)

  • 将孩子的文本从父母的文本中移除

$ b p>

  span = browser.find_element_by_xpath(// div [@ class ='video-details-inside'] / table // span [ @ class ='added-time'])
td = span.find_element_by_xpath('..')
views = td.text.replace(span.text,'').strip()


I asked my previous question here:

Xpath pulling number in table but nothing after next span

This worked and i managed to see the number i wanted in a firefox plugin called xpath checker. the results show below.

so I know i can find this number with this xpath, but when trying to run a python scrpit to find and save the number it says it cannot find it.

try:
    views = browser.find_element_by_xpath("//div[@class='video-details-inside']/table//span[@class='added-time']/preceding-sibling::text()")
except NoSuchElementException:
    print "NO views"
    views = 'n/a'
    pass  

I no that pass is not best practice but i am just testing this at the moment trying to find the number. I'm wondering if i need to change something on the end of the xpath like .text as the xpath checker normally shows a results a little differently. Like below:

i needed to use the xpath i gave rather than the one used in the above picture because i only want the number and not the date. You can see part of the source in my previous question.

Thanks in advance! scratching my head here.

解决方案

The xpath used in find_element_by_xpath() has to point to an element, not a text node and not an attribute. This is a critical thing here.

The easiest approach here would be to:

  • get the td's text (parent)
  • get the span's text (child)
  • remove child's text from parent's

Code:

span = browser.find_element_by_xpath("//div[@class='video-details-inside']/table//span[@class='added-time']")
td = span.find_element_by_xpath('..')
views = td.text.replace(span.text, '').strip()

这篇关于Python爬虫没有找到特定的Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆