Python : lxml xpath 得到两个不同的 类
Python : lxml xpath get two different classes
这是我的示例 Python 代码
import requests
import lxml.html
page = '<div class="aaaa12"><span class="test">22</span><span class="number">33</span></div><div class="dddd13"><span>Kevin</span></div>'
tree = lxml.html.fromstring(page)
number = tree.xpath('//span[@class="number"]/text()')
price = tree.xpath('.//div[@class="dddd13"]/span/text()')
print number
print price
当我 运行 我可以像下面这样
['33']
['Kevin']
不过,我想同时获得两者 = ['33','Kevin']
我试过了
number = tree.xpath('//span[@class="number"]/text() or //div[@class="dddd13"]/span/text()')
我无法获取值。获得两个不同 类 的语法是什么?
使用|
联合运算符连接多个完整路径表达式的结果:
number = tree.xpath('//span[@class="number"]/text() | //div[@class="dddd13"]/span/text()')
演示:
>>> import lxml.html
>>> page = '<div class="aaaa12"><span class="test">22</span><span class="number">33</span></div><div class="dddd13"><span>Kevin</span></div>'
>>> tree = lxml.html.fromstring(page)
>>> tree.xpath('//span[@class="number"]/text() | //div[@class="dddd13"]/span/text()')
['33', 'Kevin']
这是我的示例 Python 代码
import requests
import lxml.html
page = '<div class="aaaa12"><span class="test">22</span><span class="number">33</span></div><div class="dddd13"><span>Kevin</span></div>'
tree = lxml.html.fromstring(page)
number = tree.xpath('//span[@class="number"]/text()')
price = tree.xpath('.//div[@class="dddd13"]/span/text()')
print number
print price
当我 运行 我可以像下面这样
['33']
['Kevin']
不过,我想同时获得两者 = ['33','Kevin'] 我试过了
number = tree.xpath('//span[@class="number"]/text() or //div[@class="dddd13"]/span/text()')
我无法获取值。获得两个不同 类 的语法是什么?
使用|
联合运算符连接多个完整路径表达式的结果:
number = tree.xpath('//span[@class="number"]/text() | //div[@class="dddd13"]/span/text()')
演示:
>>> import lxml.html
>>> page = '<div class="aaaa12"><span class="test">22</span><span class="number">33</span></div><div class="dddd13"><span>Kevin</span></div>'
>>> tree = lxml.html.fromstring(page)
>>> tree.xpath('//span[@class="number"]/text() | //div[@class="dddd13"]/span/text()')
['33', 'Kevin']