Answer the question
In order to leave comments, you need to log in
How to get text from all 'p' tags?
beautifulsoup. I can't get text from all 'p' tags.
soup = BeautifulSoup('<html><div><p>hello world 1</p></div><div><p>hello world 2</p></div>
</html>', features='lxml')
tags = soup.find('div')
for x in tags.find_all('p'):
print(x.get_text())
for x in soup.find_all('p'): print(x.get_text())
Answer the question
In order to leave comments, you need to log in
Your whole problem is that you want to get the text from all the 'p' tags, but you are only extracting one 'div' tag
If you want to get the text from all the 'p' tags, then you need to read all the tags first 'div'.
It is worth replacing with
Thanks to this, we get an array in which all our 'div' tags lie. Let's
look at our current array, which stores the tags variable:
After that, it should be iterated
By looping through the elements of the array with tags 'div', we need to get a new array that will contain elements with tags 'p', which are located in a specific tag 'div'
for x in tags:
texts = x.find_all('p')
for text in texts:
print(text.get_text())
soup = BeautifulSoup('<html><div><p>hello world 1</p></div><div><p>hello world 2</p></div></html>', features='lxml')
divs = soup.find_all('div')
for div in divs:
ps = div.find_all('p')
for p in ps:
print(p.get_text())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question