我尝试运行以下脚本:
#!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。相反,它返回一个空列表。为什么?我想有一些愚蠢的原因,但我试着在谷歌上搜索,没有找到任何可以解释的东西。在
您的xpath表达式将找到
你似乎想要(因为只有3个td-s和一个班级有你不想要的班级)
这些听起来可能相似,但它们不同。 有点像
你应该得到你想要的。在
另外,您不需要使用urllib来打开url,lxml可以使用
lxml.html.parse()
为您实现这一点。在相关问题 更多 >
编程相关推荐