Python Forum
in this code, I input Key_word, it can not find although all data was exact Help me! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: in this code, I input Key_word, it can not find although all data was exact Help me! (/thread-40635.html)



in this code, I input Key_word, it can not find although all data was exact Help me! - duchien04x4 - Aug-31-2023

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.")



RE: in this code, I input Key_word, it can not find although all data was exact Help me! - deanhystad - Aug-31-2023

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)



RE: in this code, I input Key_word, it can not find although all data was exact Help me! - duchien04x4 - Aug-31-2023

(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.


RE: in this code, I input Key_word, it can not find although all data was exact Help me! - deanhystad - Aug-31-2023

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.")