質問

I've been writing a program to log in to Facebook and update the status as a side project. I managed to get the program to login. However, I'm having trouble selecting the textarea that ends up being the "Enter your status here" box. Using "Inspect Element" in Chrome, I'm able to see the form under which it's located, but listing the forms in the program doesn't seem to list said form...

import mechanize
import re

br = mechanize.Browser()
usernamecorrect = 0
while usernamecorrect == 0:
    username = raw_input("What is the username for your Facebook Account? ")
    matchmail = re.search(r'[\w.-]+@[\w.-]+', username)
    if matchmail:
        print matchmail.group()
        usernamecorrect = 1
    else:
        print "That is not a valid username; please enter the e-mail address registered with your account.\n"
password = raw_input("What is the password for your account?")
print "Logging in..."
br.set_handle_robots(False)
br.open("https://www.facebook.com/")
br.select_form(nr = 0)
br['email'] = username
br['pass'] = password
br.submit()
raw_input("Login successful!")
print "Forms: \n"
for f in br.forms():
    print f.name

The full output is as follows:

What is the username for your Facebook Account? myemail@website.com
What is the password for your account? thisisapassword
Logging in...
Login successful!
Forms:

navSearch
None

I took a look through the source of Facebook via Inspect Elements again, and "navSearch" is the "Find People, things, etc." search bar, and the unnamed form appears to have to do with the logout button. However, while Inspect Elements gives at least 2 more forms, one of which holds the status update box. I haven't been able to determine if it's because of JavaScript or not (while the status update box code block is encapsulated in , so are the navSearch and logout forms.) The most relevant thing I've been able to find is that navSearch and the logout forms are in a separate div, but I somehow feel as though that shouldn't be much of a problem for mechanize. Is there just something wrong with my code, or is it something else entirely?

役に立ちましたか?

解決

Is there just something wrong with my code, or is it something else entirely?

Your whole approach is wrong:

I've been writing a program to log in to Facebook and update the status

That’s what the Graph API is for.

Scraping FB pages and trying to act as a “browser” is not the way to go. Apart from the fact, that FB policies do not allow that, you see how difficult it gets on a page that uses JavaScript/AJAX so much.

Go with the API, it’s the easy way.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top