要爬取AppStore新上架的App数据,直接用requests和BeautifulSoup这类通用库比较麻烦,因为AppStore页面是动态加载的。更靠谱的方法是直接用苹果官方的iTunes Search API,或者用app-store-scraper这个第三方库。
方法一:使用iTunes Search API(官方,简单)
这个API是苹果提供的,可以直接获取App信息。比如找中国区最近上架的免费游戏,可以这样:
import requests
import json
def search_new_apps(country='cn', entity='software', limit=50, genre=6014):
url = 'https://itunes.apple.com/search'
params = {
'country': country,
'entity': entity,
'limit': limit,
'genreId': genre, # 游戏类ID
'attribute': 'releaseDate' # 按发布日期排序
}
response = requests.get(url, params=params)
data = response.json()
if data['resultCount'] > 0:
for app in data['results']:
print(f"App名称: {app.get('trackName')}")
print(f"开发者: {app.get('artistName')}")
print(f"价格: {app.get('formattedPrice', '免费')}")
print(f"发布日期: {app.get('releaseDate')}")
print(f"App Store链接: {app.get('trackViewUrl')}")
print("-" * 50)
# 搜索中国区新上架的游戏
search_new_apps(country='cn', genre=6014)
方法二:使用app-store-scraper库(功能更强)
这个库专门用来爬AppStore数据,能获取更详细的信息:
from app_store_scraper import AppStore
import pandas as pd
def get_new_apps_from_store(country='cn', app_name='', num=10):
# 初始化AppStore对象
app_store = AppStore(country=country, app_name=app_name)
# 搜索应用
app_store.search(limit=num)
# 获取结果
apps = app_store.get_apps()
# 转换为DataFrame方便处理
df = pd.DataFrame(apps)
# 按发布日期排序
if 'releaseDate' in df.columns:
df = df.sort_values('releaseDate', ascending=False)
return df
# 获取中国区新上架的应用
new_apps = get_new_apps_from_store(country='cn', num=20)
print(new_apps[['trackName', 'artistName', 'price', 'releaseDate']])
几个关键点:
- 国家/地区参数:
country='cn'是中国区,'us'是美国区
- 分类ID:游戏是6014,商业是6000,教育是6017,完整列表可以查苹果的文档
- 排序方式:API支持按
releaseDate(发布日期)、price(价格)等排序
- 数据量:免费API有限制,一次最多200条结果
如果要用更高级的功能,比如爬用户评论、获取实时排名,可以考虑用app-store-scraper的更多功能,或者用selenium模拟浏览器。但要注意频率,别把人家服务器搞挂了。
总结:直接用iTunes Search API最省事。