我是Python的新手,并试图> pip install linkchecker在Windows 7上.一些说明:
> pip install scrapy也会导致SSL错误.python而pip直到我安装3.4.1中不具备的命令行.> pip search linkchecker作品.也许这是因为pip搜索不会验证网站的SSL证书.以下是运行后我的pip.log的内容pip install linkchecker:
Downloading/unpacking linkchecker
Getting page https://pypi.python.org/simple/linkchecker/
Could not fetch URL https://pypi.python.org/simple/linkchecker/: connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:598)
Will skip URL https://pypi.python.org/simple/linkchecker/ when looking for download links for linkchecker
Getting page https://pypi.python.org/simple/
Could not fetch URL https://pypi.python.org/simple/: connection error: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries …Run Code Online (Sandbox Code Playgroud) 所以我正在尝试制作一个下载webcomics的Python脚本,并将它们放在桌面上的文件夹中.我在这里发现了一些类似的程序,但是没有什么比我需要的更好.我发现最相似的那个就在这里(http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images).我尝试使用此代码:
>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)
Run Code Online (Sandbox Code Playgroud)
然后我在计算机上搜索了一个文件"00000001.jpg",但我找到的只是它的缓存图片.我甚至不确定它是否将文件保存到我的电脑上.一旦我理解了如何下载文件,我想我知道如何处理剩下的文件.基本上只是使用for循环并将字符串拆分为'00000000'.'jpg'并将'00000000'递增到最大数字,我必须以某种方式确定.有关最佳方法或如何正确下载文件的任何建议吗?
谢谢!
编辑6/15/10
这是完成的脚本,它将文件保存到您选择的任何目录中.由于一些奇怪的原因,文件没有下载,他们只是做了.任何关于如何清理它的建议都将非常感激.我目前正在研究如何找到网站上存在的许多漫画,以便我可以获得最新的漫画,而不是在引发一定数量的异常后退出程序.
import urllib
import os
comicCounter=len(os.listdir('/file'))+1 # reads the number of files in the folder to start downloading at the next comic
errorCount=0
def download_comic(url,comicName):
"""
download a comic in the form of
url = http://www.example.com
comicName = '00000000.jpg'
"""
image=urllib.URLopener()
image.retrieve(url,comicName) # download comicName at URL
while comicCounter <= 1000: # not the most elegant solution
os.chdir('/file') # set …Run Code Online (Sandbox Code Playgroud) python中的许多操作都需要通过https访问.这包括pip安装,或仅使用http.client.HTTPSConnection,或内部使用这些内容的任何模块或应用程序.
如果python是从官方的python pkg安装程序安装的,从https://python.org下载,那么它使用的是openssl的内部版本,并且不包含根证书.任何使用SSL连接的内容都会导致此错误:
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
Run Code Online (Sandbox Code Playgroud)
如何安装根证书以使上述错误消失?
如果这是一个愚蠢的问题我很抱歉,但我一直在努力教自己如何使用BeautifulSoup,以便我可以创建一些项目.
我正在关注此链接作为教程:https://www.youtube.com/watch?v = 5GzVNi0oTxQ
遵循与他完全相同的代码后,这是我得到的错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1240, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1079, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 911, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 854, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1237, in connect
server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 376, in wrap_socket
_context=self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 747, in …Run Code Online (Sandbox Code Playgroud) 我正在练习'Web Scraping with Python'的代码,我一直有这个证书问题:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
global pages
html = urlopen("http://en.wikipedia.org"+pageUrl)
bsObj = BeautifulSoup(html)
for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
if 'href' in link.attrs:
if link.attrs['href'] not in pages:
#We have encountered a new page
newPage = link.attrs['href']
print(newPage)
pages.add(newPage)
getLinks(newPage)
getLinks("")
Run Code Online (Sandbox Code Playgroud)
错误是:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1049)>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我也在练习scrapy,但一直都在解决问题:找不到命令:scrapy(我在网上尝试过各种解决方案,但都没有用......真的很令人沮丧)
我需要在公司内部网上使用curtom根证书,并在Mac OS中加载它们TrustStore(KeyChain)确实解决了所有浏览器和GUI应用程序的问题.
它似乎适用curl于Mac OS X附带的版本,但它不适用于python,即使Mac OS 10.12 Sierra附带的版本(Python 2.7.10)
不过,似乎我会受到以下打击:
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
Run Code Online (Sandbox Code Playgroud)
因为我在很多Python工具中遇到这个问题,如果我找到一种方法来避免它而不必修补它,我将非常感激.
自己提供自定义CA证书不是一种选择,因为我无法修补我使用的数十种Python工具.
大多数工具都使用该requests库,但有一些工具直接在Python中使用本机ssl支持.
所以我最近在youtube上使用"新波士顿"视频开始学习Python,一切都很顺利,直到我开始制作一个简单的网络爬虫教程.虽然我理解它没有问题,但当我运行代码时,我得到的错误似乎都基于"SSL:CERTIFICATE_VERIFY_FAILED".自昨晚以来我一直在寻找答案,试图弄清楚如何修复它,似乎没有其他人在视频或他的网站上的评论中遇到与我相同的问题,甚至使用他的某些人的代码网站我得到了相同的结果.我将从网站上发布的代码发布代码,因为它给了我同样的错误,而我编码的代码现在变得一团糟.
import requests
from bs4 import BeautifulSoup
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = "https://www.thenewboston.com/forum/category.php?id=15&orderby=recent&page=" + str(page) #this is page of popular posts
source_code = requests.get(url)
# just get the code, no headers or anything
plain_text = source_code.text
# BeautifulSoup objects can be sorted through easy
for link in soup.findAll('a', {'class': 'index_singleListingTitles'}): #all links, which contains "" class='index_singleListingTitles' "" in it.
href = "https://www.thenewboston.com/" + link.get('href')
title = link.string # just the text, not the HTML …Run Code Online (Sandbox Code Playgroud) Python 2.7.9现在对SSL证书验证要严格得多.真棒!
以前工作的程序现在收到CERTIFICATE_VERIFY_FAILED错误,我并不感到惊讶.但我似乎无法让它们工作(没有完全禁用证书验证).
一个程序使用urllib2通过https连接到Amazon S3.
我将根CA证书下载到名为"verisign.pem"的文件中并尝试以下操作:
import urllib2, ssl
context = ssl.create_default_context()
context.load_verify_locations(cafile = "./verisign.pem")
print context.get_ca_certs()
urllib2.urlopen("https://bucket.s3.amazonaws.com/", context=context)
Run Code Online (Sandbox Code Playgroud)
即使在第4行正确打印出根CA,我仍然会收到CERTIFICATE_VERIFY_FAILED错误.
openssl可以很好地连接到这台服务器.实际上,这是我用来获取CA证书的命令:
openssl s_client -showcerts -connect bucket.s3.amazonaws.com:443 < /dev/null
Run Code Online (Sandbox Code Playgroud)
我拿了链中的最后一个证书并把它放在一个PEM文件中,openssl读得很好.这是Verisign证书:
Serial number: 35:97:31:87:f3:87:3a:07:32:7e:ce:58:0c:9b:7e:da
Subject key identifier: 7F:D3:65:A7:C2:DD:EC:BB:F0:30:09:F3:43:39:FA:02:AF:33:31:33
SHA1 fingerprint: F4:A8:0A:0C:D1:E6:CF:19:0B:8C:BC:6F:BC:99:17:11:D4:82:C9:D0
Run Code Online (Sandbox Code Playgroud)
任何想法如何启用验证?
我安装了python 3.6
brew install python3
并尝试six.moves.urllib.request.urlretrieve从https 下载文件,但它会引发错误
ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:749)
在Python安装(从.pkg)的,自述指示一个需要运行Install Certificates.command在安装后到
certificertify路径能够使用证书.
但是,在brew安装中,此文件不存在,似乎不会运行.
我正在尝试使用boto连接到S3,但它似乎失败了.我尝试了一些解决方法,但它们似乎没有用.任何人都可以帮我这个.下面是代码.
import boto
if not boto.config.has_section('Credentials'):
boto.config.add_section('Credentials')
boto.config.set('Credentials', 'aws_access_key_id', AWS_KEY)
boto.config.set('Credentials', 'aws_secret_access_key', AWS_SECRET_KEY)
if not boto.config.has_section('Boto'):
boto.config.add_section('Boto')
boto.config.set('Boto', 'https_validate_certificates', 'False')
boto.config.add_section('aws info')
boto.config.set('aws info','aws_validate_certs','False')
s3 = boto.connect_s3(validate_certs=False)
bucket = s3.get_bucket(Bucket_NAME)
Run Code Online (Sandbox Code Playgroud) python ×10
ssl ×6
macos ×3
urllib ×2
urllib2 ×2
web-scraping ×2
amazon ×1
amazon-s3 ×1
boto ×1
pip ×1
python-3.x ×1
scrapy ×1
truststore ×1
windows ×1