findall returning only the last attribute
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
i have searched for similar questions but didn't find what i need.
I am searching a web for two attributes in this case red
and green
in span
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.findAll("span",{"class":"red","class":"green"})
print(nameList)
However i am only getting the green attribute,i tried using
nameList,nameList2=soup.findAll("span",{"class":"red","class":"green"})
but i get the error ValueError: too many values to unpack (expected 2)
is there a way to print both and store each attribute in a namelist (without using multiple findAll
)
python web-scraping findall
add a comment |
i have searched for similar questions but didn't find what i need.
I am searching a web for two attributes in this case red
and green
in span
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.findAll("span",{"class":"red","class":"green"})
print(nameList)
However i am only getting the green attribute,i tried using
nameList,nameList2=soup.findAll("span",{"class":"red","class":"green"})
but i get the error ValueError: too many values to unpack (expected 2)
is there a way to print both and store each attribute in a namelist (without using multiple findAll
)
python web-scraping findall
add a comment |
i have searched for similar questions but didn't find what i need.
I am searching a web for two attributes in this case red
and green
in span
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.findAll("span",{"class":"red","class":"green"})
print(nameList)
However i am only getting the green attribute,i tried using
nameList,nameList2=soup.findAll("span",{"class":"red","class":"green"})
but i get the error ValueError: too many values to unpack (expected 2)
is there a way to print both and store each attribute in a namelist (without using multiple findAll
)
python web-scraping findall
i have searched for similar questions but didn't find what i need.
I am searching a web for two attributes in this case red
and green
in span
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.findAll("span",{"class":"red","class":"green"})
print(nameList)
However i am only getting the green attribute,i tried using
nameList,nameList2=soup.findAll("span",{"class":"red","class":"green"})
but i get the error ValueError: too many values to unpack (expected 2)
is there a way to print both and store each attribute in a namelist (without using multiple findAll
)
python web-scraping findall
python web-scraping findall
asked Nov 23 '18 at 13:26
wishmasterwishmaster
35210
35210
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can try to use CSS selector to match span
with both class names as below:
nameList = soup.select("span.red, span.green")
If you still want to use findAll
, try
nameList = soup.findAll("span",{"class":["red", "green"]})
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
@timmy ,soup.select("span.red, span.green")
select both,soup.select("span.green")
select green only,soup.select("span.red")
select red only
– Andersson
Nov 23 '18 at 13:56
@Andersson i understand,this a fine method. However why isfindAll
not storing both attributes?
– wishmaster
Nov 23 '18 at 14:01
@timmy , because you should pass class names as list:soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
|
show 3 more comments
As your red and green are the only class attributes you can just check for span with class attribute
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.select("span[class]")
print(nameList)
To have separate lists you can use 2 selects by class name:
reds = soup.select('span.red')
greens = soup.select('span.green')
print(reds,greens)
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
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%2f53447575%2ffindall-returning-only-the-last-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try to use CSS selector to match span
with both class names as below:
nameList = soup.select("span.red, span.green")
If you still want to use findAll
, try
nameList = soup.findAll("span",{"class":["red", "green"]})
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
@timmy ,soup.select("span.red, span.green")
select both,soup.select("span.green")
select green only,soup.select("span.red")
select red only
– Andersson
Nov 23 '18 at 13:56
@Andersson i understand,this a fine method. However why isfindAll
not storing both attributes?
– wishmaster
Nov 23 '18 at 14:01
@timmy , because you should pass class names as list:soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
|
show 3 more comments
You can try to use CSS selector to match span
with both class names as below:
nameList = soup.select("span.red, span.green")
If you still want to use findAll
, try
nameList = soup.findAll("span",{"class":["red", "green"]})
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
@timmy ,soup.select("span.red, span.green")
select both,soup.select("span.green")
select green only,soup.select("span.red")
select red only
– Andersson
Nov 23 '18 at 13:56
@Andersson i understand,this a fine method. However why isfindAll
not storing both attributes?
– wishmaster
Nov 23 '18 at 14:01
@timmy , because you should pass class names as list:soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
|
show 3 more comments
You can try to use CSS selector to match span
with both class names as below:
nameList = soup.select("span.red, span.green")
If you still want to use findAll
, try
nameList = soup.findAll("span",{"class":["red", "green"]})
You can try to use CSS selector to match span
with both class names as below:
nameList = soup.select("span.red, span.green")
If you still want to use findAll
, try
nameList = soup.findAll("span",{"class":["red", "green"]})
edited Nov 23 '18 at 14:04
answered Nov 23 '18 at 13:30
AnderssonAndersson
39.1k113669
39.1k113669
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
@timmy ,soup.select("span.red, span.green")
select both,soup.select("span.green")
select green only,soup.select("span.red")
select red only
– Andersson
Nov 23 '18 at 13:56
@Andersson i understand,this a fine method. However why isfindAll
not storing both attributes?
– wishmaster
Nov 23 '18 at 14:01
@timmy , because you should pass class names as list:soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
|
show 3 more comments
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
@timmy ,soup.select("span.red, span.green")
select both,soup.select("span.green")
select green only,soup.select("span.red")
select red only
– Andersson
Nov 23 '18 at 13:56
@Andersson i understand,this a fine method. However why isfindAll
not storing both attributes?
– wishmaster
Nov 23 '18 at 14:01
@timmy , because you should pass class names as list:soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
it works, is there a way to store red alone and green alone?
– wishmaster
Nov 23 '18 at 13:44
@timmy ,
soup.select("span.red, span.green")
select both, soup.select("span.green")
select green only, soup.select("span.red")
select red only– Andersson
Nov 23 '18 at 13:56
@timmy ,
soup.select("span.red, span.green")
select both, soup.select("span.green")
select green only, soup.select("span.red")
select red only– Andersson
Nov 23 '18 at 13:56
@Andersson i understand,this a fine method. However why is
findAll
not storing both attributes?– wishmaster
Nov 23 '18 at 14:01
@Andersson i understand,this a fine method. However why is
findAll
not storing both attributes?– wishmaster
Nov 23 '18 at 14:01
@timmy , because you should pass class names as list:
soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
@timmy , because you should pass class names as list:
soup.findAll("span",{"class":["red","green"]})
– Andersson
Nov 23 '18 at 14:04
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
this method works,but leaves no way for storing each alone right ?
– wishmaster
Nov 23 '18 at 14:16
|
show 3 more comments
As your red and green are the only class attributes you can just check for span with class attribute
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.select("span[class]")
print(nameList)
To have separate lists you can use 2 selects by class name:
reds = soup.select('span.red')
greens = soup.select('span.green')
print(reds,greens)
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
add a comment |
As your red and green are the only class attributes you can just check for span with class attribute
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.select("span[class]")
print(nameList)
To have separate lists you can use 2 selects by class name:
reds = soup.select('span.red')
greens = soup.select('span.green')
print(reds,greens)
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
add a comment |
As your red and green are the only class attributes you can just check for span with class attribute
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.select("span[class]")
print(nameList)
To have separate lists you can use 2 selects by class name:
reds = soup.select('span.red')
greens = soup.select('span.green')
print(reds,greens)
As your red and green are the only class attributes you can just check for span with class attribute
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.select("span[class]")
print(nameList)
To have separate lists you can use 2 selects by class name:
reds = soup.select('span.red')
greens = soup.select('span.green')
print(reds,greens)
edited Nov 23 '18 at 13:52
answered Nov 23 '18 at 13:42
QHarrQHarr
37.7k82245
37.7k82245
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
add a comment |
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
how can i store red alone and green alone without using multiple select?
– wishmaster
Nov 23 '18 at 13:48
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%2f53447575%2ffindall-returning-only-the-last-attribute%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