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;
}







0















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)










share|improve this question





























    0















    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)










    share|improve this question

























      0












      0








      0








      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)










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 13:26









      wishmasterwishmaster

      35210




      35210
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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"]})





          share|improve this answer


























          • 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 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











          • this method works,but leaves no way for storing each alone right ?

            – wishmaster
            Nov 23 '18 at 14:16



















          0














          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)





          share|improve this answer


























          • how can i store red alone and green alone without using multiple select?

            – wishmaster
            Nov 23 '18 at 13:48












          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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"]})





          share|improve this answer


























          • 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 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











          • this method works,but leaves no way for storing each alone right ?

            – wishmaster
            Nov 23 '18 at 14:16
















          0














          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"]})





          share|improve this answer


























          • 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 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











          • this method works,but leaves no way for storing each alone right ?

            – wishmaster
            Nov 23 '18 at 14:16














          0












          0








          0







          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"]})





          share|improve this answer















          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"]})






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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











          • 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











          • @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











          • @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













          0














          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)





          share|improve this answer


























          • how can i store red alone and green alone without using multiple select?

            – wishmaster
            Nov 23 '18 at 13:48
















          0














          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)





          share|improve this answer


























          • how can i store red alone and green alone without using multiple select?

            – wishmaster
            Nov 23 '18 at 13:48














          0












          0








          0







          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)





          share|improve this answer















          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)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]