我在网上搜索Monster的工作网站,搜索目标是“软件开发人员”,我的目标是简单地打印出那些在Python终端的描述中列出了"python“的作业,而放弃所有其他的Java,HTML,CSS等作业。然而,当我运行这段代码时,我最终打印出了页面上的所有作业。为了解决这个问题,我创建了一个变量(名为'search'),它使用'python‘搜索所有作业,并将其转换为小写。我还创建了一个变量(名为'python_jobs'),它包含页面上的所有工作列表。
然后,我创建了一个" for“循环,用于查找在”python_jobs“中找到”search“的每个实例。但是,这将产生与之前相同的结果,并打印出页面上的每个工作列表。有什么建议吗?
import requests
from bs4 import BeautifulSoup
URL = "https://www.monster.com/jobs/search/?q=Software-Developer"
page = requests.get(URL)
print(page)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(id="ResultsContainer")
search = results.find_all("h2", string=lambda text: "python" in text.lower())
python_jobs = results.find_all("section", class_="card-content")
print(len(search))
for search in python_jobs:
title = search.find("h2", class_="title")
company = search.find("div", class_="company")
if None in (title, company):
continue
print(title.text.strip())
print(company.text.strip())
print()发布于 2021-01-21 09:47:21
你的问题是你有两个不相关的列表search和python_jobs。后来,您甚至不使用list search。您应该从python_jobs获取每一项,并在此项中搜索python。
import requests
from bs4 import BeautifulSoup
URL = "https://www.monster.com/jobs/search/?q=Software-Developer"
page = requests.get(URL)
print(page)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(id="ResultsContainer")
all_jobs = results.find_all("section", class_="card-content")
for job in all_jobs:
python = job.find("h2", string=lambda text: "python" in text.lower())
if python:
title = job.find("h2", class_="title")
company = job.find("div", class_="company")
print(title.text.strip())
print(company.text.strip())
print()或
import requests
from bs4 import BeautifulSoup
URL = "https://www.monster.com/jobs/search/?q=Software-Developer"
page = requests.get(URL)
print(page)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(id="ResultsContainer")
all_jobs = results.find_all("section", class_="card-content")
for job in all_jobs:
title = job.find("h2")
if title:
title = title.text.strip()
if 'python' in title.lower():
company = job.find("div", class_="company").text.strip()
print(title)
print(company)
print()https://stackoverflow.com/questions/65819766
复制相似问题