首页 > 解决方案 > 使用 VS Code OS X Yosemite 错误在 Mac 上进行 Web 抓取

问题描述

我正在尝试使用 Python 和 Beautifulsoup 进行一些网络抓取。我在 OS X Yosemite 10.10.5 上使用的是相当旧的 MacBook,无法进一步更新操作系统。我正在使用 VS Code 编写和执行代码。

我已经使用 Home Brew 将 Python 更新到最新版本 - 我认为与 pip 相同。但是,当我尝试运行代码时,我不断收到这些错误消息,如下所示..

我在 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

当我尝试执行代码时输出:

[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

此外,我遵循的代码说明是:https ://www.freecodecamp.org/news/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe/

我尝试了许多在其他论坛上看到的解决方案,但无济于事。有谁知道如何解决这个问题??

标签: pythonsslweb-scrapinghomebrewurllib2

解决方案


推荐阅读