0

I'm trying to parse an HTML source with Python. I'm using BeautifulSoup for the purpose. What I need to get is to get all td tags with ids in the form of nameX format, where X starts from 1. So they are name1, name2, ... as many as we have.

How can I achieve this? My simple code using regex doesn't work.

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

Error:

IndexError: list index out of range

1 Answer 1

1

use lambda + startswith

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

or regex

 soup.find_all('td', id=re.compile('^name'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.