【问题标题】:python BeautifulSoup find all input for specific formpython BeautifulSoup 查找特定表单的所有输入
【发布时间】:2015-09-05 12:06:47
【问题描述】:

我正在尝试使用 BeautifulSoup 仅提取特定表单的输入字段。

使用以下方法提取表单:

soup.find('form')

现在我想提取所有作为该表单子级的输入字段。

我怎样才能用 BS 做到这一点?

【问题讨论】:

  • inputs = soup.find('form').find_all('input') 应该可以解决问题

标签: python html forms beautifulsoup html-parsing


【解决方案1】:

如 cmets 中所述,findfind_all() 用于特定于上下文的搜索:

form = soup.find('form')
inputs = form.find_all('input')

如果您只想直接使用input 元素,请添加recursive=False

form.find_all('input', recursive=False)

或者,使用CSS selectors

soup.select("form input")

而且,只获得直接的input 子元素:

soup.select("form > input")

【讨论】:

  • 请注意,除了input 之外,还有更多您可能关心的表单元素,包括buttonselecttextareaw3.org/TR/html52/sec-forms.html
  • 我假设表单中的所有元素都有一个 "name=" 属性:form = soup.select_one('form#form_id'); fields = form.select('[name]')
猜你喜欢
  • 2012-10-25
  • 1970-01-01
  • 2023-03-15
  • 2020-12-15
  • 2011-12-05
  • 2011-05-16
  • 2013-05-03
  • 1970-01-01
  • 2023-01-18
相关资源
最近更新 更多