Python Mechanize browser.forms()

标签 python forms browser https mechanize

我尝试列出页面 https://www.fmgroup.pl/login 上的所有表单使用 Python mechanize 模块和 browser.forms() 函数。

URL = 'https://www.fmgroup.pl/login'
br = mechanize.Browser()

br.open(URL)
for form in br.forms():
   print str(form)

我收到此错误:

Traceback (most recent call last):
  File "brows.py", line 25, in <module>
    for form in br.forms():
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 420, in forms
  File "build\bdist.win32\egg\mechanize\_html.py", line 557, in forms
  File "build\bdist.win32\egg\mechanize\_html.py", line 237, in forms
  File "build\bdist.win32\egg\mechanize\_form.py", line 844, in ParseResponseEx
  File "build\bdist.win32\egg\mechanize\_form.py", line 981, in _ParseFileEx
  File "build\bdist.win32\egg\mechanize\_form.py", line 758, in feed
  File "build\bdist.win32\egg\mechanize\_sgmllib_copy.py", line 110, in feed
  File "build\bdist.win32\egg\mechanize\_sgmllib_copy.py", line 144, in goahead
  File "build\bdist.win32\egg\mechanize\_sgmllib_copy.py", line 302, in parse_starttag
  File "build\bdist.win32\egg\mechanize\_sgmllib_copy.py", line 347, in finish_starttag
  File "build\bdist.win32\egg\mechanize\_sgmllib_copy.py", line 387, in handle_starttag
  File "build\bdist.win32\egg\mechanize\_form.py", line 735, in do_option
  File "build\bdist.win32\egg\mechanize\_form.py", line 480, in _start_option
mechanize._form.ParseError: OPTION outside of SELECT

我尝试使用不同的模块,但它们没有给我任何形式的结果。

最佳答案

我解决了这个问题:

import requests
import sys
import urllib2
import re
import mechanize
import cookielib
from bs4 import BeautifulSoup


URL = 'https://www.fmgroup.pl/login'
address = 'https://www.fmgroup.pl/panel/developedtree'
br = mechanize.Browser()

response = br.open(URL)
soup = BeautifulSoup(response.read())
for div in soup.findAll('select'):
    div.extract()


response.set_data(str(soup))
br.set_response(response)

br.select_form(nr=0)

但我还有另一个问题:

br.form['YumUserLogin[username]'] = LOGIN
br.form['YumUserLogin_password'] = PASSWORD

错误:

Traceback (most recent call last):
  File "brows.py", line 37, in <module>
    br.form['YumUserLogin[username]'] = LOGIN
  File "build\bdist.win32\egg\mechanize\_form.py", line 2780, in __setitem__
  File "build\bdist.win32\egg\mechanize\_form.py", line 3101, in find_control
  File "build\bdist.win32\egg\mechanize\_form.py", line 3185, in _find_control
mechanize._form.ControlNotFoundError: no control matching name 'YumUserLogin[username]'

看起来像:

<GET https://www.fmgroup.pl/search/search application/x-www-form-urlencoded
  <TextControl(q=)>
  <SubmitControl(yt0= ) (readonly)>>
<POST https://www.fmgroup.pl/user/auth/login application/x-www-form-urlencoded
  <HiddenControl(returnUrl=/panel) (readonly)>
  <HiddenControl(tag=ajax) (readonly)>
  <TextControl(YumUserLogin[username]=)>
  <PasswordControl(YumUserLogin[password]=)>
  <HiddenControl(YumUserLogin[rememberMe]=0) (readonly)>
  <CheckboxControl(YumUserLogin[rememberMe]=[1])>
  <SubmitControl(<None>=Zaloguj) (readonly)>>

关于Python Mechanize browser.forms(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626163/

相关文章:

python - 我想关联来自不同 DataFrame 的多个列

具有 Required 属性的所有字段的 Javascript 验证

php - 如何在 Symfony2 的表单下拉列表中显示多对一关系?

javascript - 是否可以缓存/存档整个 Web 文档(包括 DOM 的精确状态)并稍后重新加载?

java - "java.security.AccessControlException: access denied"执行签名的 Java Applet

html - WPF 框架 - 在外部浏览器中打开链接

python - 滚动不适用于 PySimpleGUI 中的下拉选择

python - Apache/Wsgi - 返回 header 之前脚本超时

Python CRC8计算

Jquery 停止表单提交,除非选中复选框