简体   繁体   English

无法使用机械化登录

[英]Unable to login using mechanize

from mechanize import Browser
from bs4 import BeautifulSoup as BS

br = Browser()

# Browser options
# Ignore robots.txt. Do not do this without thought and consideration.
br.set_handle_robots(False)

# Don't add Referer (sic) header
br.set_handle_referer(False)

# Don't handle Refresh redirections
br.set_handle_refresh(False)

#Setting the user agent as firefox
br.addheaders = [('User-agent', 'Firefox')]

br.open('http://pict.ethdigitalcampus.com')
br.select_form(name="loginForm")
br['loginid'] = "username"
br['password']="password"
br.hiddenfield="310a7b2cd0e52dd19c9bbe4c78f1eb6778af88a67a5990969273711054584e037c3bee2f22ea5ebfe7cb6b3d151f54b87c0b232f5424fb54ebdf64f590e9e913"
br.submit()

#Getting the response in beautifulsoup
soup = BS(br.response().read(),"html.parser")

for product in soup.find_all('td', class_="MTTD1"):

    #printing product name and url
    print "Product Name : " + product.a.text
    #print "Product Url : " + product.a["href"]
    print "======================="

I have tried logging in to the above mentioned website using python mechanize. 我尝试使用python mechanize登录到上述网站。
But it gives the following error. 但是它给出了以下错误。

" ValueError: unknown POST form encoding type 'multipart/form-data;charset=utf-8' " “ ValueError:未知的POST表单编码类型'multipart / form-data; charset = utf-8'”

Ended up using requests . 最终使用了请求 Turns out that I was not submitting all the details that the end-point expects. 原来,我没有提交端点期望的所有详细信息。 For future references anyone looking to submit any forms using POST make sure you submit all the form parameters that the end point expects. 对于将来的参考,任何希望使用POST提交任何表单的人,请确保您提交了端点期望的所有表单参数。 These parameters have a attribute name in their tags or You could also do Ctrl+Shift+I and look for the form parameters in the Network section. 这些参数在其标签中具有属性名称,或者您也可以按Ctrl + Shift + I并在“网络”部分中查找表单参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM