Python lxml.html XPath“属性不等于"操作员未按预期工作
我正在尝试运行以下脚本:
I'm trying to run the following script:
#!python
from urllib import urlopen #urllib.request for python3
from lxml import html
url = 'http://mpk.lodz.pl/rozklady/1_11_D2D3/00d2/00d2t001.htm?r=KOZINY'+\
'%20-%20Srebrzy%F1ska,%20Cmentarna,%20Legion%F3w,%20pl.%20Wolno%B6ci'+\
',%20Pomorska,%20Kili%F1skiego,%20Przybyszewskiego%20-%20LODOWA'
raw_html = urlopen(url).read()
tree = html.fromstring(raw_html) #need to .decode('windows-1250') in python3
ret = tree.xpath('//td [@class!="naglczas"]')
print ret
assert(len(ret)==1)
我希望它选择一个未将其类别设置为"naglczas"的td.相反,它向我返回了一个空列表.这是为什么?我想这是有一些愚蠢的原因,但我尝试使用Google谷歌搜索,但没有发现任何可以解释的原因.
I expect it to select the one td that doesn't have its class set to 'naglczas'. Instead, it returns me an empty list. Why is that? I guess there's some silly reason, but I tried googling and found nothing that would explain it.
您的xpath表达式将找到
Your xpath expression will find
td元素的类不是"naglczas"
a td element that has a class which is not "naglczas"
您似乎想要(因为同一个班级的仅有3个td-s具有您不想要的班级)
You seem to want(since the only 3 td-s with a class have the same class you don't want)
一个没有"naglczas"类的td元素
a td element which does not have a class of "naglczas"
这些听起来可能很相似,但是却有所不同.
像
Those might sound similar, but they are different.
Something like
tree.xpath('//td[not(@class="naglczas")]')
应该为您带来想要的东西.
should get you what you want.
另外,您不需要使用urllib打开url,lxml可以使用lxml.html.parse()
为您完成此操作.
Also, you don't need to use urllib to open the url, lxml can do that for you, using lxml.html.parse()
.