【问题标题】:Error using urllib using Python 3.6.1 and Python 2.7使用 Python 3.6.1 和 Python 2.7 使用 urllib 时出错
【发布时间】:2017-11-21 09:02:48
【问题描述】:

我试图使用 pip 方法将 urllib 安装到我的 python 3.6.1,但我无法修复错误输出。 错误似乎是这样的:

我首先在网上搜索,发现一个可能的原因是Python3无法识别0,我需要将最后一位数字更改为某事,因此我尝试打开该文件夹中的setup.py文件。 我尝试按照错误中列出的路径访问我的 mac 上的隐藏文件夹,但我无法在我的 mac 中找到任何 pip-build-zur37k_r 文件夹,我将所有隐藏文件都变为可见。

我想使用 urllib.request 库和 BeautifulSoup 提取信息,当我运行以下代码时:

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("https://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj.h1)

错误似乎是这样的:

代码应返回给我以下信息:

<h1>  An Interesting Title </h1>

【问题讨论】:

  • 粘贴错误文本,而不是链接到图像。
  • Related。 (也许是骗人的?)

标签: python python-2.7 urllib


【解决方案1】:

您的错误显示证书验证失败。所以这是网站的问题,而不是您的代码。对urlopen() 的调用对我有用,但也许您的代理服务器对证书比较挑剔。

【讨论】:

    【解决方案2】:

    您访问的网址没有任何 SSL 证书,因此当您想要请求此类站点时,您需要忽略 ssl 检查。如下:

    from urllib.request import urlopen 
    from bs4 import BeautifulSoup 
    import ssl
    
    ctx = ssl.create_default_context() 
    ctx.check_hostname = False 
    ctx.verify_mode = ssl.CERT_NONE 
    html = urlopen("https://www.pythonscraping.com/pages/page1.html",context=ctx)
    
    bsObj = BeautifulSoup(html.read()) print(bsObj.h1)
    

    所以你会得到预期的最终结果。

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多