Here is my problem with urllib in python 3.
I wrote a piece of code which works well in Python 2.7 and is using urllib2. It goes to the page on Internet (which requires authorization) and grabs me the info from that page.
The real problem for me is that I can't make my code working in python 3.4 because there is no urllib2, and urllib works differently; even after few hours of googling and reading I got nothing. So if somebody can help me to solve this, I'd really appreciate that help.
Here is my code:
request = urllib2.Request('http://mysite/admin/index.cgi?index=127') base64string = base64.encodestring('%s:%s' % ('login', 'password')).replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) result = urllib2.urlopen(request) resulttext = result.read()
Since, everyone can't be allowed to access data from every URL, one would require authentication primarily. To achieve this authentication, typically one provides authentication data through Authorization header or a custom header defined by server. Replace “user” and “pass” with your username and password.
The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.
Thankfully to you guys I finally figured out the way it works. Here is my code:
request = urllib.request.Request('http://mysite/admin/index.cgi?index=127') base64string = base64.b64encode(bytes('%s:%s' % ('login', 'password'),'ascii')) request.add_header("Authorization", "Basic %s" % base64string.decode('utf-8')) result = urllib.request.urlopen(request) resulttext = result.read()
After all, there is one more difference with urllib: the resulttext
variable in my case had the type of <bytes>
instead of <str>
, so to do something with text inside it I had to decode it:
text = resulttext.decode(encoding='utf-8',errors='ignore')
What about urllib.request ? It seems it has everything you need.
import base64 import urllib.request request = urllib.request.Request('http://mysite/admin/index.cgi?index=127') base64string = bytes('%s:%s' % ('login', 'password'), 'ascii') request.add_header("Authorization", "Basic %s" % base64string) result = urllib.request.urlopen(request) resulttext = result.read()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With