【问题标题】:Python - Web scraping using HTML tagsPython - 使用 HTML 标签进行网页抓取
【发布时间】:2018-12-03 10:13:39
【问题描述】:

我正在尝试抓取网页以列出 URL 中发布的工作:https://careers.microsoft.com/us/en/search-results?rk=l-hyderabad

网页检查详情请参考图片Web inspect

通过网页检查观察到以下内容:

  1. 列出的每个工作都在一个带有 class="jobs-list-item" 的 HTML li 中。 Li 在 li 中的父 Div 中包含以下 html 标记和数据

    data-ph-at-job-title-text="软件工程师 II", data-ph-at-job-category-text="工程", data-ph-at-job-post-date-text="2018-03-19T16:33:00"。

  2. class="information" 的父 Div 中的第一个子 Div 具有带有 url 的 HTML href="https://careers.microsoft.com/us/en/job/406138/Software-Engineer-II"

  3. 父 Div 中具有 class="description au-target" 的第三个子 Div 具有简短的职位描述

我的要求是为每个工作提取以下信息

  1. 职位名称
  2. 工作类别
  3. 职位发布日期
  4. 职位发布时间
  5. 职位网址
  6. 职位简介

我已尝试使用 Python 代码来抓取网页,但无法提取所需的信息。 (请忽略下面代码中显示的缩进)

import requests
from bs4 import BeautifulSoup
def ms_jobs():
url = 'https://careers.microsoft.com/us/en/search-results?rk=l-hyderabad'
resp = requests.get(url)

if resp.status_code == 200:
print("Successfully opened the web page")
soup = BeautifulSoup(resp.text, 'html.parser')
print(soup)
else:
print("Error")

ms_jobs()

【问题讨论】:

  • 您需要使用任何浏览器模拟器,例如selenium 从该页面中提取所需的数据,因为它们是动态生成的。
  • 感谢 SIM 的建议。我对 Python 中的 Selenium 没有任何了解。能否请您指出一些我可以调整的示例工作解决方案。

标签: python-3.x web-scraping beautifulsoup urllib2


【解决方案1】:

如果您想通过请求执行此操作,则需要对站点进行逆向工程。在 Chrome 中打开开发工具,选择网络标签并填写表格。

这将向您展示网站如何加载数据。如果您在该站点中进行挖掘,您会看到它通过对此端点执行 POST 来获取数据:https://careers.microsoft.com/widgets。它还向您显示该站点使用的有效负载。该站点使用 cookie,因此您所要做的就是创建一个保存 cookie 的会话,获取一个并复制/粘贴有效负载。

通过这种方式,您将能够提取相同的 json 数据,以便 javascript 获取以动态填充网站。

下面是一个工作示例,说明了它的外观。 Left 只是为了解析出你认为合适的 json。

import requests
from pprint import pprint

# create a session to grab a cookie from the site
session = requests.Session()
r = session.get("https://careers.microsoft.com/us/en/")

# these params are the ones that the dev tools show that site sets when using the website form
payload = {
    "lang":"en_us",
    "deviceType":"desktop",
    "country":"us",
    "ddoKey":"refineSearch",
    "sortBy":"",
    "subsearch":"",
    "from":0,
    "jobs":"true",
    "counts":"true",
    "all_fields":["country","state","city","category","employmentType","requisitionRoleType","educationLevel"],
    "pageName":"search-results",
    "size":20,
    "keywords":"",
    "global":"true",
    "selected_fields":{"city":["Hyderabad"],"country":["India"]},
    "sort":"null",
    "locationData":{}
}

# this is the endpoint the site uses to fetch json
url = "https://careers.microsoft.com/widgets"
r = session.post(url, json=payload)
data = r.json()
job_list = data['refineSearch']['data']['jobs']

# the job_list will hold 20 jobs (you can se the parameter in the payload to a higher number if you please - I tested 100, that returned 100 jobs
job = job_list[0]
pprint(job)

干杯。

【讨论】:

  • 谢谢jlaur。您的解决方案效果很好。我很好奇您对想要使用“请求”的评论。如果可用,我想使用更简单的选项。由于我对 chrome 开发人员工具的了解有限,对网站进行逆向工程似乎是一个复杂的方面。有没有更简单的解决方案?我看到@SIM 建议使用硒。
  • 不客气。你问如何处理请求。这就是你这样做的方式。我会使用请求做这样的工作。如果您想探索如何使用例如 selenium 来做到这一点,请关闭此问题并询问有关该主题的新问题。请记住,使用这样的解决方案会慢很多,所以如果您要进行严重的抓取,恕我直言,这不是解决方法......
  • 很高兴知道。 “请求”是前进的方向。谢谢小费。会记住的。也接受了你的回答
猜你喜欢
  • 2019-12-04
  • 2020-07-16
  • 2020-08-21
  • 2011-10-21
  • 1970-01-01
  • 2020-10-04
  • 2021-05-08
  • 2018-07-20
相关资源
最近更新 更多