johnny2000
johnny2000

Reputation: 143

"TypeError: argument of type 'NoneType' is not iterable" when I tried to web scrape

I tried to scrape a Facebook page by filtering the text key with contain the keyword of "Pecahan setiap negeri (Kumulatif).

Although first it was working, right now I couldn't run it again. It showed me

  if wanted in post.get("text", ""):
TypeError: argument of type 'NoneType' is not iterable

First I thought I was banned by Facebook because I keep loop over and over again. But when I tested without if wanted in post.get("text") : it was working fine, so I think it was not Facebook's fault.

How can I solve this issue? Why was it working first but not now?

My code:

from facebook_scraper import get_posts
listposts = []

wanted = "Pecahan setiap negeri (Kumulatif)"  # wanted post
for post in get_posts("myhealthkkm", pages=10):
    if wanted in post.get("text") :
        # print("Found", t)
        listposts.append(post)
    else:
        pass
        # print("Not found")
print(listposts)

Upvotes: 0

Views: 772

Answers (1)

Adam Smooch
Adam Smooch

Reputation: 1322

for loops require an iterable to run, e.g. a list, dictionary, range(), etc.

get_posts("myhealthkkm", pages=10) is what's being passed to your only for loop, so this FN must be returning None - which is not iterable.

you can try:

[...]
post_list = get_posts("myhealthkkm", pages=10)
print(type(post_list), post_list[:50])
for post in post_list:
  [...]

Update: Since the output of get_posts() can be None, add a condition outside the for-loop to loop only if post_list is not None:

Upvotes: 1

Related Questions