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;
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
add a comment |
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
add a comment |
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
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
python for-loop if-statement web-scraping
asked Mar 8 at 23:04
James KongJames Kong
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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).
1
@TemporalWolf true, edited
– gmds
Mar 8 at 23:31
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%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
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).
1
@TemporalWolf true, edited
– gmds
Mar 8 at 23:31
add a comment |
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).
1
@TemporalWolf true, edited
– gmds
Mar 8 at 23:31
add a comment |
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).
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).
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
add a comment |
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
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%2f55072224%2ffor-loop-isnt-picking-up-the-if-else-statement%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