python - 在python中解析html值

标签 python html beautifulsoup

我有以下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'})]
附言请注意,使用正则表达式进行网络抓取是一种非常糟糕的做法 - 有足够多的网络抓取工具可以轻松为您完成此操作(例如,BeautifulSouplxml 可以在 Python 中使用)

关于python - 在python中解析html值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65305097/

相关文章:

Python 图表 : Latex Math rendering of node labels

python - psycopg2.编程错误: column of relation does not exist

javascript - 如何使用查询更改 tinymce 背景 onchange 的颜色

python - 按名称、beautiful soup 和 python 获取元标记内容

python - 如何查找仅具有某些属性的标签 - BeautifulSoup

python - 如何安装kaggle_datasets?

python - 在同一台机器上运行 Python 3.2 和 2.7

python - Flask-SQLAlchemy - 模型没有属性 'foreign_keys'

javascript - JavaScript 中的此属性范围

html - 用于键盘导航的 tabindex 或焦点