Ejemplo n.º 1
0
 def parse(self, response):
     item = CourseItem()
     # xpath
     for box in response.xpath(
             '//div[@class="moco-course-wrap"]/a[@target="_self"]'):
         #课程路径
         item['url'] = 'http://www.imooc.com' + box.xpath(
             './/@href').extract()[0]
         #课程标题
         item['title'] = box.xpath('.//img/@alt').extract()[0].strip()
         #标题图片地址
         item['image_url'] = box.xpath('.//@src').extract()[0]
         #学生人数
         item['student'] = box.xpath(
             './/span/text()').extract()[0].strip()[:-3]
         #课程简介
         item['introduction'] = box.xpath(
             './/p/text()').extract()[0].strip()
         #返回信息
         yield item
     #url跟进开始
     #获取下一页的url信息
     url = response.xpath("//a[contains(text(),'下一页')]/@href").extract()
     if url:
         #将信息组合成下一页的url
         page = 'http://www.imooc.com' + url[0]
         #返回url
         yield scrapy.Request(page, callback=self.parse)
Ejemplo n.º 2
0
    def parse(self, response):
        #实例一个容器保存爬取的信息
        item = CourseItem()

        #先获取每个课程的div
        for box in response.xpath(
                '//div[@class="course-card-container"]/a[@target="_blank"]'):
            #获取每个div中的课程路径
            item['url'] = 'http://www.imooc.com' + box.xpath(
                './/@href').extract()[0]
            # 获取div中的课程标题
            item['title'] = box.xpath('.//h3/text()').extract()[0].strip()
            #获取div中的标题图片地址
            item['image_url'] = "http:" + box.xpath('.//@src').extract()[0]
            #获取学生数量信息
            item['student'] = box.xpath('.//span/text()').extract()[0].strip()
            #获取div中的课程简介
            item['introduction'] = box.xpath(
                './/p/text()').extract()[0].strip()
            #返回信息
            yield item

        #url跟进开始
        #获取下一页的url信息
        url = response.xpath("//a[contains(text(),'下一页')]/@href").extract()
        if url:
            #将信息组合成下一页的url
            page = 'http://www.imooc.com' + url[0]
            #返回url
            yield scrapy.Request(page, callback=self.parse)
Ejemplo n.º 3
0
    def parse(self, response):
        item = CourseItem()
        #print("****",response.body)
        for box in response.xpath('.//div[@class="course-card-container"]'):
            item['url'] = "http://www.imooc.com" + box.xpath(
                './/@href').extract()[0]
            item['title'] = box.xpath('.//h3/text()').extract()[0]
            item['image_url'] = box.xpath('.//@src').extract()[0]
            item['student'] = box.xpath(
                './/div[@class="course-card-info"]/span[2]/text()').extract(
                )[0]
            item['introduction'] = box.xpath(
                './/p/text()').extract()[0].strip()
            if len(
                    box.xpath('.//div[@class="course-label"]/label/text()').
                    extract()) == 1:

                item['catycray'] = box.xpath(
                    './/div[@class="course-label"]/label/text()').extract()[0]

            elif len(
                    box.xpath('.//div[@class="course-label"]/label/text()').
                    extract()) == 2:
                item['catycray'] = box.xpath(
                    './/div[@class="course-label"]/label/text()'
                ).extract()[0] + ' ' + box.xpath(
                    './/div[@class="course-label"]/label/text()').extract()[1]
            elif len(
                    box.xpath('.//div[@class="course-label"]/label/text()').
                    extract()) == 3:
                item['catycray'] = box.xpath(
                    './/div[@class="course-label"]/label/text()'
                ).extract()[0] + ' ' + box.xpath(
                    './/div[@class="course-label"]/label/text()'
                ).extract()[1] + ' ' + box.xpath(
                    './/div[@class="course-label"]/label/text()').extract()[2]
            else:
                item['catycray'] = ''
            #下载图片
            #urllib.urlretrieve(item['image_url'],'pic/'+item['title']+'.jpg')
            #返回信息
            yield scrapy.Request(item['url'],
                                 callback=self.parseNest,
                                 meta=item)
        for x in range(2, 3):
            page = 'http://www.imooc.com/course/list?page=' + str(x)
            yield scrapy.Request(page, callback=self.parse)
