【问题标题】:Trying to extract data using python/scrapy and not able to find the correct xpath尝试使用 python/scrapy 提取数据但无法找到正确的 xpath
【发布时间】:2019-04-22 04:08:36
【问题描述】:

我想抓取网站。

https://stackoverflow.com/jobs?med=site-ui&ref=jobs-tab

我要提取

  • 标题
  • 位置
  • 公司

招聘信息。

我尝试了几个 xpath 的位置、公司和标题,但没有任何效果。我还尝试将其写入 CSV 文件。所有位置、公司和标题都显示为空白。我认为我的 xpath 不正确

import scrapy


class JobItem(scrapy.Item):
    # Data structure to store the title, company name and location of the job
    title = scrapy.Field()
    company = scrapy.Field()
    location = scrapy.Field()

class stackoverflow(scrapy.Spider):
    name = 'stack_bot'
    start_urls = ['https://stackoverflow.com/jobs?med=site-ui&ref=jobs-tab']

    def parse(self, response):
        for a_el in response.xpath('//div[@class="listResults"]'):
            section = JobItem()
            section['title']   = ?
            section['company'] = ?
            section['location'] = ?
            yield section

谁能帮助我获取标题、公司和位置的 xpath。 xpath('//div[@class="listResults"]') 也是正确的。

【问题讨论】:

  • 添加到答案中。请查看here 以了解有关抓取 SO 作业的一般讨论。

标签: python xpath web-scraping scrapy


【解决方案1】:

我不确定xpath('//div[@class="listResults"]') 是否正确。它只给出一个元素。这是我的代码版本:

def parse(self, response):
    for a_el in response.xpath('//div[contains(@class, "-job-summary")]'):
        section = JobItem()
        section['title']   = a_el.css('h2 a::text').get()
        section['company'] = a_el.xpath('.//div[contains(@class, "-company")]/span[1]/text()').get()
        section['location'] = a_el.xpath('.//div[contains(@class, "-company")]/span[2]/text()').get()
        yield section

【讨论】:

    【解决方案2】:

    考虑使用 RSS 提要作为源,因为随着时间的推移这将更加强大

    https://stackoverflow.com/jobs/feed
    

    然后你可以使用下面的css选择器来生成可以一起列出(zip())的列表

    标题选择器:item title

    公司选择器:a10\:author

    地点:location

    【讨论】:

    • 感谢您的快速回复,非常感谢。我尝试了第一个,它奏效了。再次感谢:-)
    猜你喜欢
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多