1

我正在编写一个 python 代码,它在 facebook 上接收新通知并将“m”发送到串行端口
(arduino 连接到 PC)

在 python 监视器上,您可以写下您的电子邮件和密码以登录您的 Facebook
,如果还没有新通知,则会出现“Zzz...”。如果有新通知,屏幕上会出现“Facebook 上的新通知,升旗……”。

对于登录 facebook 并打印“Zzz...”,它工作得很好。但问题是,即使我的 facebook 上有新通知,代码在屏幕上也不会显示任何差异。

import mechanize
import cookielib
import serial
import sys
import time
import warnings
'VERSION 2.3 Unlike version 2.0, in this implementation: Instead of counting starting from 2 HTML tags <div class="c"> default, and'
'possible clikkare the link: clik on Clear Notifications (will not be '
'just a <div class="c">) and start counting for> 1'
'In this way, we avoid counting invitations to events or warnings as if they were birthday notifications.'
warnings.filterwarnings("ignore") 
# hiding any warning 
SERIALPORT = raw_input("Enter the serial port to which' the connected device:") 

# Set up serial port 
try:
    ser = serial.Serial(SERIALPORT, 9600)
    ser.timeout = None
except serial.SerialException:
    print "No device connected. exiting..."
    time.sleep(4)
    sys.exit()

# Alternatively you can 'go on facebook. com / notifications.php and lean to feed (using then the python library: feedparser). 
# Browser 
br = mechanize.Browser()

# Cookie Jar 
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj) 

# Browser options
br.set_handle_equiv(True)
#br.set_handle_grip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False) 

# Follows refresh 0 but not hangs on refresh> 0 
# br.set_handle_refresh (mechanize._http.HTTPRefreshProcessor (), max_time = 1) 



# User-Agent (this is cheating, ok?) 
br.addheaders = [('User-agent' , 'Mozilla/5.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)')] 

# Open some site, let's pick a random one, the first That pops into mind: 
r = br.open('http://m.facebook.com') 
#html= r.read() 

# Show the source 
#print html
# or  
#print br. response (). read () 

# Show the html title 
print "- Connected to:" + br.title ()

# Select the first (index zero) form 
br.select_form(nr = 0) 

try: 
    wait = input("\ n Every how many seconds you want to check for new notifications? ") 
    while(wait>=5):
        wait = input(" \ n - E 'recommended a value below 5 seconds. \ n Re-Enter a value: ") 
except: 
    print "It 's necessary to enter a numeric value, not a string. \ n Closing the program ..."
    time.sleep(5) 
    sys.exit() 

print "\ n - Enter your login for Facebook: \ n "
# Let's search 
br.form['email'] = '% s'% raw_input(" Enter Email ") 
br.form['pass'] = '% s'% raw_input(" Enter Password: ") 

br.submit()
x = br.response().read() 
z = x.count("Combination e-mail/password incorrect.") 

while z: 
    print "Unable to login, username or password incorrect. Retry: \ n "
    go = False
    br. select_form (nr = 0) 
    br. form ['email'] = '% s'% raw_input (" Enter Email ") 
    br. form ['pass'] = '% s'% raw_input ("Enter Password:") 
    br. submit () 
    x = br. response().read () 
    z = x. count("Combination e-mail/password incorrect.")
else: 
    # we are fully logged.  (Z = 0)
    go = True 
    status = False #status flag, lowered = False 
    try:
        br.Follow_link(text = 'Delete notifications', nr = 0) # Clear previous notifications 
    except: 
        print "\ n No Notification! \ n "
    print " \ n - Mark as read, all previous notifications ... \ n "
    ser.write(" n ") # report the servant to the starting position. 

while True and go: 
    try: 
      # br.reload ( ) 
      # print br.response (). read () 
      x = br.open ('http://m.facebook.com/home.php?refid=0').read () 
      i = x.count ('< div class = "c"> ') # This field HTML is repeated more' than one time if there 'a new notification (before but' are read all notifications such events etc..). 
    except: 
        print "Delay in charge the page, wait 3 seconds applied .. \ n "
        time.sleep(3) 
    if i>= 1: 
        notification = True # Notifications to read 
    else:
        i = 0 #No notification 
        notification = False 

# to read control: 
    if notification == True: 
        if status == False: # If the flag and the 'down 
            ser.write("m") 
            print "New Notification on Facebook, hoisting flag ..." 
            status = True 
        else: 
            print "There are still notifications view ... "

    elif status == False: 
        print " Zzz ... " #flag already 'down, no notification to view 

    else: 
        ser.write("n") 
        print "No new notification flag down ... "
        status = False 

    time.sleep(wait) # Wait before checking for new notifications 

#END 

我认为从 facebook 接收数据存在某种问题,这些数据来自代码:

while True and go: 
    try: 
      # br.reload ( ) 
      # print br.response (). read () 
      x = br.open ('http://m.facebook.com/home.php?refid=0').read () 
      i = x.count ('< div class = "c"> ') # This field HTML is repeated more' than one time if there 'a new notification (before but' are read all notifications such events etc..). 
    except: 
        print "Delay in charge the page, wait 3 seconds applied .. \ n "
        time.sleep(3) 
    if i>= 1: 
        notification = True # Notifications to read 
    else:
        i = 0 #No notification 
        notification = False 

你怎么看?编码是从 python 2.7 完成的。

4

0 回答 0