Python Mechanize 无法识别表单

标签 python html beautifulsoup mechanize

我正在尝试登录 this page .

br = mechanize.Browser(factory=mechanize.RobustFactory())
br.set_cookiejar(cj)

current_page = br.open(LOGIN_URL)
soup = BeautifulSoup(current_page.get_data())
current_page.set_data(soup.prettify())
br.set_response(current_page)

 print soup.findAll('form')

 assert br.viewing_html()

 for f in br.forms():
     print f.name

但是即使 BeautifulSoup 完美地找到了表格,它也会为表格打印 None 。谁有想法?

最佳答案

像这样的事情会起作用:

    from bs4 import BeautifulSoup

    import mechanize
    import cookielib

    br = mechanize.Browser()

    cj = cookielib.LWPCookieJar()

    br.set_cookiejar(cj)

    host = 'https://order.papajohns.com/secure/signin/frame.html?destination=http%3a%2f%2forder.papajohns.com%2findex.html%3fsite%3dWEB%26dclid%3d%2525n-2543611-4121096-71899047-246709315-0%26esvt%3d336192-GOUSe339376223%26esvq%3dpapa%2520johns%26esvadt%3d999999-0-3934985-1%26esvcrea%3d41751468573%26esvplace%26esvd%3dc%26esvaid%3d30536%26gclid%3dCI2psOHqtbwCFRPxOgodr0gAAg'

    br.addheaders = [('User-agent', 'Firefox')]
    br.open(host)
    br.form = list(br.forms())[0]
    br.form['userName'] = username
    br.form['pwd'] = password
    submit = br.submit()
    code = response.read()
    soup = BeautifulSoup(code)

关于Python Mechanize 无法识别表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21592145/

相关文章:

html - 我有这样的代码 :Dropdown button need to fetch the data which i have entered in input textfield

python - 使用 BeautifulSoup 获取 HTML 标签

python - 导入 Cython 模块时出现 undefined symbol 错误

python - 如何在pygame中检测Sprite和Rect之间的碰撞

python - 从列表构造 numpy 数组

javascript - 如何在 Css 或 Javascript 中为其他图像制作动画?

javascript - html 从右到左导出表格到 excel

Python:在解析 html 代码时跳过行并去除空格

Python 漂亮的 Soup 代码不起作用

python - 用于 iPod 同步的优秀 Python 库