我有以下html:
<td>
<input maxlen="1" name="db" size="1" type="text" value="25"/>
<div style="display:inline-block;position:relative;top:6px;left:0px;width:20px;">
<input class="p_b" name="ta" style="height:1em; width:1.5em;line-height:1em;padding:0px;margin:0px;border:0px;background-color:#f3f3f3" type="submit" value="▴"/>
<input class="p_b" name="ta" style="height:1em; width:1.5em;line-height:1em;padding:0px;margin:0px;border:0px;background-color:#f3f3f3" type="submit" value="▾"/>
</div>
<span style="position:relative;top:8px">
</span>
<input maxlen="1" name="dc" size="1" type="text" value="0"/>
<div style="display:inline-block;position:relative;top:6px;left:0px;width:20px;">
<input class="p_b" name="tb" style="height:1em; width:1.5em;line-height:1em;padding:0px;margin:0px;border:0px;background-color:#f3f3f3" type="submit" value="▴"/>
<input class="p_b" name="tb" style="height:1em; width:1.5em;line-height:1em;padding:0px;margin:0px;border:0px;background-color:#f3f3f3" type="submit" value="▾"/>
</div>
</td>
我需要从 value="25"和 value="0"中提取两个数字。我做了一个解决方法,如:
y = soup.findAll('input', {'type':'text'})
a = re.findall('(?<=value=")(\d*)',str(y))
但我认为应该有更直接的方法通过解析器来做到这一点,有人可以帮忙吗?
最佳答案
尝试在下面的代码行中提取 @value
从每个输入节点
values = [element['value'] for element in soup.findAll('input', {'type':'text'})]
附言请注意,使用正则表达式进行网络抓取是一种非常糟糕的做法 - 有足够多的网络抓取工具可以轻松为您完成此操作(例如,BeautifulSoup 和 lxml 可以在 Python 中使用)
关于python - 在python中解析html值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65305097/