apple store 爬虫

前段时间受朋友之托,写了一个爬去Apple store APP应用信息的爬虫。基于scrapy写的。第一次接触scrapy,有很多不太了解的地方。请大家指教。核心代码很短

#! usr/bin/python
# -*- coding: utf-8 -*-

import scrapy
from tutorial.items import TutorialItem
from urllib import unquote
import re


class AppleSpider(scrapy.Spider):
    name = 'apple'
    allowed_domains = ['itunes.apple.com']
    current_category = {}

    def start_requests(self):
        yield scrapy.Request('https://itunes.apple.com/cn/genre/ios/id36?mt=8', self.parse)

    def parse(self, response):
        my_item = TutorialItem()
        app_url = response.url
        app_name = response.xpath('//h1[@itemprop="name"]/text()').extract()
        if len(app_name) > 0:
            category = response.meta['category']
            my_item['app_name'] = app_name[0]
            app_category = response.xpath('//span[@itemprop="applicationCategory"]/text()').extract()
            if len(app_category) > 0:
                my_item['app_category'] = category[0]
            content = response.xpath('//p[@itemprop="description"]/text()').extract()
            temp_content = ""
            if len(content) > 0:
                for c in content:
                    temp_content += c
                my_item['app_content'] = temp_content
                if my_item['app_category'] in self.current_category:
                    self.current_category[my_item['app_category']] += 1
                else:
                    self.current_category[my_item['app_category']] = 1

                my_item['app_rank'] = self.current_category[my_item['app_category']]
                my_item['app_url'] = app_url
                my_item['app_developer'] = response.xpath('//div[@class="left"]//h2/text()').extract()[0]
        yield my_item
        decode_url = unquote(app_url)
        category = re.findall(r"https://itunes.apple.com/cn/genre/ios-(.*)/.*?", decode_url)

        for url in response.xpath('//div[@class="grid3-column"]//ul//li//a/@href').extract():
            yield scrapy.Request(url, meta={'category':category}, callback=self.parse)

粘贴出来,一起学习。

github地址:https://github.com/luotuo/spider-for-apple-store

欢迎大家来fork,star或者pr

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值