为什么我的代码重复从"薪水"变量中抓取数据?



我正在尝试从确实抓取数据。变量:job_titlecompany工作良好,但salary在每行上一遍又一遍地返回相同的数据。有人能帮忙解决这个问题吗?Url保存在代码中供任何想要检查HTML的人使用。谢谢!

import requests
from bs4 import BeautifulSoup
url = "https://au.indeed.com/jobs?q=Software%20Engineer&l=Adelaide%20Region%20SA&radius=100&vjk=ca36e39d39db9210"
r = requests.get(url)
webpage = r.content
soup = BeautifulSoup(webpage, "html.parser")

job_card = soup.find_all('div', class_ = 'job_seen_beacon')
for item in job_card:
job_title = item.find('span').text.strip()
company = item.find('span', class_ = 'companyName').text.strip()

s = soup.find('div', class_ = 'metadata salary-snippet-container')
for span in s:
salary = span.find('span').text.strip()

print(job_title, company, salary)

的回报:波音软件工程师年薪8.5万到10万美元新哈德逊澳大利亚年收入8.5万至10万美元WiseTech Global软件工程师年薪8.5万- 10万美元软件工程师(集成)霍尼韦尔85,000 - 100,000美元年薪软件支持工程师:年薪8.5万- 10万美元软件工程师(执业)莱茵金属防务澳大利亚有限公司年薪8.5万- 10万美元new Paxus Australia Pty Ltd年薪8.5万至10万美元5级软件工程师年薪8.5万- 10万美元软件工程师(PC应用)Codan Limited年薪85,000 - 100,000美元霍尼韦尔全栈软件工程师年薪8.5万- 10万美元Topcon Positioning Systems软件测试工程师年薪85,000 - 100,000美元软件工程师工作人员- ATMS洛克希德马丁澳大利亚$85,000 - $100,000年薪阿德莱德大学:每年8.5万到10万美元新聘的Rapid Global年薪为8.5万至10万美元新的凯捷澳大利亚$85,000 - $100,000每年

这些容器没有提到工资,但有些是存在的,因此如果您从根s = soup.find中发现是不正确的。在这种情况下,您只需要指定salary_item,然后检查它是否为None,如果不是None,则写下文本。

import requests
from bs4 import BeautifulSoup
url = "https://au.indeed.com/jobs?q=Software%20Engineer&l=Adelaide%20Region%20SA&radius=100&vjk=ca36e39d39db9210"
r = requests.get(url)
webpage = r.content
soup = BeautifulSoup(webpage, "html.parser")

job_card = soup.find_all('div', {'class': 'job_seen_beacon'})
for item in job_card:
job_title = item.find('h2', {'class': 'jobTitle'}).text.strip()
company = item.find('span', {'class': 'companyName'}).text.strip()
salary_item = item.find('div', {'class':'metadata salary-snippet-container'})
salary = salary_item.text.strip() if salary_item is not None else ''
print(job_title, company, salary, sep=';')

结果:

Software Engineer;BOEING;
Software Engineer;WiseTech Global;
Software Engineer (Integrations);Honeywell;
Software Support Engineer;Fivecast;
Software Engineer (Practitioner);Rheinmetall Defence Australia Pty Ltd;
Software Engineer;Fivecast;
Software Engineer (PC Applications);Codan Limited;
newSoftware Engineer;Paxus Australia Pty Ltd;
Full Stack Software Engineer;Honeywell;
Software Engineer Staff - ATMS;Lockheed Martin Australia;
Software Test Engineer;Topcon Positioning Systems;
newSoftware Engineer - Data and Analytics;Capgemini Australia;
Software Engineer;QinetiQ;
Lead Software Engineer;The University of Adelaide;$116,557 - $124,698 a year
Senior Software Engineer;WiseTech Global;

最新更新