for-loop isn't picking up the if-else statement Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Replacements for switch statement in Python?Styling multi-line conditions in 'if' statements?Accessing the index in 'for' loops?A 'for' loop to iterate over an enum in JavaPython's equivalent of && (logical-and) in an if-statementPutting a simple if-then-else statement on one lineLoop through an array in JavaScriptIterating over dictionaries using 'for' loopsWhy does python use 'else' after for and while loops?if else statement in AngularJS templates

Where is the Next Backup Size entry on iOS 12?

what is the log of the PDF for a Normal Distribution?

Why are vacuum tubes still used in amateur radios?

Does the Black Tentacles spell do damage twice at the start of turn to an already restrained creature?

Relating to the President and obstruction, were Mueller's conclusions preordained?

How to change the tick of the color bar legend to black

Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

How does the math work when buying airline miles?

I can't produce songs

Positioning dot before text in math mode

What would you call this weird metallic apparatus that allows you to lift people?

Universal covering space of the real projective line?

Monty Hall Problem-Probability Paradox

How can a team of shapeshifters communicate?

Why is std::move not [[nodiscard]] in C++20?

How to force a browser when connecting to a specific domain to be https only using only the client machine?

Sally's older brother

Tips to organize LaTeX presentations for a semester

Why is it faster to reheat something than it is to cook it?

Can you force honesty by using the Speak with Dead and Zone of Truth spells together?

Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?

Resize vertical bars (absolute-value symbols)

Differences to CCompactSize and CVarInt



for-loop isn't picking up the if-else statement



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Replacements for switch statement in Python?Styling multi-line conditions in 'if' statements?Accessing the index in 'for' loops?A 'for' loop to iterate over an enum in JavaPython's equivalent of && (logical-and) in an if-statementPutting a simple if-then-else statement on one lineLoop through an array in JavaScriptIterating over dictionaries using 'for' loopsWhy does python use 'else' after for and while loops?if else statement in AngularJS templates



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm struggling to print out 10 rows/outputs in this function. So far, I've two outputs only. Either the for-loop isn't reading line by line by tag, or the if-else statement is buggy.



# copy and paste the url from indeed using your search term
URL = 'https://www.indeed.com/jobs?q=data+scientist+%2485%2C000&l=New+York'

#conducting a request of the stated URL above:
page = requests.get(URL)

#specifying a desired format of “page” using the html parser - this allows python to read the various components of the page, rather than treating it as one long string.

soup = BeautifulSoup(page.text, 'html.parser')
#printing soup in a more structured tree format that makes for easier reading
print(soup.prettify())


def extract_salary_from_result(soup):
salaries = []
for td in soup.find_all(name='td', attrs='class':'snip'):
for div in td.find_all(name='div', attrs='class':'salarySnippet'):
salary = div.find_all(name='span', attrs='class':'salary no-wrap')
#print('salary in 2nd for-loop', salary)
#if len(salary) > 0:
for c in salary:
salaries.append(c.text.strip())
print('salary in if statement',salaries)
else:
salaries.append('Nothing_found')
print('salary in else statement',salaries)
return(salaries)
salary = extract_salary_from_result(soup)
print('salary is: ', salary)


Currently outputs are:



salary in if statement ['$115,000 a year']
salary in else statement ['$115,000 a year', 'Nothing_found']
salary is: ['$115,000 a year', 'Nothing_found']


the ideal output should be:



['$115,000 a year', 'Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found'] 









