I have tried to write the following code, I am trying to write a code in Python 3.7
that just opens a web browser and the website fed to it in the Command Line
:
Example.py
import sys
from mechanize import Browser
browser = Browser()
browser.set_handle_equiv(True)
browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
# pretend you are a real browser
browser.addheaders = [('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36')]
listOfSites = sys.argv[1:]
for i in listOfSites:
browser.open(i)
I have entered the following command in the cmd
:
python Example.py https://www.google.com
And I have the following traceback:
Traceback (most recent call last):
File "Example.py", line 19, in <module>
browser.open(i)
File "C:\Python37\lib\site-packages\mechanize\_mechanize.py", line 253, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python37\lib\site-packages\mechanize\_mechanize.py", line 283, in _mech_open
response = UserAgentBase.open(self, request, data)
File "C:\Python37\lib\site-packages\mechanize\_opener.py", line 188, in open
req = meth(req)
File "C:\Python37\lib\site-packages\mechanize\_urllib2_fork.py", line 1104, in do_request_
for name, value in self.parent.addheaders:
ValueError: too many values to unpack (expected 2)
I am very new to Python
. This is my first code here. I am stuck with the above traceback but haven't found the solution yet. I have searched for a lot of questions on SO community as well but they didn't seem to help. What should I do next?
UPDATE:
As suggested by @Jean-François-Fabre, in his answer, I have added 'User-agent'
to the header, now there is no traceback, but still there is an issue where my link cannot be opened in the browser.
Here is how the addheader
looks like now:
browser.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36')]
ModuleNotFoundError: No module named 'urllib2
" – Plutonian