深色proxyscrape 徽标

Web Scraping For Job Postings – An Ultimate 2024 Guide

蟒蛇, 搜索, Jan-04-20235 分钟阅读
对招聘信息进行网络抓取是求职者建立当前职位空缺数据库的另一条捷径。Randstad 指出,从提出申请到职位被录用,平均求职时间可能长达5 到 6 个月。如果有一种解决方案可以减轻您的负担,让您在所有的求职门户网站中寻找适合自己的职位,那会怎么样呢? 

这篇文章将带你了解如何进行网络招聘信息搜索。因此,你正在求职市场上努力寻找最好的工作。但你想玩得更聪明,而不是更辛苦。何不创建一个网络搜索器,为你收集和解析招聘信息。一旦设置好,它就会以整洁的格式为你提供丰富的数据,这样你就不必反复手动检查了。让我们开始吧

什么是招聘信息的网络抓取?

网络招聘信息搜索是一种自动从多个招聘门户网站收集数据的解决方案,可减少您从每个网站获取数据的时间。有了这样一个能为你提供完整的职位空缺数据库的工具,你的任务就会简化许多倍。您所要做的就是筛选出适合您的职位,然后进行申请。 

因此,您正在求职市场上努力寻找最好的工作。但是,你想玩得更聪明,而不是更辛苦。为什么不创建一个网络搜索器来为你收集和解析招聘信息呢?一旦设置好,它就会以整洁的格式为你提供丰富的数据,这样你就不必反复手动检查了。让我们开始吧

[免责声明!许多网站都会限制从其网页中提取数据。用户可能会遇到法律问题,这取决于他们试图提取信息的地点和方式。因此,用户在查看存放其数据的网站时需要格外小心。例如,Facebook、Linked In 和 Craiglist 有时会介意从其页面上搜刮数据。因此,如果你想搜刮,请自担风险]。

这将是一篇非常基础的文章,我们将通过从 indeed.com 中提取与 "数据科学 "相关的工作岗位的一些有用信息,了解网络搜索的基础知识。我们将编写一个神奇的程序,多次手动更新职位信息。在构建该搜索器时,"requests "和 "BeautifulSoup "是一些非常有用的库。

了解 URL 和页面结构

首先,让我们来看看我们要提取样本页面。 

URL 的结构非常重要:

  • 注意 "q="是页面上 "什么 "字段的开头字符串,搜索词之间用 "+"分隔(例如,搜索 "数据+科学家 "职位)。
  • 在指定薪金时,它将通过薪金数字中的逗号进行解析,因此薪金开头将以 %24 开头,然后是第一个逗号前的数字,接着以 %2C 断开,然后继续后面的数字(例如,%2420%2C000 = $20,000)。
  • 注意"&l="开头的字符串表示感兴趣的城市,如果城市不只一个词,则用 "+"分隔搜索词(例如,"New+York")。
  • 注"&start="表示您想从哪个搜索结果开始(例如,从第 10 个搜索结果开始)

当我们继续构建刮板并从多个页面收集数据时,这种 URL 结构将大有帮助。 

Chrome 浏览器可以通过右键单击页面并使用检查元素选项来检查页面的 HTML 结构。右侧会出现一个菜单,它还会显示嵌套元素标签,当你把光标放在这些元素上时,屏幕上的这一部分就会高亮显示。  

在本文中,我假设你了解 HTML 的基础知识,如标签、div 等,但幸运的是,你并不需要了解一切。你只需了解页面结构和不同组件的层次结构。

开始使用刮板

现在我们已经分析了页面结构。这将有助于我们根据这些信息编写代码,以提取我们所选择的数据。首先,让我们导入我们的库。请注意,在这里我们还导入了 "时间",这将有助于在抓取信息时避免网站服务器不堪重负。

导入请求
导入bs4
bs4导入BeautifulSoup
importpandasaspd
导入时间

我们将首先针对单个页面提取我们想要的每一条信息、

URL ="https://www.indeed.com/jobs?q=data+scientist+%2420%2C000&l=New+York&start=10"
# 对上述 URL 进行请求:
page = requests.get(URL)
#使用 html 解析器指定所需的 "页面 "格式--这样,python 就能读取页面的各个组成部分,而不是将其视为一个长字符串。
soup = BeautifulSoup(page.text,"html.parser")
#以结构更合理的树形格式打印汤,以便于阅读
print(soup.prettify())

使用 prettify 可以更轻松地概览页面的 HTML 编码,并提供类似这样的输出、

 现在,我们感兴趣的页面上的所有信息都在我们的变量 "汤 "中。我们必须进一步挖掘代码,遍历各种标签和子标签,以获取所需的信息。

获取数据的基本要素

每份招聘启事的五个要点是

  1. 职位名称
  2. 公司名称:
  3. 地点。
  4. 工资。
  5. 工作摘要

如果我们看一下页面,有 15 个招聘信息。因此,我们的代码也应该生成 15 个不同的项目。但是,如果代码提供的信息少于这个数量,我们就可以返回页面查看哪些信息没有被捕获。

获取职位名称

As can be seen, the entirety of each job posting is under <div> tags, with an attribute “class” = “row result.”

Further, we could also see that job titles are under <a> tags, with the attribute “title = (title)”. One can see the value of the tag’s attribute with tag[“attribute”], so I can use it to find each posting’s job title.