share|improve this question




























    0















    I'm struggling to print out 10 rows/outputs in this function. So far, I've two outputs only. Either the for-loop isn't reading line by line by tag, or the if-else statement is buggy.



    # copy and paste the url from indeed using your search term
    URL = 'https://www.indeed.com/jobs?q=data+scientist+%2485%2C000&l=New+York'

    #conducting a request of the stated URL above:
    page = requests.get(URL)

    #specifying a desired format of “page” using the html parser - this allows python to read the various components of the page, rather than treating it as one long string.

    soup = BeautifulSoup(page.text, 'html.parser')
    #printing soup in a more structured tree format that makes for easier reading
    print(soup.prettify())


    def extract_salary_from_result(soup):
    salaries = []
    for td in soup.find_all(name='td', attrs='class':'snip'):
    for div in td.find_all(name='div', attrs='class':'salarySnippet'):
    salary = div.find_all(name='span', attrs='class':'salary no-wrap')
    #print('salary in 2nd for-loop', salary)
    #if len(salary) > 0:
    for c in salary:
    salaries.append(c.text.strip())
    print('salary in if statement',salaries)
    else:
    salaries.append('Nothing_found')
    print('salary in else statement',salaries)
    return(salaries)
    salary = extract_salary_from_result(soup)
    print('salary is: ', salary)


    Currently outputs are:



    salary in if statement ['$115,000 a year']
    salary in else statement ['$115,000 a year', 'Nothing_found']
    salary is: ['$115,000 a year', 'Nothing_found']


    the ideal output should be:



    ['$115,000 a year', 'Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found'] 









    share|improve this question
























      0












      0








      0








      I'm struggling to print out 10 rows/outputs in this function. So far, I've two outputs only. Either the for-loop isn't reading line by line by tag, or the if-else statement is buggy.



      # copy and paste the url from indeed using your search term
      URL = 'https://www.indeed.com/jobs?q=data+scientist+%2485%2C000&l=New+York'

      #conducting a request of the stated URL above:
      page = requests.get(URL)

      #specifying a desired format of “page” using the html parser - this allows python to read the various components of the page, rather than treating it as one long string.

      soup = BeautifulSoup(page.text, 'html.parser')
      #printing soup in a more structured tree format that makes for easier reading
      print(soup.prettify())


      def extract_salary_from_result(soup):
      salaries = []
      for td in soup.find_all(name='td', attrs='class':'snip'):
      for div in td.find_all(name='div', attrs='class':'salarySnippet'):
      salary = div.find_all(name='span', attrs='class':'salary no-wrap')
      #print('salary in 2nd for-loop', salary)
      #if len(salary) > 0:
      for c in salary:
      salaries.append(c.text.strip())
      print('salary in if statement',salaries)
      else:
      salaries.append('Nothing_found')
      print('salary in else statement',salaries)
      return(salaries)
      salary = extract_salary_from_result(soup)
      print('salary is: ', salary)


      Currently outputs are:



      salary in if statement ['$115,000 a year']
      salary in else statement ['$115,000 a year', 'Nothing_found']
      salary is: ['$115,000 a year', 'Nothing_found']


      the ideal output should be:



      ['$115,000 a year', 'Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found'] 









      share|improve this question














      I'm struggling to print out 10 rows/outputs in this function. So far, I've two outputs only. Either the for-loop isn't reading line by line by tag, or the if-else statement is buggy.



      # copy and paste the url from indeed using your search term
      URL = 'https://www.indeed.com/jobs?q=data+scientist+%2485%2C000&l=New+York'

      #conducting a request of the stated URL above:
      page = requests.get(URL)

      #specifying a desired format of “page” using the html parser - this allows python to read the various components of the page, rather than treating it as one long string.

      soup = BeautifulSoup(page.text, 'html.parser')
      #printing soup in a more structured tree format that makes for easier reading
      print(soup.prettify())


      def extract_salary_from_result(soup):
      salaries = []
      for td in soup.find_all(name='td', attrs='class':'snip'):
      for div in td.find_all(name='div', attrs='class':'salarySnippet'):
      salary = div.find_all(name='span', attrs='class':'salary no-wrap')
      #print('salary in 2nd for-loop', salary)
      #if len(salary) > 0:
      for c in salary:
      salaries.append(c.text.strip())
      print('salary in if statement',salaries)
      else:
      salaries.append('Nothing_found')
      print('salary in else statement',salaries)
      return(salaries)
      salary = extract_salary_from_result(soup)
      print('salary is: ', salary)


      Currently outputs are:



      salary in if statement ['$115,000 a year']
      salary in else statement ['$115,000 a year', 'Nothing_found']
      salary is: ['$115,000 a year', 'Nothing_found']


      the ideal output should be:



      ['$115,000 a year', 'Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found'] 






      python for-loop if-statement web-scraping






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 23:04









      James KongJames Kong

      62




      62






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You don't have an if-else there. You have a for-else. You probably want this:



           for c in salary:
          if len(c) > 0:
          salaries.append(c.text.strip())
          print('salary in if statement',salaries)
          else:
          salaries.append('Nothing_found')
          print('salary in else statement',salaries)


          The else in for-else doesn't take the place of else in if-else. It's more like a "finally" statement (it executes after the loop ends if no break statement was encountered).






          share|improve this answer




















          • 1





            @TemporalWolf true, edited

            – gmds
            Mar 8 at 23:31











          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%2f55072224%2ffor-loop-isnt-picking-up-the-if-else-statement%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









          0














          You don't have an if-else there. You have a for-else. You probably want this:



           for c in salary:
          if len(c) > 0:
          salaries.append(c.text.strip())
          print('salary in if statement',salaries)
          else:
          salaries.append('Nothing_found')
          print('salary in else statement',salaries)


          The else in for-else doesn't take the place of else in if-else. It's more like a "finally" statement (it executes after the loop ends if no break statement was encountered).






          share|improve this answer




















          • 1





            @TemporalWolf true, edited

            – gmds
            Mar 8 at 23:31















          0














          You don't have an if-else there. You have a for-else. You probably want this:



           for c in salary:
          if len(c) > 0:
          salaries.append(c.text.strip())
          print('salary in if statement',salaries)
          else:
          salaries.append('Nothing_found')
          print('salary in else statement',salaries)


          The else in for-else doesn't take the place of else in if-else. It's more like a "finally" statement (it executes after the loop ends if no break statement was encountered).






          share|improve this answer




















          • 1





            @TemporalWolf true, edited

            – gmds
            Mar 8 at 23:31













          0












          0








          0







          You don't have an if-else there. You have a for-else. You probably want this:



           for c in salary:
          if len(c) > 0:
          salaries.append(c.text.strip())
          print('salary in if statement',salaries)
          else:
          salaries.append('Nothing_found')
          print('salary in else statement',salaries)


          The else in for-else doesn't take the place of else in if-else. It's more like a "finally" statement (it executes after the loop ends if no break statement was encountered).






          share|improve this answer















          You don't have an if-else there. You have a for-else. You probably want this:



           for c in salary:
          if len(c) > 0:
          salaries.append(c.text.strip())
          print('salary in if statement',salaries)
          else:
          salaries.append('Nothing_found')
          print('salary in else statement',salaries)


          The else in for-else doesn't take the place of else in if-else. It's more like a "finally" statement (it executes after the loop ends if no break statement was encountered).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 23:30

























          answered Mar 8 at 23:11









          gmdsgmds

          6,981830




          6,981830







          • 1





            @TemporalWolf true, edited

            – gmds
            Mar 8 at 23:31












          • 1





            @TemporalWolf true, edited

            – gmds
            Mar 8 at 23:31







          1




          1





          @TemporalWolf true, edited

          – gmds
          Mar 8 at 23:31





          @TemporalWolf true, edited

          – gmds
          Mar 8 at 23:31



















          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%2f55072224%2ffor-loop-isnt-picking-up-the-if-else-statement%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

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved