python爬虫xpath出来空值,Python爬虫没有找到特定的Xpath

I asked my previous question here:

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

b2751f82e3100a2def1e0d5c9290e4d2.png

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:

bab884db28ab507b0478e6ec4018416f.png

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值