Как НЕ печатать смайлики из комментариев или представления при использовании praw - PullRequest
0 голосов
/ 02 ноября 2019

Получение сообщений об ошибках, когда я пытаюсь распечатать комментарии или отправку с смайликами. Как я могу просто игнорировать и печатать только буквы и цифры?

Использование Praw для webscarpe

top_posts2 = page.top(limit = 25)
for post in top_posts2:
   outputFile.write(post.title)
   outputFile.write('   ')
   outputFile.write(str(post.score))
   outputFile.write('\n')
   outputFile.write(post.selftext)
   outputFile.write('\n')

   submissions = reddit.submission(id = post.id)

   comment_page = submissions.comments
   top_comment = comment_page[0] #by default, this will be the best comment of the post

   commentBody = top_comment.body

   outputFile.write(top_comment.body)
   outputFile.write('\n')

Я хочу выводить только буквы и цифры. и, возможно, некоторые специальные символы (или все)

1 Ответ

0 голосов
/ 04 ноября 2019

Есть несколько способов сделать это. Я бы порекомендовал создать функцию «очистки текста»

def cleanText(text):
    new_text = ""
    for c in text:       # for each character in the text
        if c.isalnum():  # check if it is either a letter or number (alphanumeric)
            new_text += c
    return new_text

или если вы хотите включить определенные не алфавитно-цифровые числа

def cleanText(text):
    valid_symbols = "!@#$%^&*()"    # <-- add whatever symbols you want here
    new_text = ""
    for c in text:       # for each character in the text
        if c.isalnum() or c in valid_symbols:  # check if alphanumeric or a valid symbol
            new_text += c
    return new_text

, чтобы в вашем скрипте вы могли что-то сделатькак

commentBody = cleanText(top_comment.body)
...