Deseo enviar el inicio de sesión al sitio web Reddit.com, navegar a un área particular de la página y enviar un comentario. No veo cuál es el problema con este código, pero no está funcionando porque no se refleja ningún cambio en el sitio de Reddit.Uso de Python y Mechanize para enviar datos del formulario y autenticar
import mechanize
import cookielib
def main():
#Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(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)
#Opens the site to be navigated
r= br.open('http://www.reddit.com')
html = r.read()
# Select the second (index one) form
br.select_form(nr=1)
# User credentials
br.form['user'] = 'DUMMYUSERNAME'
br.form['passwd'] = 'DUMMYPASSWORD'
# Login
br.submit()
#Open up comment page
r= br.open('http://www.reddit.com/r/PoopSandwiches/comments/f47f8/testing/')
html = r.read()
#Text box is the 8th form on the page (which, I believe, is the text area)
br.select_form(nr=7)
#Change 'text' value to a testing string
br.form['text']= "this is an automated test"
#Submit the information
br.submit()
¿Qué pasa con esto?
Intenta agregar un descanso de al menos 10 segundos. También debe inspeccionar (no 'Ver código fuente', sino 'Inspeccionar elemento' en Chrome o similar en FF) el formulario en su navegador y compararlo con el HTML descargado. Podría tener campos llenados dinámicamente por JS. – TryPyPy
Por cierto, Reddit tiene una API, ¿no funcionaría mejor? – TryPyPy
Hmm, déjame intentar agregar sueño. No estoy seguro de cómo usar API ya que no hay documentación para enviar comentarios. – Parseltongue