使用scrapy爬虫,爬取17k小说网的案例-方法一

无意间看到17小说网里面有一些小说小故事,于是决定用爬虫爬取下来自己看着玩,下图这个页面就是要爬取的来源。

a

这个页面一共有125个标题,每个标题里面对应一个内容,如下图所示

 

下面直接看最核心spiders中的代码

# -*- coding: utf-8 -*-
import scrapy
from k17.items import K17Item
import json
class A17kSpider(scrapy.Spider):
    name = '17k'
    allowed_domains = ['17k.com']
    start_urls = ['http://www.17k.com/list/271047.html']
    def parse(self, response):
        old_url='http://www.17k.com'
            
for bb in response.xpath('//div[@class="Main List"]/dl[@class="Volume"]/dd'): ##把xpath表达式作为normalize-space()函数的参数 此方法可以去除数据的值有\r\n\t link=bb.xpath("a/@href").extract()
for newurl in link: new_url=old_url+newurl yield scrapy.Request(new_url, callback=self.parse_item) def parse_item(self,response): for aa in response.xpath('//div[@class="readArea"]/div[@class="readAreaBox content"]'): item=K17Item() title=aa.xpath("h1/text()").extract()###得到每一章的标题 new_title=(''.join(title).replace('\n','')).strip() item['title']=new_title dec= aa.xpath("div[@class='p']/text()").extract()###得到每一章的详细内容 dec_new=((''.join(dec).replace('\n','')).replace('\u3000','')).strip() ###去除内容中的\n 和\u3000和空格的问题 item['describe'] = dec_new yield item
 
  

  

 

 




转载于:https://www.cnblogs.com/stevenshushu/p/9212854.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值