Python Forum
in this code, I input Key_word, it can not find although all data was exact Help me!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
in this code, I input Key_word, it can not find although all data was exact Help me!
#1
Question 
import requests
import tkinter as tk
from tkinter import filedialog
import re

# Thay thế các giá trị sau bằng thông tin thật của bạn
access_token = "FACE_BOOK API"
group_id = "GROUP_ID

# Nhập từ khóa từ người dùng
keyword = input("Nhập từ khóa: ")

# Loại bỏ ký tự đặc biệt và dấu cách trong từ khóa
keyword = re.sub(r'[^a-zA-Z0-9 ]', '', keyword)
keyword = keyword.lower()

# Lấy danh sách bài viết trong nhóm
group_feed_url = f"https://graph.facebook.com/{group_id}/feed"
group_feed_params = {
    "access_token": access_token,
    "fields": "id,message",
    "limit": 5000,
}

response = requests.get(group_feed_url, params=group_feed_params, timeout=60)
data = response.json()

if "data" in data:
    # Lưu comment của những bài viết chứa từ khóa vào biến comments_text
    comments_text = ""
    for post in data["data"]:
        if "message" in post:
            cleaned_message = re.sub(r'[^a-zA-Z0-9 ]', '', post["message"].lower())
            if keyword in cleaned_message:
                post_id = post["id"]
                comments_url = f"https://graph.facebook.com/{post_id}/comments"
                comments_params = {"access_token": access_token}
                
                comments_response = requests.get(comments_url, params=comments_params)
                comments_data = comments_response.json()
                
                comments_text += f"Post: {post['message']}\n"
                
                for comment in comments_data["data"]:
                    comments_text += f"Comment: {comment['message']}\n"
    
    # Hiển thị hộp thoại để chọn nơi lưu file
    root = tk.Tk()
    root.withdraw()  # Ẩn cửa sổ chính của tkinter
    
    file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
    if file_path:
        with open(file_path, "w", encoding="utf-8") as file:
            file.write(comments_text)
        print(f"Đã lưu file tại: {file_path}")
    else:
        print("Lưu file đã bị hủy.")
else:
    print("Không tìm thấy dữ liệu bài viết trong nhóm.")
Larz60+ write Aug-30-2023, 06:18 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags added for you this time. Please use BBCode tags on future posts.

Also, please provide more information than 'it did not work'

Attached Files

.txt   savecommentFACE.txt (Size: 2.19 KB / Downloads: 95)
Reply
#2
Print the value of keyword and cleaned_message. Is keyword appearing in cleaned_message? Print context_text to see if any matches were found. If you think you saw where keyword was in cleaned_message, but nothing appears in context_text, post the printed output.

You don't have to create a window to use a tkinter file dialog. This works:
from tkinter import filedialog

file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
print(file_path)
Reply
#3
(Aug-31-2023, 03:45 PM)deanhystad Wrote: Print the value of keyword and cleaned_message. Is keyword appearing in cleaned_message? Print context_text to see if any matches were found. If you think you saw where keyword was in cleaned_message, but nothing appears in context_text, post the printed output.

You don't have to create a window to use a tkinter file dialog. This works:
from tkinter import filedialog

file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
print(file_path)
thanks but it still did not work.
Reply
#4
Did you add print commands to your program to print out keyword and cleaned_message? if so, what does it print?
import requests
import tkinter as tk
from tkinter import filedialog
import re
 
# Thay thế các giá trị sau bằng thông tin thật của bạn
access_token = "FACE_BOOK API"
group_id = "GROUP_ID
 
# Nhập từ khóa từ người dùng
keyword = input("Nhập từ khóa: ")
 
# Loại bỏ ký tự đặc biệt và dấu cách trong từ khóa
keyword = re.sub(r'[^a-zA-Z0-9 ]', '', keyword)
keyword = keyword.lower()
print(f"Keyword = '{keyword}')
 
# Lấy danh sách bài viết trong nhóm
group_feed_url = f"https://graph.facebook.com/{group_id}/feed"
group_feed_params = {
    "access_token": access_token,
    "fields": "id,message",
    "limit": 5000,
}
 
response = requests.get(group_feed_url, params=group_feed_params, timeout=60)
data = response.json()
 
if "data" in data:
    # Lưu comment của những bài viết chứa từ khóa vào biến comments_text
    comments_text = ""
    for post in data["data"]:
        if "message" in post:
            cleaned_message = re.sub(r'[^a-zA-Z0-9 ]', '', post["message"].lower())
            print(f"Message= '{cleaned_message }')
            if keyword in cleaned_message:
                print("Match found")
                post_id = post["id"]
                comments_url = f"https://graph.facebook.com/{post_id}/comments"
                comments_params = {"access_token": access_token}
                 
                comments_response = requests.get(comments_url, params=comments_params)
                comments_data = comments_response.json()
                 
                comments_text += f"Post: {post['message']}\n"
                 
                for comment in comments_data["data"]:
                    comments_text += f"Comment: {comment['message']}\n"
     
    # Hiển thị hộp thoại để chọn nơi lưu file
    root = tk.Tk()
    root.withdraw()  # Ẩn cửa sổ chính của tkinter
     
    file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
    if file_path:
        with open(file_path, "w", encoding="utf-8") as file:
            file.write(comments_text)
        print(f"Đã lưu file tại: {file_path}")
    else:
        print("Lưu file đã bị hủy.")
else:
    print("Không tìm thấy dữ liệu bài viết trong nhóm.")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 417 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 346 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 1,062 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  Code won't break While loop or go back to the input? MrKnd94 2 964 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Help Switching between keyboard/Mic input in my code Extra 1 1,099 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  get data from excel and find max/min Timmy94 1 1,122 Jul-27-2022, 08:23 AM
Last Post: Larz60+
  what will be the best way to find data in txt file? korenron 2 1,172 Jul-25-2022, 10:03 AM
Last Post: korenron
  Showing an empty chart, then input data via function kgall89 0 985 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,143 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Matching Exact String(s) Extra 4 1,930 Jan-12-2022, 04:06 PM
Last Post: Extra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020