使用 lxml 和 Python 解析部分输入标签
Parsing part of an input tag with lxml and Python
我有一个这样的输入标签:
<input name="sheet" value="5B" tabindex="994" data-enpassid="__11" type="submit">
不,我只想获取赋予值属性的内容(在本例中为 5B)。这可能使用 lxml 吗?如果是,如何使用?
试试这个。我使用 css 选择器和 xpath 创建了两个表达式。他们都会给你 5B
作为结果:
html='''
<input name="sheet" value="5B" tabindex="994" data-enpassid="__11" type="submit">
'''
from lxml.html import fromstring
root = fromstring(html)
item = root.cssselect("input")[0].attrib['value']
item1 = root.xpath("//input/@value")[0]
print(item, item1)
结果:
5B, 5B
我有一个这样的输入标签:
<input name="sheet" value="5B" tabindex="994" data-enpassid="__11" type="submit">
不,我只想获取赋予值属性的内容(在本例中为 5B)。这可能使用 lxml 吗?如果是,如何使用?
试试这个。我使用 css 选择器和 xpath 创建了两个表达式。他们都会给你 5B
作为结果:
html='''
<input name="sheet" value="5B" tabindex="994" data-enpassid="__11" type="submit">
'''
from lxml.html import fromstring
root = fromstring(html)
item = root.cssselect("input")[0].attrib['value']
item1 = root.xpath("//input/@value")[0]
print(item, item1)
结果:
5B, 5B