0

I am trying to do some web scraping using using Python and Beautifulsoup. I am using a fairly old MacBook on OS X Yosemite 10.10.5 and can't update the OS further. I am using VS Code to write and execute the code.

I have used Home Brew to update Python to the latest version - I think, same with pip. However when I try to run the code I keep getting these error messages, as seen below..

Code I enter into VS Code

 # import libraries
import urllib2
from bs4 import BeautifulSoup
# specify the url
quote_page = 'http://www.bloomberg.com/quote/SPX:IND'
#
page = urllib2.urlopen(quote_page)
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page,'html.parser')
# Take out the <div> of name and get its value
name_box = soup.find('h1', attrs={'class': 'name'})
name = name_box.text.strip() # strip() is used to remove starting and trailing
print name
# get the index price
price_box = soup.find('div', attrs={'class':'price'})
price = price_box.text
print price

Output when I try to execute code:

[Running] python -u "/Users/TheChef/Desktop/# import libraries.py"
Traceback (most recent call last):
  File "/Users/TheChef/Desktop/# import libraries.py", line 7, in <module>
    page = urllib2.urlopen(quote_page)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 469, in error
    result = self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 656, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590)>

[Done] exited with code=1 in 1.165 seconds

Also, the instructions for the code that I'm following are: https://www.freecodecamp.org/news/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe/

I have tried a number of solutions that I've seen on other forums, to no avail. Any one know how to solve this??

TheChef
  • 1
  • 3
  • Have you seen this: https://stackoverflow.com/questions/44316292/ssl-sslerror-tlsv1-alert-protocol-version Your version of OpenSSL may be too old – Dan-Dev Mar 22 '20 at 19:11
  • @Dan-Dev thanks for your response, Dan. Yeah I have seen that and tried that solution a long with several proposed in the comments - but still doesn't seem to be working. I've updated Python and SSL but it seems like my system keeps refering to the old version of OpenSSL: ~~~~ MacBook-Pro:~ TheChef$ openssl version OpenSSL 1.1.1d 10 Sep 2019 MacBook-Pro:~ TheChef$ python -c "import ssl; print ssl.OPENSSL_VERSION" OpenSSL 0.9.8zg 14 July 2015 ~~~~ – TheChef Mar 22 '20 at 21:22

0 Answers0