扫码关注官方订阅号
走同样的路,发现不同的人生
仔细看看一看, 默认的方法可能用错了,应该为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中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
仔细看看一看, 默认的方法可能用错了,应该为start_requests ,和修改了一些bug,就可能爬了,以下是修改后的源代码!
一看是百度的东西,就知道不好爬,基本上都做了防爬处理,用普通方式去弄肯定不行。要具体分析,这个过程貌似是困难的,祝楼主好运