开始使用免费开始使用

综合爬虫练习

这个练习让您展示所学内容!您将为一个 spider 编写 parse 函数,然后补全几个空白以完成这个 spider。在 DataCamp 的课程目录页,每门课程都有一个标题和简短的课程描述。该 spider 将抓取课程目录,提取课程标题和简短描述。本次不需要跟随任何链接。您只需要了解以下信息:

  • 课程标题位于 h4 元素中,其 class 属性包含字符串 block__title(双下划线)。
  • 简短课程描述位于段落 p 元素中,其 class 属性包含字符串 block__description(双下划线)。

本练习是课程的一部分

Python Web 爬取

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# parse method
def parse(self, response):
  # Extracted course titles
  crs_titles = response.xpath(____).extract()
  # Extracted course descriptions
  crs_descrs = response.xpath(____).extract()
  # Fill in the dictionary: it is the spider output
  for crs_title, crs_descr in zip(crs_titles, crs_descrs):
    dc_dict[crs_title] = crs_descr
编辑并运行代码