使用Python

时间:2017-02-21 11:23:29

标签: python reddit

我必须在pokemonGO subreddit(r / pokemonGO /)上的前500名所有时间标题中计算Charizard,Valor,Mystic,Instinct,Pokemon,pokémon(大写和小写)的出现次数

我得到了Charizard,Valor,Mystic和Instinct的答案。然而,我的口袋妖怪(口袋妖怪+神奇宝贝)的价值是122.正确的值是129.我的代码在下面;我错过了什么?

import urllib.request
from urllib.request import Request
import json
import praw
import requests 
from pprint import pprint 
import string

Charizard = 0
Pokemon = 0
Valor = 0
Instinct = 0
Mystic = 0
numtitle = 0
url = "https://www.reddit.com/r/pokemongo/top.json?sort=top&t=all&limit=100"
i = 0
for j in range(5):
    r = requests.get(url, headers={'User-agent': "orew"})
    data = r.json()
    for i in range((len(data["data"]["children"]))):
        title = (data['data']['children'][i]["data"]  ['title']).lower().translate(str.maketrans('', '', string.punctuation))
        if "charizard" in title.split():
            Charizard += 1
        if "pokemon" in title.split():
            Pokemon += 1
        if "pokémon"in title.split():
            Pokemon += 1
        if "valor" in title.split():
            Valor += 1
        if "mystic" in title.split():
            Mystic += 1
        if "instinct" in title.split():
            Instinct += 1
        numtitle += 1
    url = "https://www.reddit.com/r/pokemongo/top.json? sort=top&t=all&limit=100&after=" + data["data"]["after"]
    print(url)
print("The number of occurrences for Charizard is:",(Charizard))
print("The number of occurrences for Pokemon is:", (Pokemon))
print("The number of occurrences for Valor is:",(Valor))
print("The number of occurrences for Mystic is:",(Mystic))
print("The number of occurrences for Instinct is:",(Instinct))
print(numtitle)

0 个答案:

没有答案