Python爬虫:如何爬取苹果AppStore排行榜最新上架的应用?

这个有接口嘛?

就是定时爬最新上架的 appstore 上面的应用


Python爬虫:如何爬取苹果AppStore排行榜最新上架的应用?
8 回复

App Annie 了解一下,曾经去面试,最后一关挂在了与 CTO 的英文沟通上……


要爬AppStore排行榜,直接用requests会被反爬,得模拟真实请求。这里用requests配合伪装headers,再解析JSON数据。

import requests
import json
from datetime import datetime

def fetch_appstore_charts(country='cn', chart_type='top-free', limit=100):
    """
    获取AppStore排行榜数据
    
    Parameters:
    country: 国家代码,默认'cn'(中国)
    chart_type: 榜单类型 
        'top-free': 免费榜
        'top-paid': 付费榜
        'top-grossing': 畅销榜
    limit: 获取数量,默认100
    """
    
    # 伪装成真实浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Referer': f'https://apps.apple.com/{country}/charts/{chart_type}'
    }
    
    # AppStore的API端点
    url = f'https://amp-api.apps.apple.com/v1/catalog/{country}/charts'
    
    params = {
        'limit': limit,
        'chart': chart_type,
        'platform': 'web',
        'additionalPlatforms': 'appletv,ipad,iphone,mac'
    }
    
    try:
        response = requests.get(url, headers=headers, params=params, timeout=10)
        response.raise_for_status()
        
        data = response.json()
        
        # 提取应用信息
        apps = []
        if 'results' in data and chart_type in data['results']:
            chart_data = data['results'][chart_type][0]['data']
            
            for item in chart_data:
                app_info = {
                    'rank': item.get('attributes', {}).get('rank', 0),
                    'name': item.get('attributes', {}).get('name', ''),
                    'artist': item.get('attributes', {}).get('artistName', ''),
                    'bundleId': item.get('attributes', {}).get('bundleId', ''),
                    'url': item.get('attributes', {}).get('url', ''),
                    'icon': item.get('attributes', {}).get('artwork', {}).get('url', ''),
                    'genres': item.get('attributes', {}).get('genres', []),
                    'rating': item.get('attributes', {}).get('userRating', {}).get('value', 0),
                    'ratingCount': item.get('attributes', {}).get('userRating', {}).get('ratingCount', 0)
                }
                apps.append(app_info)
        
        return apps
        
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")
        return []
    except json.JSONDecodeError as e:
        print(f"JSON解析失败: {e}")
        return []

# 使用示例
if __name__ == "__main__":
    # 获取中国区免费榜前50
    apps = fetch_appstore_charts(country='cn', chart_type='top-free', limit=50)
    
    print(f"获取到 {len(apps)} 个应用")
    print("\n排行榜前10:")
    for i, app in enumerate(apps[:10], 1):
        print(f"{i}. {app['name']} - {app['artist']} (评分: {app['rating']:.1f})")
    
    # 保存到JSON文件
    with open('appstore_top_free.json', 'w', encoding='utf-8') as f:
        json.dump(apps, f, ensure_ascii=False, indent=2)

关键点:

  1. 用AppStore的内部API,比爬网页稳定
  2. headers要伪装,特别是User-Agent
  3. 参数country支持’us’、'jp’等国家代码
  4. chart_type可换’top-paid’或’top-grossing’

注意频率控制,别请求太猛。要获取最新上架的应用,可以定期跑这个脚本对比数据变化。

简单说就是调官方接口拿数据,别硬爬。

App Annie 有最新的上架的接口?

苹果好像自己有接口提供

没看到 app 数据啊

To search for applications titled “ Yelp ” and return only the results from the United States iTunes Store, your URL would look like the following:
https://itunes.apple.com/search?term=yelp&country=us&entity=software

4 楼提供的苹果自己的接口,可以根据名称搜 App,但好像没找到根据时间的

你好,苹果这个申请好像有点麻烦,你有申请过账号吗?

回到顶部