首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >web抓取求职网站的问题

web抓取求职网站的问题
EN

Stack Overflow用户
提问于 2021-01-21 08:56:44
回答 1查看 113关注 0票数 0

我在网上搜索Monster的工作网站,搜索目标是“软件开发人员”,我的目标是简单地打印出那些在Python终端的描述中列出了"python“的作业,而放弃所有其他的Java,HTML,CSS等作业。然而,当我运行这段代码时,我最终打印出了页面上的所有作业。为了解决这个问题,我创建了一个变量(名为'search'),它使用'python‘搜索所有作业,并将其转换为小写。我还创建了一个变量(名为'python_jobs'),它包含页面上的所有工作列表。

然后,我创建了一个" for“循环,用于查找在”python_jobs“中找到”search“的每个实例。但是,这将产生与之前相同的结果,并打印出页面上的每个工作列表。有什么建议吗?

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-21 09:47:21

你的问题是你有两个不相关的列表searchpython_jobs。后来,您甚至不使用list search。您应该从python_jobs获取每一项,并在此项中搜索python

代码语言:javascript
复制
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()

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65819766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档