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: 290)
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
  data input while debugging causes unwanted code completion fred1232 2 2,593 Sep-14-2025, 03:32 PM
Last Post: deanhystad
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 1,309 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  find the average data from everyone in the same year STUdevil 11 3,708 Oct-22-2024, 02:30 PM
Last Post: deanhystad
  I think I need to delete input data because returning to start fails thelad 2 1,679 Sep-24-2024, 10:12 AM
Last Post: thelad
  Help with to check an Input list data with a data read from an external source sacharyya 3 2,618 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 1,159 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 4,033 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  Code won't break While loop or go back to the input? MrKnd94 2 2,743 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Help Switching between keyboard/Mic input in my code Extra 1 2,420 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  get data from excel and find max/min Timmy94 1 2,321 Jul-27-2022, 08:23 AM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.