Python Forum
Issues Scraping Facebook using facebook_scraper
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues Scraping Facebook using facebook_scraper
#1
I'm scraping facebook groups using Facebook_Scraper but I am experiencing issues with it.

Here's the code I have:

import operator
from facebook_scraper import get_posts
from wordlist import words


title_list = []

for submission in get_posts(group = "nintendo", pages=1):
	titles = str(submission['text'][:10])
	titles = titles.replace("\n", " ")
	title_list.append(titles)
	word_list = [word for line in title_list for word in line.split()]

filtered_words1 = [word for word in word_list if word in words]

print(filtered_words1)
The file wordlist.py contains only the following:

words = ["Python"]
Here's the following error I receive:

Error:
Traceback (most recent call last): File "main.py", line 15, in <module> filtered_words1 = [word for word in word_list if word in words] NameError: name 'word_list' is not defined
I'm not sure why it's throwing the error. word_list is defined clearly enough.
Reply
#2
word_list is only defined within the for loop. If get_posts is empty, the loop is not run.

If there were posts, the filtered_words1 assignment would only be using the word_list that was set on the last iteration of the loop. The earlier posts wouldn't contribute. That seems incorrect to me.

As the word_list construction relies only on title_list, I think it needs to be moved outside the loop.
Reply
#3
(Dec-06-2020, 06:38 AM)bowlofred Wrote: word_list is only defined within the for loop. If get_posts is empty, the loop is not run.

If there were posts, the filtered_words1 assignment would only be using the word_list that was set on the last iteration of the loop. The earlier posts wouldn't contribute. That seems incorrect to me.

As the word_list construction relies only on title_list, I think it needs to be moved outside the loop.

How would you adjust it? I'm not 100% sure I understand what you mean.
Reply
#4
Line 12 is inside the loop. Get rid of the indent so that it runs after the loop is complete.
Reply
#5
I have a difficult to find a tutorial to study facebook scraper library, can you suggest one ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to accept facebook cookies using python selenium? pablo86ad 0 171 Apr-06-2024, 09:19 PM
Last Post: pablo86ad
  DELETE Post using Python FaceBook Graph API BIG_PESH 0 1,454 Mar-24-2022, 08:28 PM
Last Post: BIG_PESH
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,312 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  How to add an image to an existing facebook post using python graph API? Ascalon 0 2,223 Dec-01-2020, 08:33 PM
Last Post: Ascalon
  Calling a Facebook API gpbarsky 0 2,175 Jun-04-2018, 02:55 PM
Last Post: gpbarsky

Forum Jump:

User Panel Messages

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