Kaow
Kaow

Reputation: 563

Facebook scraper 'NoneType' object has no attribute 'find' while get_post

While i using facebook_scraper libraries to get post from facebook page with this code.

from facebook_scraper import get_posts
for post in get_posts('ThaiPBSFan', pages = 50):
    print(post['text'][:100])

It work with few post, then error like this.

Traceback (most recent call last):
  File ".\main.py", line 2, in <module>
    for post in get_posts('ThaiPBSFan', pages = 50):
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\facebook_scraper.py", line 75, in _get_posts
    yield _extract_post(article)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\facebook_scraper.py", line 102, in _extract_post
    text, post_text, shared_text = _extract_text(article)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\facebook_scraper.py", line 137, in _extract_text
    nodes = article.find('p, header')
AttributeError: 'NoneType' object has no attribute 'find'

So what's a problem and how can i fix it.

Upvotes: 0

Views: 794

Answers (2)

ftorreggiani
ftorreggiani

Reputation: 1

I had the same issue, but only when using the most recent version of the package (0.1.12). Try with an older version of the package. For example, I tried the version 0.1.4 and it worked well. To install it, write:

pip install facebook_scraper==0.1.4

in your terminal.

Upvotes: 0

dspencer
dspencer

Reputation: 4481

From the traceback, it seems that facebook_scraper is not returning a valid post; this may be because there are no further posts to find on the page.

Therefore, you could use a try/except block to catch this exception, i.e.:

try:
    for post in get_posts('ThaiPBSFan', pages=50):
        print(post['text'][:100])
except AttributeError:
    print("No more posts to get")

It's not ideal as you would preferably be able to get a more specific exception once there were no more posts to retrieve, but it should work in your case. Be careful with the code insider your try clause - if an AttributeError is raise anywhere else, you will miss it.

Upvotes: 1

Related Questions