【问题标题】:Can't scrape few field using css selector and selector gadget无法使用 css 选择器和选择器小工具抓取少数字段
【发布时间】:2019-10-13 07:37:39
【问题描述】:

所以,我有一个从 class-central.com 抓取数据的项目。我使用选择器小工具来获取几个字段的 .css 标签,但是当我运行我的程序时,我得到的只是一个空字段。我无法识别我的代码中的错误,所以请帮忙! (PS:我是scrapy的新手,这是我的第一个项目。所以请确保您的回答对于像我这样的人来说是可以理解的)

打开以下链接查看字段:https://www.classcentral.com/subject/cs

我无法抓取以下字段:

  1. start_date:课程开始的日期。
  2. via:托管课程的网站(例如 Coursera)。
  3. 评分:此课程获得的星数和评论数。

    import scrapy
      from ..items import ClasscentralItem
      class ClassCentral(scrapy.Spider):
          name = 'spidy'
          start_urls = [
              'https://www.classcentral.com/subject/cs'
          ]
      def parse(self, response):
          items = ClasscentralItem()
          all_tr = response.css('.xlarge-up-width-9-16')
          courses = response.css('.number-of-courses .text--bold::text')
          for x in all_tr:
              sub = response.css('.medium-up-head-1::text').extract()
              course_name = x.css('.course-name .text--        bold::text').extract()
              course_devloper = x.css('.uni-name::text').extract()
              via = x.css('.hover-initiativelinks , #course-listing-tbody .text--italic::text').extract()
              duration = x.css('.icon-clock-charcoal::text').extract()
              start_date = x.css('#course-listing-tbody .medium-only-hidden::text').extract()
              rating =x.css('.review-rating::text').extract()
              items['subjectname'] = sub
              items['course_name'] = course_name
              items['course_devloper'] = course_devloper
              items['via'] = via
              items['duration'] = duration
              items['start_date'] = start_date
              items['rating'] = rating
              yield items
    

【问题讨论】:

  • 请定义“我不能”。有什么错误吗?你如何运行抓取过程?
  • @rok 先生,我只是问为什么编译器没有显示任何错误时很少有字段是空白的。

标签: python-3.x web-scraping scrapy


【解决方案1】:

实际上,您的 all_tr 只是所有 Course Name 列的列表(而不是所有表行)。这就是为什么你不能从x 得到start_date(它在另一列)。

def parse(self, response):

    items = {}
    all_tr = response.css('#course-listing-tbody tr')
    courses = response.css('.number-of-courses .text--bold::text')
    for x in all_tr:
        sub = response.css('.medium-up-head-1::text').extract()
        course_name = x.css('.course-name .text--bold::text').get()
        course_devloper = x.css('.uni-name::text').extract()
        via = x.css('.text--italic::text').get()
        duration = x.css('.icon-clock-charcoal::text').extract()
        start_date = x.css('.medium-only-hidden::text').get()
        rating =x.css('td:nth-child(4)').attrib['data-timestamp']
        items['subjectname'] = sub
        items['course_name'] = course_name
        items['course_devloper'] = course_devloper
        items['via'] = via
        items['duration'] = duration
        items['start_date'] = start_date
        items['rating'] = rating
        yield items

更新对于rating,我抓取第四列(评级)的data-timestamp 属性

如果您查看页面源代码,您会发现有些行没有课程详细信息(广告行)。这就是为什么你在 5 个结果后得到一个错误。要获取所有课程,您需要修改 all_tr 选择器:

all_tr = response.css('#course-listing-tbody tr[itemscope]')

【讨论】:

  • 嗨,你能解释一下你对标签“评级”做了什么吗?而且现在蜘蛛只给我5门课程的输出,而网页有10多门课程。你也能说明一下吗?
  • 您好,先生,感谢您的帮助。只有最后一个问题,[itemscope] 做什么?是否在页面的源代码中?
  • 另外,为什么你在某些地方使用 .get() 而在其他地方使用 .extract() 而没有使用其中任何一个进行评分?
  • itemscopetr 标记的 HTML 属性。我用它来过滤掉数据行。
  • 我也只使用了.get() 用于您询问的变量。 .get().extract_first() 相同。我没有碰过您的.extract(),但我确定您需要使用.get().extract_first() 来获取这些值。
猜你喜欢
  • 2017-10-27
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
相关资源
最近更新 更多