Select several tag items with BeautifulSoup in python
I have the next html:
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
I want iterate over each article tags and I really get with:
from requests import get
from bs4 import BeautifulSoup
response = get("https://example.com")
soup = BeautifulSoup(response.text, "html.parser")
issues = soup.find_all("article", {"class": "issue"})
for issue in issues:
print (issue)
And now I want select from each article tag the span tag with class 'description', but when I call 'issue.span' only select the first tag found.
Any suggestions?
Thanks in advance.
python web-scraping beautifulsoup
add a comment |
I have the next html:
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
I want iterate over each article tags and I really get with:
from requests import get
from bs4 import BeautifulSoup
response = get("https://example.com")
soup = BeautifulSoup(response.text, "html.parser")
issues = soup.find_all("article", {"class": "issue"})
for issue in issues:
print (issue)
And now I want select from each article tag the span tag with class 'description', but when I call 'issue.span' only select the first tag found.
Any suggestions?
Thanks in advance.
python web-scraping beautifulsoup
add a comment |
I have the next html:
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
I want iterate over each article tags and I really get with:
from requests import get
from bs4 import BeautifulSoup
response = get("https://example.com")
soup = BeautifulSoup(response.text, "html.parser")
issues = soup.find_all("article", {"class": "issue"})
for issue in issues:
print (issue)
And now I want select from each article tag the span tag with class 'description', but when I call 'issue.span' only select the first tag found.
Any suggestions?
Thanks in advance.
python web-scraping beautifulsoup
I have the next html:
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
I want iterate over each article tags and I really get with:
from requests import get
from bs4 import BeautifulSoup
response = get("https://example.com")
soup = BeautifulSoup(response.text, "html.parser")
issues = soup.find_all("article", {"class": "issue"})
for issue in issues:
print (issue)
And now I want select from each article tag the span tag with class 'description', but when I call 'issue.span' only select the first tag found.
Any suggestions?
Thanks in advance.
python web-scraping beautifulsoup
python web-scraping beautifulsoup
asked Nov 24 '18 at 9:14
user202890user202890
203
203
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Do you mean as follows. using CSS selectors in combination? I use a descendant combinator to combine selectors such that you get span.description
children of article.issue
. This way of writing means you will only get descriptions where they exist so no additional test is needed.
from bs4 import BeautifulSoup
html = '''
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
descriptions = soup.select('article.issue span.description')
descriptions = [description.text for description in descriptions]
print(descriptions)
Result:
For yours, you would need to select the span.description
from the issue
print([issue.select('span.description') for issue in issues])
1
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
1
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53456752%2fselect-several-tag-items-with-beautifulsoup-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do you mean as follows. using CSS selectors in combination? I use a descendant combinator to combine selectors such that you get span.description
children of article.issue
. This way of writing means you will only get descriptions where they exist so no additional test is needed.
from bs4 import BeautifulSoup
html = '''
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
descriptions = soup.select('article.issue span.description')
descriptions = [description.text for description in descriptions]
print(descriptions)
Result:
For yours, you would need to select the span.description
from the issue
print([issue.select('span.description') for issue in issues])
1
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
1
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
add a comment |
Do you mean as follows. using CSS selectors in combination? I use a descendant combinator to combine selectors such that you get span.description
children of article.issue
. This way of writing means you will only get descriptions where they exist so no additional test is needed.
from bs4 import BeautifulSoup
html = '''
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
descriptions = soup.select('article.issue span.description')
descriptions = [description.text for description in descriptions]
print(descriptions)
Result:
For yours, you would need to select the span.description
from the issue
print([issue.select('span.description') for issue in issues])
1
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
1
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
add a comment |
Do you mean as follows. using CSS selectors in combination? I use a descendant combinator to combine selectors such that you get span.description
children of article.issue
. This way of writing means you will only get descriptions where they exist so no additional test is needed.
from bs4 import BeautifulSoup
html = '''
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
descriptions = soup.select('article.issue span.description')
descriptions = [description.text for description in descriptions]
print(descriptions)
Result:
For yours, you would need to select the span.description
from the issue
print([issue.select('span.description') for issue in issues])
Do you mean as follows. using CSS selectors in combination? I use a descendant combinator to combine selectors such that you get span.description
children of article.issue
. This way of writing means you will only get descriptions where they exist so no additional test is needed.
from bs4 import BeautifulSoup
html = '''
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
descriptions = soup.select('article.issue span.description')
descriptions = [description.text for description in descriptions]
print(descriptions)
Result:
For yours, you would need to select the span.description
from the issue
print([issue.select('span.description') for issue in issues])
edited Nov 24 '18 at 9:40
answered Nov 24 '18 at 9:29
QHarrQHarr
32.4k82042
32.4k82042
1
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
1
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
add a comment |
1
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
1
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
1
1
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
+1 for using css. I sometimes think beautiful soup is a conspiracy to keep python users from learning css.
– pguardiario
Nov 24 '18 at 9:50
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
@pguardiario Thanks. I am new to python but can’t imagine not using css.
– QHarr
Nov 24 '18 at 9:51
1
1
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
That's probably because you're a legit coder as opposed to casual users who mostly just paste stuff from SO :)
– pguardiario
Nov 24 '18 at 9:57
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.
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%2f53456752%2fselect-several-tag-items-with-beautifulsoup-in-python%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