2016-04-23 61 views
0

當我運行的代碼,它的工作原理沒有錯誤,但我注意到,它確實沒有登錄到Facebook併發布一篇文章,說「你好世界」 。一旦登錄,永遠不會。 我使用圖形API。試圖登錄到Facebook,但它沒有得到

想得到一些幫助。謝謝

import urllib2, cookielib, re, os, sys 
from facepy import GraphAPI 
import facebook 


class Facebook: 
    def __init__(self, email, password): 

     self.email = email 
     self.password = password 

     cj = cookielib.CookieJar() 
     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
     opener.add_headers = [('Referer', 'http://login.facebook.com/login.php'), 
          ('Content-Type', 'application/x-www-form-urlencoded'), 
          ('User-Agent', 'Mozilla/8.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7'), 
          'Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)'] 

     self.opener = opener 

    def login(self): 

     url = 'https://login.facebook.com/login.php' 
     data = "locale=en_US&non_com_login=&email=" + self.email + "&pass="+ self.password + "&lsd=20TOl" 


     usock = self.opener.open('https://www.facebook.com/') 
     usock = self.opener.open(url, data) 

     read = usock.read() 

     if "Logout" in read: 
      print "Logged in." 
      graph = facebook.GraphAPI(Access Token) 
      graph.put_object("me", "feed", post="Hello, World!") 
     else: 
      print "failed login" 
      print read 
      sys.exit() 
     print read 

f = Facebook("email", "password") 
f.login() 


nik = graph.fql('select uid, name, friends from user where uid in' 
       '(select first_name, middle_name, last_name ' 
       'from friend where uid1 = me())') 
print nik 
+0

您的問題不包括問題。給出一個[mcve]和一個比*「it failed」*更好的問題描述。 – jonrsharpe

+0

@jonrsharpe謝謝 – user3225609

回答

0

我建議你查找一些Facebook文檔,可能有一個API。但我試圖用請求登錄Facebook,但它很痛苦。如果您的目標只是輕鬆登錄到Facebook,您可以簡單地使用硒的webdriver,這很好。 這裏是一個可能讓你開始一個片段:

from selenium import webdriver 

EMAIL='yourmail' 
PASSWORD='yourpassword' 
url = 'https://de-de.facebook.com/login/' 

driver = webdriver.PhantomJS() 
# driver = webdriver.Firefox() 
driver.get(url) 
driver.find_element_by_id('email').send_keys(EMAIL) 
driver.find_element_by_id('pass').send_keys(PASSWORD) 
driver.find_element_by_id('loginbutton').submit() 

出於測試目的,你可以使用的webdriver.Firefox()代替PhantomJS() 一旦你登錄後,您還可以切換到一個請求會話,這可能會更快。 (因此您需要import requests

session = requests.session() 
cook = {i['name']: i['value'] for i in driver.get_cookies()} 
#transfer cookies to new session 
requests.utils.add_dict_to_cookiejar(session.cookies, cook) 
driver.quit() 
相關問題