1
這是一個簡單的機器人登錄到vbulliten的信息板,掃描某個文本的線程,然後在新發布此文本格式化的方式。在我的while循環中,我有一個if else else,其中x == 1用於測試目的。當我這樣做時,機械化工作正常,它選擇正確的形式,並且代碼正常工作。但是當我簡單地改變if else以基於特定時間時,我得到一個「formnotFound」錯誤。Python的機械化失敗,並有一定的條件,但成功與另一個......不知道爲什麼
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
如果我minute > 30
取代x== 1
,我得到一個形式沒有發現在機械化的錯誤(是的,時間已經過去了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語句時爲什麼機械化會失敗?這對我沒有任何意義。謝謝