2
import mechanize,cookielib
br=mechanize.Browser()
cookie_jar=cookielib.CookieJar()
br.set_cookiejar(cookie_jar)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.set_handle_referer(True)
br.set_handle_robots(False)
br.open("http://facebook.com/login.php")
br.select_form(nr=0)
br.form['email']=raw_input("Email please: ")
br.form['pass']=raw_input("Password please: ")
br.submit()
if "Logout" in br.response().read():
    print "Successfuly Logged in...May the Spam Process Begin ;) "
else:
    print "Something is wrong with logging in.Sorry :( "
link_to_spam=raw_input("Enter the messages link for the one you want to spam\n\
Example: https://www.facebook.com/messages/someone: ")
br.open(link_to_spam)
for f in br.forms():
    print f

所以这段代码没有在 facebook 中显示消息表单。不要评论代码的目的。我只是为了好玩而编写它,除非我的朋友接受它,否则实际上不会使用它,但即便如此我不会用太多。有谁知道它为什么不显示?

4

2 回答 2

7

我不确定他们是否使用表单来发送消息,很可能是 javascript。无论哪种方式,这都可以用 Selenium 来完成。

首先,安装 Selenium:https ://pypi.python.org/pypi/selenium

然后从这里下载 chrome 驱动程序:https ://code.google.com/p/chromedriver/downloads/list

将二进制文件放在与您正在编写的 python 脚本相同的文件夹中。(或将其添加到路径或其他任何内容,更多信息:https ://code.google.com/p/selenium/wiki/ChromeDriver )

之后,以下代码应该可以工作:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
email = raw_input("Email: ")
password = raw_input("Password: ")
person =raw_input("Enter the messages link for the one you want to spam\n\
Example: https://www.facebook.com/messages/someone: ")
driver.get("http://www.facebook.com")
elem = driver.find_element_by_id("email")
elem.send_keys(email)
elem = driver.find_element_by_id("pass")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
driver.get("https://facebook.com/messages/"+person)
elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")
elem.send_keys("python test")
elem.send_keys(Keys.RETURN)

它实际上会在新窗口中打开 chrome 并在浏览器中输入文本。

于 2013-07-21T04:53:28.083 回答
0

几乎一切正常,但不是:

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")
elem.send_keys("python test")
elem.send_keys(Keys.RETURN)

应该有一些其他代码,而不是:

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")
于 2018-01-19T11:23:42.607 回答