这是一个简单的机器人,它登录到 vbulliten 留言板,扫描线程中的某些文本,然后以新格式的方式发布此文本。在我的 while 循环中,我有一个 if else 条件,其中 x == 1 用于测试目的。当我这样做时,mechanize 工作正常,它选择了正确的形式,并且代码运行良好。但是,当我简单地将 if else 更改为基于某个时间时,我会收到“formnotFound”错误。
from BeautifulSoup import BeautifulSoup
from datetime import datetime
import mechanize
import re
import sqlite3
import time
def login(page):
br = mechanize.Browser()
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_equiv(True)
br.set_handle_refresh(True)
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]
br.open("http://www.nottherealsite.com")
br.select_form(nr=0)
br.form['vb_login_username']='notrealusername'
br.form['vb_login_password']='notrealpassword'
br.form['cookieuser'] = ['1']
br.submit()
br.open(page)
return br
br = login("http://www.nottherealsite.com")
html = br.response().read()
soup = BeautifulSoup(html)
x = 1
# Ahead you'll see two functions, insertData() and extraction().
# I didn't include them in my submission here
# because I don't think it's necessary. they dont involve anything to do with
# mechanize, they just strip data out of the pages. The functions also work fine
# with the x==1 conditional, fyi
while True:
now = datetime.now()
minute = now.minute
这工作正常:
if x == 1:
print "it is working . . ."
insertData()
votes = extraction()
br.select_form(nr=6)
br.form['message'] = votes
br.submit()
x = 2
else:
print "we are now done"
break
如果我替换x== 1
为minute > 30
,我会在 mechanize 中收到一个 form not found 错误(是的,时间已超过 30 分钟):
if minute > 30:
print "it is working . . ."
insertData()
votes = extraction()
br.select_form(nr=6)
br.form['message'] = votes
br.submit()
# remember, I am just concerned about mechanize going through
x = 2
else:
print "we are now done"
break
我知道可能很难说出我在做什么实际的事情,但请记住,上面的代码现在是用于测试目的。有谁知道为什么当我更改 if else 语句时机械化会失败?这对我来说没有任何意义。谢谢