工具跟细节之类的就不写了,具体参考我的前一篇文章,这篇直接上代码吧~
戳这里:Python爬取应用宝
这次的代码:
import scrapy
class AnruanSpider(scrapy.Spider):
name = 'Y_Spider'
#首页
def start_requests(self):
urls = ['http://soft.anruan.com/']
for url in urls:
yield scrapy.Request(url=url,callback=self.parse_Android)
#安卓软件
def parse_Android(self, response):
for Android_url in response.xpath("//ul[@class='QZ_link fl']/li/a/@href"):
Android_url = response.urljoin(Android_url.extract())
yield scrapy.Request(url=Android_url,callback=self.parse_Android_cutegory)
#安卓软件分类
def parse_Android_cutegory(self,response):
for cutegory_url in response.xpath("//div[@class='Min2_L']/a/@href"):
cutegory_url = response.urljoin(cutegory_url.extract())
yield scrapy.Request(url=cutegory_url,callback=self.parse_cutegory_app)
#安卓软件 #其实爬出来的不止安卓的
def parse_cutegory_app(self,response):
#这个循环是用来控制进入下一页的,我也不清楚它到底起作用了没有,要稳一点的话可以用wireshark或者fiddler或者firefox或者Chrome等工具抓包分析一下数据,然后根据数据模拟用户点击来进入下一页
for page_url in response.xpath("//a[@class='next']/@href"):
page_url = response.urljoin(page_url.extract())
yield scrapy.Request(url=page_url,callback=page_url)
###########################################################################
#爬取当前页的app
for app_url in response.xpath("//div[@class='liswrap']/ul/li/a/@href"):
app_url = response.urljoin(app_url.extract())
yield scrapy.Request(url=app_url,callback=self.app_details)
#软件详情
def app_details(self,response):
list = []
for i in response.xpath("//div[@class='info_cent']/ul/li/p/text()").extract():
list.append(i)
yield {
'Title': response.xpath("//div[@class='bt']/p/text()").extract_first(),
'Deverloper': list[0],
'Size': list[1],
'Label': response.xpath("//div[@class='lkbq']/a/text()").extract(),
'Socre': response.xpath("//div[@class='processingbar']/font/text()").extract_first(),#这里爬出来全是9.7分....不想改了
'Introduce': response.xpath("//p[@style='text-indent:2em;']/text()").extract_first(),
'Download': response.xpath("//div[@class='btn az_btn ']/a/@href|//div[@class='']/a/@href").extract()#有的没有下载链接,因为爬到的不止安卓类的数据,可以自行修改一下
}
存到文件是这样的:
这次爬取的感觉跟应用宝对比一下得出来的结论:BAT真好
具体怎么好…各位自行体会…
写到这里,爬了新东西再来分享 88888888