python - 关于scrapy的使用
怪我咯
怪我咯 2017-04-18 10:09:56
[Python讨论组]
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(2)
怪我咯

仔细看看一看, 默认的方法可能用错了,应该为start_requests ,和修改了一些bug,就可能爬了,以下是修改后的源代码!


# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import scrapy
from diandian.items import diandianitem
from scrapy.http import Request

class myspider(scrapy.Spider):
    name = 'novel'
    allowed_domain = ['23wx.com']

    def start_requests(self):
        for i in range(1,11):
            url  = 'http://www.23wx.com/class/{}_1.html'.format(i)
            yield Request(url,callback=self.getallurl)
    #得到每个类别的url

    def getallurl(self,response):
        id = BeautifulSoup(response.body, 'lxml').select('.first')[0]['href'].split('_')[0].split('/')[-1]
        maxnumber = BeautifulSoup(response.body, 'lxml').select('.last')[0].text
        for j in range(1, int(maxnumber) + 1):
            url = 'http://www.23wx.com/class/{}_{}.html'.format(id, j)
            yield Request(url, callback=self.getdetail_url)
     # 得到每个类别所有页数的url


    def getdetail_url(self,response):
        for each in BeautifulSoup(response.body, 'lxml').find_all(bgcolor="#FFFFFF"):
            detailurl = each.find_all('td', class_='L')[0].find_all('a')[0]['href']
            yield Request(detailurl,callback=self.parse)
    #得到具体每个小说的url

    def parse(self, response):
        items = diandianitem()
        soup = BeautifulSoup(response.body,'lxml')
        items['name'] = soup.select('#content dd h1')[0].text.split(' ')[0]
        t1 = soup.find_all('table', cellspacing="1")[0].find_all('tr')[0]
        items['category'] = t1.find_all('a')[0].text
        items['author'] = t1.find_all('td')[1].text.strip()
        items['condition'] = t1.find_all('td')[2].text.strip()
        t2 = soup.find_all('table', cellspacing="1")[0].find_all('tr')[1]
        items['save'] = t2.find_all('td')[0].text.strip()
        items['length'] = t2.find_all('td')[1].text.strip()
        items['last_updated'] = t2.find_all('td')[2].text.strip()
        yield items
    #得到小说的具体信息
怪我咯

一看是百度的东西,就知道不好爬,基本上都做了防爬处理,用普通方式去弄肯定不行。要具体分析,这个过程貌似是困难的,祝楼主好运

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号