BeautifulSoup4 Find Method











up vote
0
down vote

favorite












I tried scraping some a number from yahoo finance using python3, but all I get is a "None".



from bs4 import BeautifulSoup
import requests

source = requests.get('https://finance.yahoo.com/quote/SWCH?
p=SWCH&.tsrc=fin-srch').text

soup = BeautifulSoup(source, 'lxml')

price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')

print(price)


Thanks,
R.Vij










share|improve this question




























    up vote
    0
    down vote

    favorite












    I tried scraping some a number from yahoo finance using python3, but all I get is a "None".



    from bs4 import BeautifulSoup
    import requests

    source = requests.get('https://finance.yahoo.com/quote/SWCH?
    p=SWCH&.tsrc=fin-srch').text

    soup = BeautifulSoup(source, 'lxml')

    price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')

    print(price)


    Thanks,
    R.Vij










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I tried scraping some a number from yahoo finance using python3, but all I get is a "None".



      from bs4 import BeautifulSoup
      import requests

      source = requests.get('https://finance.yahoo.com/quote/SWCH?
      p=SWCH&.tsrc=fin-srch').text

      soup = BeautifulSoup(source, 'lxml')

      price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')

      print(price)


      Thanks,
      R.Vij










      share|improve this question















      I tried scraping some a number from yahoo finance using python3, but all I get is a "None".



      from bs4 import BeautifulSoup
      import requests

      source = requests.get('https://finance.yahoo.com/quote/SWCH?
      p=SWCH&.tsrc=fin-srch').text

      soup = BeautifulSoup(source, 'lxml')

      price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')

      print(price)


      Thanks,
      R.Vij







      python python-3.x beautifulsoup






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 2:54

























      asked Nov 11 at 2:48









      R.Vij

      176




      176
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.



          Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.



          I revised your find statement to the following one:



          soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))


          The following code returns the current price of SWCH



          from bs4 import BeautifulSoup
          import requests
          source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
          soup = BeautifulSoup(source, 'lxml')
          price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
          print(price.text) # 9.29 for now





          share|improve this answer





















          • Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
            – R.Vij
            Nov 11 at 3:53










          • Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
            – Eternal_flame-AD
            Nov 11 at 3:58




















          up vote
          0
          down vote













          'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:



          soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())  
          #<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...





          share|improve this answer





















          • I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
            – R.Vij
            Nov 11 at 3: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',
          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%2f53245424%2fbeautifulsoup4-find-method%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








          up vote
          1
          down vote



          accepted










          Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.



          Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.



          I revised your find statement to the following one:



          soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))


          The following code returns the current price of SWCH



          from bs4 import BeautifulSoup
          import requests
          source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
          soup = BeautifulSoup(source, 'lxml')
          price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
          print(price.text) # 9.29 for now





          share|improve this answer





















          • Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
            – R.Vij
            Nov 11 at 3:53










          • Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
            – Eternal_flame-AD
            Nov 11 at 3:58

















          up vote
          1
          down vote



          accepted










          Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.



          Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.



          I revised your find statement to the following one:



          soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))


          The following code returns the current price of SWCH



          from bs4 import BeautifulSoup
          import requests
          source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
          soup = BeautifulSoup(source, 'lxml')
          price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
          print(price.text) # 9.29 for now





          share|improve this answer





















          • Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
            – R.Vij
            Nov 11 at 3:53










          • Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
            – Eternal_flame-AD
            Nov 11 at 3:58















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.



          Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.



          I revised your find statement to the following one:



          soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))


          The following code returns the current price of SWCH



          from bs4 import BeautifulSoup
          import requests
          source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
          soup = BeautifulSoup(source, 'lxml')
          price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
          print(price.text) # 9.29 for now





          share|improve this answer












          Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.



          Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.



          I revised your find statement to the following one:



          soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))


          The following code returns the current price of SWCH



          from bs4 import BeautifulSoup
          import requests
          source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
          soup = BeautifulSoup(source, 'lxml')
          price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
          print(price.text) # 9.29 for now






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 3:24









          Eternal_flame-AD

          3126




          3126












          • Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
            – R.Vij
            Nov 11 at 3:53










          • Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
            – Eternal_flame-AD
            Nov 11 at 3:58




















          • Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
            – R.Vij
            Nov 11 at 3:53










          • Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
            – Eternal_flame-AD
            Nov 11 at 3:58


















          Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
          – R.Vij
          Nov 11 at 3:53




          Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
          – R.Vij
          Nov 11 at 3:53












          Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
          – Eternal_flame-AD
          Nov 11 at 3:58






          Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first x and stuff) and then checks if the actual class names are a superset of the required class names.
          – Eternal_flame-AD
          Nov 11 at 3:58














          up vote
          0
          down vote













          'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:



          soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())  
          #<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...





          share|improve this answer





















          • I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
            – R.Vij
            Nov 11 at 3:48















          up vote
          0
          down vote













          'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:



          soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())  
          #<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...





          share|improve this answer





















          • I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
            – R.Vij
            Nov 11 at 3:48













          up vote
          0
          down vote










          up vote
          0
          down vote









          'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:



          soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())  
          #<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...





          share|improve this answer












          'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:



          soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())  
          #<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 2:54









          DYZ

          24k61948




          24k61948












          • I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
            – R.Vij
            Nov 11 at 3:48


















          • I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
            – R.Vij
            Nov 11 at 3:48
















          I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
          – R.Vij
          Nov 11 at 3:48




          I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
          – R.Vij
          Nov 11 at 3:48


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245424%2fbeautifulsoup4-find-method%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

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly