0
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 "======================="
我试图使用python mechanize登录上述网站。
但它给出了以下错误。无法使用机械化登录
" ValueError: unknown POST form encoding type 'multipart/form-data;charset=utf-8' "
如果你没有得到一个有用的答案你可能会考虑切换到硒。 (您可能不知道?)与2011年上次上传的机械化形成对比,该产品正在积极开发中,许多人正在使用它。开始使用它也很容易。请参阅http://selenium-python.readthedocs.io/index.html。 –
谢谢,@BillBell使用硒也一样,工作得很好:) –
非常欢迎。 –