抓aiyadu的电子书链接代码,调试后版本

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy import Item,Field
from ..items import BookItem
class BooksSpider(scrapy.Spider):
    name = 'books'
    allowed_domains = ['aiyadu.com']
    # start_urls = ['http://aiyadu.com/']


 #当前页数
    current_page = 1
 
    #设置用户代理为浏览器类型
    aiyadu_headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36"}
 
    def start_requests(self):
        url = "http://www.aiyadu.com/page/1"
        yield scrapy.Request(url, headers=self.aiyadu_headers, callback=self.parse)

    
    # 书籍列表页面解析
    def parse(self, response):
        #每本书的链接:
        #使用xpath定位到小说内容的div元素
        list_selector =  response.xpath("//div[@class='excerpts']//h2/a/@href")
        #读取每部小说的元素
        for one_selector in list_selector:
            #获取链接,链接为绝对地址,如http://www.aiyadu.com/14777.html
            urltemp1 = one_selector.extract()
            print('urltemp1 is ' ,urltemp1)
            # 构建为详细页地址,http://www.aiyadu.com/wp-content/plugins/ordown/down.php?id=14777
            tail = urltemp1.split('/')[-1].split('.')[0]
            urltemp2 ='http://www.aiyadu.com/wp-content/plugins/ordown/down.php?id=' + tail
            #print(urltemp2)
            yield scrapy.Request(urltemp2,callback=self.parse_book) 
            
                      
        #多页数据爬取,原理:执行完一次爬取,当前页数加1,然后通过回调函数重新执行parse方法
        self.current_page+=1
        if(self.current_page < 200):
            next_url = "http://www.aiyadu.com/page/%d"%(self.current_page)
            yield scrapy.Request(next_url, headers=self.aiyadu_headers, callback=self.parse)

        

# 书籍页面解析
    def parse_book(self, response):
        book=BookItem()
        book['name']=response.xpath('//div[@class="ordown-header"]//h2//text()').extract()
        book['url']=response.xpath('//div[@class="ordown-header"]//a[@class="ordown-button"]/@href').extract()
        print(book['name'],book['url'])
        yield book

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值