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

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 - Python机械化browser.forms(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23626163/

10-11 05:06
查看更多