【问题标题】:Xpath or css selector attrbute value in scrapyscrapy中的Xpath或css选择器属性值
【发布时间】:2018-08-08 11:01:33
【问题描述】:

您好,我是 scrapy 的新手,我想从 html 元素中提取属性值。那么从 html 中提取该属性值的正确方法是什么?我想提取“data-next-url”属性

<div class="loading_more_jobs" data-type="loading_more_jobs" style="display:none;" data-next-url="https://www.ziprecruiter.com/candidate/search?search=restaurant&amp;page=2&amp;location=Atlanta%2C+Georgia"></div>

我正在使用那个 xpath,但它不工作

 response.xpath('//*[@class="loading_more_jobs"]/@data-next-url').extract()

【问题讨论】:

  • 在浏览器中禁用 JS,然后查看源代码中是否存在该元素,我确定它不存在,您的 Xpath 看起来还不错
  • 那么如果我想获得那个属性值该怎么办
  • 如果它不在源代码中,您必须研究该网站如何通过 AJAX 加载该数据,或者他们已经在页面上以 JSON 或其他形式提供该数据。
  • ziprecruiter.com/candidate/… 我想从该页面的“加载更多工作”中获取该信息

标签: python xpath web-scraping scrapy css-selectors


【解决方案1】:

如果您检查源 HTML,您会发现:

  <button class="load_more_jobs" data-type="load_more_jobs" data-next-url="">Load More Job Results</button>
  <div class="loading_more_jobs" data-type="loading_more_jobs" style="display:none;"></div>

但无论如何你都可以获得下一页 URL:

<div class="job_results" data-this-url="/candidate/search?search=restaurant&amp;location=Atlanta%2C+Georgia" data-next-url="/candidate/search?location=Atlanta%2C+Georgia&amp;page=2&amp;search=restaurant" data-type="job_results">

=>

response.xpath('//div[@class="job_results"]/@data-next-url').extract_first()

<link rel="next" href="https://www.ziprecruiter.com/candidate/search?location=Atlanta%2C+Georgia&amp;page=2&amp;search=restaurant">

=>

response.xpath('//link[@rel="next"]/@href').extract_first()

【讨论】:

    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多