Python Request Get方法

Python request库模拟客户端链接,通过Restful API获取Google Play数据

注意Get方法参数的传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
import json
import os

#url_base = "http://127.0.0.1:3000/api/apps/?collection=topselling_free&country=%s&num=500&category="


categories = ['weather','tools','travel_and_local','sports',
'social','news_and_magazines','lifestyle','health_and_fitness',
'entertainment','education','shopping','game']

country = "jp"

if not os.path.isdir(country):
    os.makedirs(country)

url = "http://127.0.0.1:3000/api/apps/"

params = {
    "collection":"topselling_free",
    "country":country,
    "num":"500",
    "category":"",
}


for category in categories:
    params["category"] = category.upper()
    response = requests.get(url,params=params)
    data = response.text
    json_data = json.loads(data)
    apps_info = json_data['results']
    f = open(country + "/" + category+".txt", "w")
    for app_info in apps_info:
        f.write(app_info['appId']+'\n')