I am write a script for automate things for one website. But when i open this website it has a login server which ask for username and password you need to pass that authentication to open the website. i have that authorization details.
so how to open this wesite with the help of MECHANIZE IN PYTHON
import urllib
import re
import mechanize
br = mechanize.Browser()
response = br.open("http://ab/cabs");
print response.geturl()
print response.read()
but it is giving this error httperror_seek_wrapper: HTTP Error 401: Authorization Required
what should i add?
from base64 import b64encode
import mechanize
url = 'http://192.168.3.5/table.js'
username = 'admin'
password = 'password'
# I have had to add a carriage return ('%s:%s\n'), but
# you may not have to.
b64login = b64encode('%s:%s' % (username, password))
br = mechanize.Browser()
# # I needed to change to Mozilla for mine, but most do not
# br.addheaders= [('User-agent', 'Mozilla/5.0')]
br.addheaders.append(
('Authorization', 'Basic %s' % b64login )
)
br.open(url)
r = br.response()
data = r.read()
print data
And perhaps didn't try this could also work:
import urllib
import re
import mechanize
br = mechanize.Browser()
response = br.open("http://USERNAME:PASSWORD@ab/cabs");
print response.geturl()
print response.read(
)
您尚未提供用户名和密码
br.add_password("http://ab/cabs", "username", "pswd")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.