概括起来,我们将要看到的功能包括以下三个步骤、

  1. Pulling out all the <div> tags with class including “row”.
  2. Identifying <a> tags with attribute “data-tn-element”:”jobTitle”
  3. For each of these <a> tags, find attribute values “title”
def extract_job_title_from_result(soup): 
  jobs = []
  for div in soup.find_all(name="div", attrs={"class":"row"}):
    for a in div.find_all(name="a", attrs={"data-tn-element":"jobTitle"}):
      jobs.append(a["title"])
  return(jobs)
extract_job_title_from_result(soup)

这段代码的输出结果如下

获取公司名称

Getting company names can be a bit tricky because most of them are appearing in <span> tags, with “class”:” company”.  They are also housed in <span> tags with “class”:” result-link-source”.

我们将使用 if/else 语句从这些地方提取公司信息。为了在输出时删除公司名称周围的空白,我们将在最后使用 inputting.strip()。

def extract_company_from_result(soup): 
 companies = []
 for div in soup.find_all(name="div", attrs={"class":"row"}):
   company = div.find_all(name="span", attrs={"class":"company"})
   if len(company) &gt; 0:
    for b in company:
     companies.append(b.text.strip())
   else:
    sec_try = div.find_all(name="span", attrs={"class":"result-link-source"})
    for span in sec_try:
      companies.append(span.text.strip())
 return(companies)
 
extract_company_from_result(soup)

获取位置

Locations are located under the <span> tags. Span tags are sometimes nested within each other, such that the location text may sometimes be within “class”:”location” attributes, or nested in “itemprop”:”addressLocality”. However a simple for loop can examine all span tags for text and retrieve the necessary information.

def extract_location_from_result(soup): 
  locations = []
  spans = soup.findAll('span', attrs={'class': 'location'})
  for span in spans:
    locations.append(span.text)
  return(locations)
extract_location_from_result(soup)

获取工资

从招聘信息中提取薪资是最具挑战性的部分。大多数招聘信息根本不发布薪资信息,而其他发布薪资信息的招聘信息可能有多个地方可以提取薪资信息。因此,我们必须编写一段代码,可以从多个地方提取多个薪资信息,如果没有找到薪资信息,我们需要为不包含薪资信息的职位创建一个占位符 "Nothing Found "值。 

Some salaries are under <nobr> tags, while others are under <div> tags, “class”:”sjcl” and are under separate div tags with no attributes. Try/except statement can be helpful while extracting this information. 

def extract_salary_from_result(soup): 
  salaries = []
  for div in soup.find_all(name="div", attrs={"class":"row"}):
    try:
      salaries.append(div.find('nobr').text)
    except:
      try:
        div_two = div.find(name="div", attrs={"class":"sjcl"})
        div_three = div_two.find("div")
        salaries.append(div_three.text.strip())
      except:
        salaries.append("Nothing_found")
  return(salaries)
extract_salary_from_result(soup)

获取工作摘要

最后一项工作是获取职位摘要。但是,我们无法获取每个特定职位的职位摘要,因为特定 Indeed 页面的 HTML 中并不包含这些摘要。我们可以从提供的信息中获取每个职位的一些信息。为此,我们可以使用 Selenium。

But let’s first try this using python. Summaries are located under <span> tags. Span tags are nested within each other such that the location text is within “class”:” location” tags or nested in “itemprop”:” adressLocality”. However, using a simple for loop can examine all span tags for text to retrieve the necessary information.

常见问题

1. Why is it necessary to scrape job details?
有足够多的招聘门户网站可以通知您与您的个人资料相匹配的工作。不过,使用职位搜索解决方案可以将完整的职位列表和描述作为一个方便的数据库。这样,您就可以在一个地方更好地了解所有工作机会。
2. How can a proxy help in scraping job details?
网络搜索通常会遇到一些挑战,如 IP 屏蔽、地理屏蔽和速度限制。从招聘门户网站搜索工作也会遇到此类挑战。要绕过所有这些问题,代理地址可以帮助您获得所需地点的 IP 地址,并确保匿名性。
3. What are the python libraries required to scrape job details?
Requests, BeautifulSoup, and Pandas are quite common python libraries to scrape job-related data from websites. Request library is the most common python library used to send web requests, while BeautifulSoup is responsible for parsing data, and Pandas library will help with the data structure operations.
相关文章

总结

在本文中,我们通过一个从 Indeed 网页中抓取工作数据的实际例子,了解了什么是网络抓取,以及它如何在我们的日常生活中发挥作用。请注意,由于网页是动态的,因此信息会随着时间的推移而不断变化,您得到的结果可能与这些不同。 

如果按照您的要求正确操作,网络抓取是一种令人难以置信的技术。我们进一步了解了每个招聘信息的五个重要方面,以及如何提取它们。当你自己尝试这段代码时,你就会获得招聘信息的数据,而不需要手动搜索招聘信息了,这真是太神奇了。同样的技术也可以应用于其他网页,但它们的结构可能有所不同。因此,需要根据实际情况优化代码。但本文已涵盖了所有基础知识,因此在搜索其他网页时也不会有任何困难。

如果您正在寻找代理服务,请不要忘记查看 ProxyScrape住宅代理和高级代理