Pythonlxml.htmlXPath“attribute not equal”运算符未按预期工作

2024-09-30 05:20:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试运行以下脚本:

#!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。相反,它返回一个空列表。为什么?我想有一些愚蠢的原因,但我试着在谷歌上搜索,没有找到任何可以解释的东西。在


Tags: fromimport脚本treeurlforrawrequest
1条回答
网友
1楼 · 发布于 2024-09-30 05:20:47

您的xpath表达式将找到

a td element that has a class which is not "naglczas"

你似乎想要(因为只有3个td-s和一个班级有你不想要的班级)

a td element which does not have a class of "naglczas"


这些听起来可能相似,但它们不同。 有点像

tree.xpath('//td[not(@class="naglczas")]')

你应该得到你想要的。在


另外,您不需要使用urllib来打开url,lxml可以使用lxml.html.parse()为您实现这一点。在

相关问题 更多 >

    热门问题