【问题标题】:Parse HTML to get specific tags in Python解析 HTML 以获取 Python 中的特定标签
【发布时间】:2018-10-03 21:08:04
【问题描述】:

我正在尝试使用 Python 解析 HTML 源代码。我为此目的使用BeautifulSoup。我需要得到的是以nameX 格式获取所有td 标签,其中X 从1 开始。所以它们是name1, name2, ...,与我们拥有的一样多。

我怎样才能做到这一点?我使用正则表达式的简单代码不起作用。

soup = BeautifulSoup(response.text,"lxml")
resp=soup.find_all("td",{"id":'name*'})

错误:

IndexError: list index out of range

【问题讨论】:

    标签: python python-3.x beautifulsoup html-parsing string-parsing


    【解决方案1】:

    使用 lambda + 以

    开头
    soup.find_all('td', id=lambda x: x and x.startswith('name'))
    

    或正则表达式

     soup.find_all('td', id=re.compile('^name'))
    

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2013-11-14
      • 2014-03-27
      相关资源
      最近更新 更多