Ejemplo n.º 4
0
 def parse(self, response):
     # 实例一个容器保存爬取的信息
     item = CourseItem()
     # 这部分是爬取部分,使用xpath的方式选择信息,具体方法根据网页结构而定
     # 先获取每个课程的div
     for box in response.xpath('//div[@class="moco-course-wrap"]/a[@target="_self"]'):
         # 获取每个div中的课程路径
         item['url'] = 'http://www.imooc.com' + box.xpath('.//@href').extract()[0]
         # 获取div中的课程标题
         item['title'] = box.xpath('.//img/@alt').extract()[0].strip()
         # 获取div中的标题图片地址
         item['image_url'] = box.xpath('.//@src').extract()[0]
         # 获取div中的学生人数
         item['student'] = box.xpath('.//span/text()').extract()[0].strip()[:-3]
         # 获取div中的课程简介
         item['introduction'] = box.xpath('.//p/text()').extract()[0].strip()
         # 返回信息
         yield item
Ejemplo n.º 5
0
 def parse(self, response):
     #实例一个容器保存爬取的信息
     item = CourseItem()
     #这部分是爬取部分,使用xpath的方式选择信息,具体方法根据网页结构而定
     #直接爬取弹幕内容
     arr = [];
     str0 = ''
     for box in response.xpath('/i/d/text()'):
         #获取div中的课程标题
         #item['content'] = box.extract();
         str0 += box.extract()+',';
        # arr.append(box.extract())
         #返回信息
         #print(str0)
         #item
         
         
     item['content'] = str0;
     yield item
Ejemplo n.º 6
0
    def parse(self, response):  #默认回调给parse函数
        #实例一个容器保存爬取的信息
        item = CourseItem()
        #这部分是爬取部分,使用xpath的方式选择信息,具体方法根据网页结构而定
        #先获取每个课程的div

        #       for box in response.xpath('//div[@class="moco-course-wrap"]/a[@target="_self"]'):
        for box in response.xpath(
                '//div[@class="course-card-container"]/a[@target="_blank"]'):
            #获取每个div中的课程路径
            item['url'] = 'http://www.imooc.com' + box.xpath(
                './/@href').extract()[
                    0]  #selector 用extract()提取里面的内容,前面的 . 表示在box标签下找
            #获取div中的课程标题
            item['title'] = box.xpath(
                './/div[@class="course-card-content"]/h3/text()').extract(
                )[0].strip()  #提取h3标签里面的内容
            #获取div中的标题图片地址
            item['image_url'] = "http:" + box.xpath(
                './/img[@class="course-banner lazy"]/@src').extract()[
                    0]  #寻找所有含有属性class="course-banner lazy"的img标签,提取里面的src属性
            print(item['image_url'])
            #获取div中的学生人数
            item['student'] = box.xpath('.//span/text()').extract()[1]
            #获取div中的课程简介
            item['introduction'] = box.xpath(
                './/p/text()').extract()[0].strip()
            #返回信息
            yield item  #如果是request则加入爬取队列,如果是item类型则使用pipeline处理,其他类型则返回错误信息

        #url跟进开始
        #获取下一页的url信息
        url = response.xpath(
            "//a[contains(text(),'下一页')]/@href").extract()  # contains用法
        if url:
            #将信息组合成下一页的url
            page = 'http://www.imooc.com' + url[0]
            #返回url
            yield scrapy.Request(
                page, callback=self.parse)  #每抓取到一个网页,调用callback指向的方法
Ejemplo n.º 7
0
    def parse(self, response):
        item = CourseItem()
        list = response.xpath(
            '//ul[@class="thumbItem large clearfix"]//li//img')
        # print(list)
        for box in list:
            a = 3
            # filename = response.url.split("/")[-2]
            # with open(filename, 'wb') as f:
            #     f.write(response.body)
            # 获取每个div中的课程路径
            src = "http://www.cssmoban.com" + box.xpath('./@src').extract()[0]
            item['url'] = src
            print(src)
            if src:
                absoluteSrc = src
                file_name = src.split("/")[-1]
                file_path = os.path.join("F:\\pics", file_name)
                urllib.request.urlretrieve(absoluteSrc, file_path)

        nexturl = "http://www.cssmoban.com/tags.asp" + box.xpath(
            '//a[@class="num"]/@href').extract()[0]
        print(nexturl)
        yield Request(nexturl, callback=self.parse)