简体   繁体   English

有什么办法可以进入 <option>使用lxml.html解析表单时输入文本?

[英]Is there any way to access <option> text when parsing forms using lxml.html?

I'm trying parse a html form that looks like that: 我正在尝试解析如下所示的html表单:

<select name="country">
<option value="1">Afghanistan</option>
<option value="2">Albania</option>
<option value="3">Algeria</option>
<option value="4">Andorra</option>
....
</select>

After I parse the document using lxml.html.parse, I can access the list of values using: 使用lxml.html.parse解析文档之后,可以使用以下方法访问值列表:

doc.forms[0].elements["country"].value_options

However, this returns a list of raw values (['1', '2', '3', '4' ...]) without the corresponding country names. 但是,这将返回原始值的列表(['1','2','3','4'...]),而没有相应的国家/地区名称。 Is there an easy way to get the contents of the option tag including both text and values? 是否有一种简单的方法来获取选项标签的内容,包括文本和值?

I use xpath to get go through html... try: 我使用xpath通过html ...尝试:

options = doc.xpath("//select[@name='country']/option")
option_text = [option.text for option in options]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM