网页抓取问题 求职网站

时间:2021-01-21 00:56:43

标签: python web-scraping

我正在使用针对“软件开发人员”的搜索在网络上抓取 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()

1 个答案:

答案 0 :(得分:1)

您的问题是您有两个不相关的分隔列表 searchpython_jobs。后来你甚至不使用列表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()