0

i managed to find a way to scrap discord messages from a specific channel, turn them into a json format and print the result.

import requests
import json

    def retrieve_messages(channelid):
      headers = {
        'authorization':'MjExMTQ1ODEzNTk2ODMxNzU2.YjtnkQ.-jOz1PucvMjdQ8r2yXdKMpSLK2M'
      }
      response = requests.get(
       f'https://discord.com/api/v9/channels/{channelid}/messages', headers=headers)
      data = json.loads(response.text)
      for description in data:
        print(description)
    
    retrieve_messages('956262565237903401')

Which returns just fine what it's supposed to.

E.G: Cleared in 27:38 of 38:00 (27.3% remaining) for 148 Points\n\n Kindama - Healer (Holy Priest)

The thing that i'd like to do and cannot find how to whatsoever, is, how can i count the occurences in all those lines of a particular keyword such as "Kindama"

I've tried syntax with .index which return false even if the string is present in json,

If anyone with python/json knowledge could give me hints about where to go from there i'd be thankful,

EDIT, Tryouts:

adding to the function:

index=data.index("Kindama")
print(index)

Tells me that 'Kindama' is not in list

adding to the function:

for line in data:
      for key in occurences.keys():
          occurences[key] += line.count(key)        
print(occurences)

Tells me that "AttributeError: 'dict' object has no attribute 'count'"

So what i'm guessing at that point is that the scrapping didnt transform the data in a proper string or that i'm accessing it properly

sleezy
  • 11
  • 2
  • Does this answer your question? [Count number of occurrences of a substring in a string](https://stackoverflow.com/questions/8899905/count-number-of-occurrences-of-a-substring-in-a-string) – matszwecja Mar 24 '22 at 13:23
  • you are saying that index() is giving false. Can you share that code? There may be some issue in that part of the code – Shravya Boggarapu Mar 24 '22 at 13:41
  • @matszwecja the occurences count with .count looks like it should work, just using Kindama_count=data.count("Kindama") print(Kindama_count) but the problem is that it returns 0, it seems like the json.load doesn't create a proper string somehow – sleezy Mar 24 '22 at 14:08

1 Answers1

0

You can use a dictionary and string.count(substring), like so:

text = [
    " Cleared in 27:38 of 38:00 (27.3% remaining) for 148 Points\n\n Kindama - Healer (Holy Priest)",
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.",
    "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
    "Lorem, ipsum, ipsum"
]

occurences = {
    "Kindama" : 0,
    "Lorem" : 0,
    "ipsum" : 0,
    "Nope" : 0
}

for line in text:
    for key in occurences.keys():
        occurences[key] += line.count(key)
        
print(occurences)

Result:

{'Kindama': 1, 'Lorem': 3, 'ipsum': 4, 'Nope': 0}

Notice that the solution is case sensitive

Roman Purgstaller
  • 910
  • 3
  • 11
  • 24
  • This would be just fine, when i inject that in the code it tells me that "AttributeError: 'dict' object has no attribute 'count'", so what i'm guessing is that the discord messages scrapped aren't formated properly in lines like what you used in your exemple – sleezy Mar 24 '22 at 14:03
  • Apparently you are trying to use the "count" function directly on the dictionary returned by the request. You need to access the desired text entry in the dictionary. If you need more help, please provide some details about the returned dictionary from the request. Please also look at the docs: https://docs.python-requests.org/en/latest/ – Roman Purgstaller Mar 25 '22 at 09:36