【问题标题】:I am attempting to get BeautifulSoup to open wikipedia, but I'm getting a lot of errors back我正在尝试让 BeautifulSoup 打开维基百科,但我收到了很多错误
【发布时间】:2020-01-17 20:44:14
【问题描述】:

我在 pycharm 上运行 bs4,当我设置代码时它只是抛出错误

import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.newegg.com/'

uClient = uReq(my_url)

page_html = uClient.read()

uClient.close()

page_soup = soup(page_html, 'html.parser')

/Users/alirahman/PycharmProjects/scraper/venv/bin/python /Users/alirahman/PycharmProjects/scraper/app.py 回溯(最近一次通话最后): 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 1317 行,在 do_open encode_chunked=req.has_header('传输编码')) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1244 行,在请求中 self._send_request(方法、url、正文、标头、encode_chunked) _send_request 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1290 行 self.endheaders(正文,encode_chunked=encode_chunked) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1239 行,在 endheaders self._send_output(message_body, encode_chunked=encode_chunked) _send_output 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1026 行 自我发送(味精) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 966 行,在发送中 self.connect() 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1414 行,在连接中 服务器主机名=服务器主机名) wrap_socket 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第 423 行 会话=会话 _create 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第 870 行 self.do_handshake() 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第 1139 行,在 do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1076)

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“/Users/alirahman/PycharmProjects/scraper/app.py”,第 7 行,在 uClient = uReq(my_url) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 222 行,在 urlopen 返回 opener.open(url, 数据, 超时) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 525 行,打开 响应 = self._open(请求,数据) _open 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 543 行 '_open',请求) _call_chain 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 503 行 结果 = 函数(*args) https_open 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 1360 行 上下文=self._context,check_hostname=self._check_hostname) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 1319 行,在 do_open 引发 URLError(err) urllib.error.URLError:

进程以退出代码 1 结束

【问题讨论】:

标签: python beautifulsoup pycharm


【解决方案1】:

使用requests 库而不是urllib.request。以下应该工作。

import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.newegg.com/")
soup = BeautifulSoup(response.content, "html.parser")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2018-05-27
    相关资源
    最近更新 更多