【问题标题】:Javascript - Document Query SelectorJavascript - 文档查询选择器
【发布时间】:2021-12-21 22:11:51
【问题描述】:

我正在尝试选择职位发布标题和职位发布的 href。然后返回响应。 你能告诉我我在这里做错了什么吗?

网址:https://epyz.fa.us2.oraclecloud.com/hcmUI/CandidateExperience/en/sites/CX_1/requisitions?location=Jacksonville,%20FL,%20United%20States&locationId=300000002870977&locationLevel=city&mode=location&radius=25&radiusUnit=MI

这是我最后一次尝试:

  function ExecuteScript() {
    let response = '';
    document.querySelectorAll('h2[data-bind="text: job.title"]').forEach((element, i) => {
      response += element.innerHTML + '\\t' + location.host + element.getAttribute('href') + '\\n';
    });
    return response;
  }

【问题讨论】:

    标签: javascript web-scraping jquery-selectors


    【解决方案1】:

    首先,检查页面结构的样子:每个列表元素(职位发布)都是列表项 (li),其中包含完整大小的链接 (a)。每个项目还包含代表帖子标题的h2标题:

    li -> a
       -> div -> h2
    

    所以要同时获取标题和链接,您可以遍历列表项并搜索目标子元素:

    Array.from(document.querySelectorAll('li.job-tile')).map(element => ({
     title: element.querySelector('h2.job-tile__title').innerText,
     link: element.querySelector('a.job-tile__show').href
    }))
    

    在哪里

    li.job-tile        - list item, post tile actually
    h2.job-tile__title - header, post title
    a.job-tile__show   - link, post page URL
    

    这个 sn-p 将返回带有标题和链接的对象数组,如下所示:

    [
      {
        title: 'Application Engineer (Custom Solutions)', 
        link: 'https://epyz.fa.us2.oraclecloud.com/hcmUI/Candidat…nLevel=city&mode=location&radius=25&radiusUnit=MI'
      },
      {
         title: 'Intern - Custom Solutions (Information Systems)', 
         link: 'https://epyz.fa.us2.oraclecloud.com/hcmUI/Candidat…nLevel=city&mode=location&radius=25&radiusUnit=MI'
      },
      ...
    ]
    

    还应注意,您提供的提要包含分页(作为无限滚动),因此像这样的抓取方法将解析仅可见帖子(已加载),而不是所有帖子。

    【讨论】:

    • 我正在尝试返回响应,因为我将其作为变量存储在 Microsoft Power Automate Desktop 中。是否可以使用我上面使用的格式?我尝试使用你的,它为我返回了一个空结果。希望这是有道理的?
    猜你喜欢
    • 2017-11-18
    • 2020-12-17
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多