kevinzg / facebook-scraper

Scrape Facebook public pages without an API key
MIT License
2.35k stars 626 forks source link

how to set username and password? #456

Open songsh opened 3 years ago

songsh commented 3 years ago

i see the readme, no mention the username, passwor, how to set?

neon-ninja commented 3 years ago

Use the parameter credentials, and pass a tuple. For example, get_posts("Nintendo", credentials=("user@mail.com","password"))

jorgegene commented 3 years ago

Is it still working? I've been using the credentials of a new create account and it just pops with unsuccesful login error.

neon-ninja commented 3 years ago

Still working fine for me

AndreaWozzup commented 2 years ago

question: if I set the credentials parameter, can I avoid adding the cookies?

i.e. facebook_scraper.get_page_info(identifier, credentials=(user, password))

I get LoginRequired exception: facebook_scraper.exceptions.LoginRequired: A login (cookies) is required to see this page

neon-ninja commented 2 years ago

get_page_info doesn't support the credentials parameter the same way as get_posts, you would probably want to decouple it, like so:

from pprint import pprint
from facebook_scraper import _scraper, get_page_info
_scraper.login(user, password)
pprint(get_page_info(identifier))
AndreaWozzup commented 2 years ago

Thanks, so if I provide user and password can I avoid passing cookies?

neon-ninja commented 2 years ago

Yes

AndreaWozzup commented 2 years ago

I have a couple more questions, and probably very python-noob (sorry!), still related to login with user and password. Let's say I want to use multiple accounts for scraping, and the scraping may happen concurrently for the different accounts.

So I initialize a dictionary of scrapers, something like this:

users = ["user1@something.com", "user2@something.com", "user3@something.com"]

scrapers = {}

for user in users:
    scraper = FacebookScraper()
    for i in range(3):
        try:
            scraper.login(user, <user password>)
            if scraper.is_logged_in():
                print("User " + user + " logged in.")
                scrapers[user] = scraper
                break
        except LoginError:
            pass
        except TemporarilyBanned:
            print("User " + user + " is temporarily banned.")
            break

1) Do I get to have multiple instances of scraper class in scrapers dict that stay logged in and I can use concurrently, and thread safe?

2) Moreover, usually the first login attempt always fails, it succeeds only on second attempt, is this normal?

neon-ninja commented 2 years ago
  1. I think so, it looks fine to me. Should work, but keep an eye on the interpreted datetimes, as I don't think dateparser is thread safe
  2. First login works fine for me
null15 commented 2 years ago

For some reason I get login unsuccessful?

from facebook_scraper import get_posts

for post in get_posts('113060560534111', pages=5, credentials=("******@hotmail.com","*****")):
    print(post['text'][:50])

File "C:\Users\Raiko\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\facebook_scraper\facebook_scraper.py", line 567, in login raise exceptions.LoginError("Login unsuccessful") facebook_scraper.exceptions.LoginError: Login unsuccessful

neon-ninja commented 2 years ago

Can you login with those credentials in an incognito window on m.facebook.com?

null15 commented 2 years ago

Can you login with those credentials in an incognito window on m.facebook.com?

How could I attempt this? I tried a different approach by saving cookie, and it seem to work, but I really want to be able to login with the credentials tuple, if possible...

neon-ninja commented 2 years ago

Go to https://m.facebook.com/ in your browser, in an incognito session if you're using Chrome or inprivate session if you're using Firefox

neon-ninja commented 2 years ago

Is a login_error.html file generated in your current working directory? If so, can you upload it?

null15 commented 2 years ago

Can you login with those credentials in an incognito window on m.facebook.com?

Yeah, I can login with my credentials on incognito

null15 commented 2 years ago

Is a login_error.html file generated in your current working directory? If so, can you upload it?

https://file.io/RkYpBoHffUQu

neon-ninja commented 2 years ago

It says that file has been deleted. Can you just upload it to this thread directly, by drag & drop?

null15 commented 2 years ago

It says that file has been deleted. Can you just upload it to this thread? login_error.zip

null15 commented 2 years ago

It says that file has been deleted. Can you just upload it to this thread directly, by drag & drop?

It wasn't supported so I had to zip it

neon-ninja commented 2 years ago

The error message is in Swedish, which means your login page was in Swedish too. The login function is currently only designed to work with the English version of the m.facebook.com login page.

null15 commented 2 years ago

The error message is in Swedish, which means your login page was in Swedish too. The login function is currently only designed to work with the English version of the m.facebook.com login page.

How can I make it not be Swedish?

neon-ninja commented 2 years ago

Use a proxy or VPN from an English speaking country?

null15 commented 2 years ago

Use a proxy or VPN from an English speaking country?

Changed to U.S.A now, the error I get now is "login approval needed"

neon-ninja commented 2 years ago

Now you need to approve the new login from a web browser that is logged into that account. You should have got a notification

null15 commented 2 years ago

Now you need to approve the new login from a web browser that is logged into that account. You should have got a notification

I approved it 2 times, but when I run the command prompt again, it asks for login approval again

null15 commented 2 years ago

Maybe I will be forced to use the cookies file? Using NordVPN to USA just keeps asking for login approval, even though I approved myself many times now

neon-ninja commented 2 years ago

Maybe try this? https://github.com/kevinzg/facebook-scraper/commit/7aa3fb10df3c59c7c334239779595dbb66d1bc17

null15 commented 2 years ago

Maybe try this? 7aa3fb1

What should I do? try again?

neon-ninja commented 2 years ago

First install the latest master branch so that you get that commit with pip install git+https://github.com/kevinzg/facebook-scraper.git. Then try again.

null15 commented 2 years ago

First install the latest master branch so that you get that commit with pip install git+https://github.com/kevinzg/facebook-scraper.git. Then try again.

So tried 4 times, had to approve on all 4 First time: had to change PW other 3 times: logged in after confirming with the number sent to my mail

neon-ninja commented 2 years ago

Sounds like it might be easier to just use cookies then. Probably FB detects that thousands of people use that IP address so don't mark it as trusted even when you approve the login.

null15 commented 2 years ago

Sounds like it might be easier to just use cookies then. Probably FB detects that thousands of people use that IP address so don't mark it as trusted even when you approve the login.

Yeah possible, I changed to UK now instead, now I get login unsuccessful, so I suppose only U.S would work

null15 commented 2 years ago

update: chose canada and it works!