【问题标题】:Scrapy get text spanning multiple lines and within nested elementsScrapy 获取跨越多行和嵌套元素内的文本
【发布时间】:2019-05-16 08:14:18
【问题描述】:

我确实正在尝试抓取以获取班加罗尔所有工作列表的信息。

网址:https://www.indeed.co.in/jobs?q=software+developer&l=Bengaluru,+Karnataka&start=0

我感兴趣的父 div 的 Xpath:

//div[contains(@class, "jobsearch-SerpJobCard")]

我想提取结构如下的公司名称:

<span class="company">
        <a>
              Micro Focus
        </a>
</span>

还有一些类似的:

<div>
    <span class="company">
        SSG <b>Software</b> Systems</span>

    </div>

我正在使用一个常见的 Xpath 表达式来抓取这两种标题。我在使用第二种类型时遇到了问题,因为它包含多个转义字符,例如 \n,这些字符反映在我的结果中,并且在剥离结果时显示为空字符串。

用于提取标题的 Xpath:

//div[包含(@class, "jobsearch-SerpJobCard")]//span[@class="company"]/text()

结果:

['\n ', '\n ', '\n ', '\n 客户端 分析人力资本', '\n Advantage Tech', '\n ', '\n SQUARE', '\n DART', '\n posmab 技术', '\n ', '\n PENTAMOUNT TECHNOLOGIES', '\n ', '\n
MobileComm, Inc.', '\n IGLOBAL IMPACT ITES PVT.LTD.', '\n
', '\n ']

我能做些什么来摆脱那些多余的 '\n' 字符?

【问题讨论】:

  • 必须和 Scrapy 一起使用吗?您是否期待 more... 隐藏的文本?
  • 是的,我正在为客户分配任务,他需要通过scrapy完成。

标签: python web-scraping scrapy


【解决方案1】:

您可以使用normalize-space XPath 函数来实现这一点。

>>> fetch('https://www.indeed.co.in/jobs?q=software+developer&l=Bengaluru,+Karnataka&start=0')
2018-12-15 09:47:22 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.indeed.co.in/jobs?q=software+developer&l=Bengaluru,+Karnataka&start=0> (referer: None)
>>> response.xpath('//div[contains(@class, "jobsearch-SerpJobCard")]//span[@class="company"]').xpath('normalize-space()').getall()
['Amazon.com', 'Sabre', 'Altisource Labs', 'CGI', 'Allscripts Solutions', 'Shilpin Consulting', 'Access6 technology', 'CGI Group, Inc.', 'Misys Software Solutions India', 'Siemens AG']

【讨论】:

  • 这将删除所有不必要的空格,\n\t 如果它们在那里,我认为这是推荐的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多