Я изо всех сил пытаюсь распечатать 10 строк / выводов в этой функции. Пока у меня есть только два выхода. Либо цикл for не читает строку за строкой, либо оператор if-else содержит ошибки.
# 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)
В настоящее время выходы:
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']
идеальный результат должен быть:
['$115,000 a year', 'Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found','Nothing_found']