解析HTML以获取Python中的特定标记

解析HTML以获取Python中的特定标记,python,python-3.x,beautifulsoup,html-parsing,string-parsing,Python,Python 3.x,Beautifulsoup,Html Parsing,String Parsing,我正在尝试用Python解析HTML源代码。我使用的是beautifulsou。我需要得到的是以nameX格式获取所有带有ID的td标记,其中X从1开始。因此,它们是name1,name2,…我们拥有的数量 我怎样才能做到这一点?我使用regex的简单代码不起作用 soup = BeautifulSoup(response.text,"lxml") resp=soup.find_all("td",{"id":'name*'}) 错误: IndexError: list index out of

我正在尝试用Python解析HTML源代码。我使用的是
beautifulsou
。我需要得到的是以
nameX
格式获取所有带有ID的
td
标记,其中X从1开始。因此,它们是
name1,name2,…
我们拥有的数量

我怎样才能做到这一点?我使用regex的简单代码不起作用

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

IndexError: list index out of range

使用lambda+startswith

soup.find_all('td', id=lambda x: x and x.startswith('name'))
或正则表达式

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