Select all tag (and extract text) between two other tags with BeatifulSoup
up vote
2
down vote
favorite
I would like to extract all instances of a given tag that are included between two tags. Currently I am working with BeautifulSoup.
Below you can find an example:
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
I am interested in selecting all the instances of class 'y' between the two tags 'x', which also have different id's. With regards to the specific example, I would like to select all p's with class = 'y' to then retrieve the text. My final desired output would be: 'some content a', 'some content b', and 'some content c'.
I tried using the findAllNext method, but that gives me 'some content a', 'some content b', 'some content c' AND 'some content d', 'some content e', 'some content f'.
Below is my code
par = BeautifulSoup(HTML_CODE).content, 'lxml')
loc = par.find('p', class_ = 'x', id ='1')
desired = loc.findAllNext('p', class_ = 'y')
Is there any way to avoid selecting also the instances of class = 'y' that appear after the tag with class='x' with id = '2' ?
Thank you.
python html beautifulsoup tags
add a comment |
up vote
2
down vote
favorite
I would like to extract all instances of a given tag that are included between two tags. Currently I am working with BeautifulSoup.
Below you can find an example:
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
I am interested in selecting all the instances of class 'y' between the two tags 'x', which also have different id's. With regards to the specific example, I would like to select all p's with class = 'y' to then retrieve the text. My final desired output would be: 'some content a', 'some content b', and 'some content c'.
I tried using the findAllNext method, but that gives me 'some content a', 'some content b', 'some content c' AND 'some content d', 'some content e', 'some content f'.
Below is my code
par = BeautifulSoup(HTML_CODE).content, 'lxml')
loc = par.find('p', class_ = 'x', id ='1')
desired = loc.findAllNext('p', class_ = 'y')
Is there any way to avoid selecting also the instances of class = 'y' that appear after the tag with class='x' with id = '2' ?
Thank you.
python html beautifulsoup tags
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I would like to extract all instances of a given tag that are included between two tags. Currently I am working with BeautifulSoup.
Below you can find an example:
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
I am interested in selecting all the instances of class 'y' between the two tags 'x', which also have different id's. With regards to the specific example, I would like to select all p's with class = 'y' to then retrieve the text. My final desired output would be: 'some content a', 'some content b', and 'some content c'.
I tried using the findAllNext method, but that gives me 'some content a', 'some content b', 'some content c' AND 'some content d', 'some content e', 'some content f'.
Below is my code
par = BeautifulSoup(HTML_CODE).content, 'lxml')
loc = par.find('p', class_ = 'x', id ='1')
desired = loc.findAllNext('p', class_ = 'y')
Is there any way to avoid selecting also the instances of class = 'y' that appear after the tag with class='x' with id = '2' ?
Thank you.
python html beautifulsoup tags
I would like to extract all instances of a given tag that are included between two tags. Currently I am working with BeautifulSoup.
Below you can find an example:
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
I am interested in selecting all the instances of class 'y' between the two tags 'x', which also have different id's. With regards to the specific example, I would like to select all p's with class = 'y' to then retrieve the text. My final desired output would be: 'some content a', 'some content b', and 'some content c'.
I tried using the findAllNext method, but that gives me 'some content a', 'some content b', 'some content c' AND 'some content d', 'some content e', 'some content f'.
Below is my code
par = BeautifulSoup(HTML_CODE).content, 'lxml')
loc = par.find('p', class_ = 'x', id ='1')
desired = loc.findAllNext('p', class_ = 'y')
Is there any way to avoid selecting also the instances of class = 'y' that appear after the tag with class='x' with id = '2' ?
Thank you.
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
<p class='x' id = '1'> some content 1 <p>
<p class='y' id = 'a'> some content a <p>
<p class='y' id = 'b'> some content b <p>
<p class='y' id = 'c'> some content c <p>
<p class='potentially some other class'> <p>
<p class='x' id = '2'> some content 2 <p>
<p class='y' id = 'd'> some content d <p>
<p class='y' id = 'e'> some content e <p>
<p class='y' id = 'f'> some content f <p>
python html beautifulsoup tags
python html beautifulsoup tags
asked Nov 11 at 18:53
mgiom
226
226
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can start iterating from where you want and end it until found something mark finished.
from bs4 import BeautifulSoup
html = """
<p class='x' id = '1'> some content 1 </p>
<p class='y' id = 'a'> some content a </p>
<p class='y' id = 'b'> some content b </p>
<p class='y' id = 'c'> some content c </p>
<p class='potentially some other class1'> potentially some other class 1 </p>
<p class='potentially some other class2'> potentially some other class 2</p>
<p class='potentially some other class3'> potentially some other class 3 </p>
<p class='x' id = '2'> some content 2 </p>
<p class='y' id = 'd'> some content d </p>
<p class='y' id = 'e'> some content e </p>
<p class='y' id = 'f'> some content f </p>
"""
soup = BeautifulSoup(html,"lxml")
start = soup.find("p",class_="y",id="c")
end = soup.find("p",class_="x",id="2")
def next_ele(ele,result=):
row = ele.find_next("p")
if not row or row == end:
return result
result.append(row)
return next_ele(row,result)
print(next_ele(start))
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
1
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can start iterating from where you want and end it until found something mark finished.
from bs4 import BeautifulSoup
html = """
<p class='x' id = '1'> some content 1 </p>
<p class='y' id = 'a'> some content a </p>
<p class='y' id = 'b'> some content b </p>
<p class='y' id = 'c'> some content c </p>
<p class='potentially some other class1'> potentially some other class 1 </p>
<p class='potentially some other class2'> potentially some other class 2</p>
<p class='potentially some other class3'> potentially some other class 3 </p>
<p class='x' id = '2'> some content 2 </p>
<p class='y' id = 'd'> some content d </p>
<p class='y' id = 'e'> some content e </p>
<p class='y' id = 'f'> some content f </p>
"""
soup = BeautifulSoup(html,"lxml")
start = soup.find("p",class_="y",id="c")
end = soup.find("p",class_="x",id="2")
def next_ele(ele,result=):
row = ele.find_next("p")
if not row or row == end:
return result
result.append(row)
return next_ele(row,result)
print(next_ele(start))
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
1
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
add a comment |
up vote
2
down vote
accepted
You can start iterating from where you want and end it until found something mark finished.
from bs4 import BeautifulSoup
html = """
<p class='x' id = '1'> some content 1 </p>
<p class='y' id = 'a'> some content a </p>
<p class='y' id = 'b'> some content b </p>
<p class='y' id = 'c'> some content c </p>
<p class='potentially some other class1'> potentially some other class 1 </p>
<p class='potentially some other class2'> potentially some other class 2</p>
<p class='potentially some other class3'> potentially some other class 3 </p>
<p class='x' id = '2'> some content 2 </p>
<p class='y' id = 'd'> some content d </p>
<p class='y' id = 'e'> some content e </p>
<p class='y' id = 'f'> some content f </p>
"""
soup = BeautifulSoup(html,"lxml")
start = soup.find("p",class_="y",id="c")
end = soup.find("p",class_="x",id="2")
def next_ele(ele,result=):
row = ele.find_next("p")
if not row or row == end:
return result
result.append(row)
return next_ele(row,result)
print(next_ele(start))
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
1
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can start iterating from where you want and end it until found something mark finished.
from bs4 import BeautifulSoup
html = """
<p class='x' id = '1'> some content 1 </p>
<p class='y' id = 'a'> some content a </p>
<p class='y' id = 'b'> some content b </p>
<p class='y' id = 'c'> some content c </p>
<p class='potentially some other class1'> potentially some other class 1 </p>
<p class='potentially some other class2'> potentially some other class 2</p>
<p class='potentially some other class3'> potentially some other class 3 </p>
<p class='x' id = '2'> some content 2 </p>
<p class='y' id = 'd'> some content d </p>
<p class='y' id = 'e'> some content e </p>
<p class='y' id = 'f'> some content f </p>
"""
soup = BeautifulSoup(html,"lxml")
start = soup.find("p",class_="y",id="c")
end = soup.find("p",class_="x",id="2")
def next_ele(ele,result=):
row = ele.find_next("p")
if not row or row == end:
return result
result.append(row)
return next_ele(row,result)
print(next_ele(start))
You can start iterating from where you want and end it until found something mark finished.
from bs4 import BeautifulSoup
html = """
<p class='x' id = '1'> some content 1 </p>
<p class='y' id = 'a'> some content a </p>
<p class='y' id = 'b'> some content b </p>
<p class='y' id = 'c'> some content c </p>
<p class='potentially some other class1'> potentially some other class 1 </p>
<p class='potentially some other class2'> potentially some other class 2</p>
<p class='potentially some other class3'> potentially some other class 3 </p>
<p class='x' id = '2'> some content 2 </p>
<p class='y' id = 'd'> some content d </p>
<p class='y' id = 'e'> some content e </p>
<p class='y' id = 'f'> some content f </p>
"""
soup = BeautifulSoup(html,"lxml")
start = soup.find("p",class_="y",id="c")
end = soup.find("p",class_="x",id="2")
def next_ele(ele,result=):
row = ele.find_next("p")
if not row or row == end:
return result
result.append(row)
return next_ele(row,result)
print(next_ele(start))
edited Nov 13 at 4:52
answered Nov 12 at 12:31
kcorlidy
1,725317
1,725317
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
1
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
add a comment |
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
1
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
This is useful, but I don't necessarily know how many instances of class 'y' I have between the two tags with class = 'x'. Is there a way to proceed in the iteration you described, but in such a way that it goes on until it finds again an instance of the class 'x' ? Thank you
– mgiom
Nov 12 at 20:41
1
1
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
@mgiom i have edited my code, this only an example. In different situation you need to add more params.
– kcorlidy
Nov 13 at 4:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252053%2fselect-all-tag-and-extract-text-between-two-other-tags-with-beatifulsoup%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown