Python 2.7 在python上下载文件会被禁止使用http 403

Python 2.7 在python上下载文件会被禁止使用http 403,python-2.7,http-error,Python 2.7,Http Error,我一直在尝试使用Python 2.7从链接下载一个文件,但它一直在说: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)> 我推荐请求和假用户代理 首先,确保已使用命令行安装它们: pip install requests fake-

我一直在尝试使用Python 2.7从链接下载一个文件,但它一直在说:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)>

我推荐
请求
假用户代理

首先,确保已使用命令行安装它们:

pip install requests fake-useragent
然后使用:

import requests
from fake_useragent import UserAgent

ua_str = UserAgent().chrome
url = "https://www.py4e.com/code3/mbox.txt"
r = requests.get(url, headers={"User-Agent": ua_str})
txt = r.content

这是另一种简单的方法

代码:

import urllib2
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

req = urllib2.Request("http://www.py4e.com/code3/mbox.txt", headers=hdr)
page = urllib2.urlopen(req)
content = page.read()
print content
样本输出:

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Received: from murder (mail.umich.edu [141.211.14.90])
     by frankenstein.mail.umich.edu (Cyrus v2.3.8) with LMTPA;
     Sat, 05 Jan 2008 09:14:16 -0500
X-Sieve: CMU Sieve 2.3
Received: from murder ([unix socket])
     by mail.umich.edu (Cyrus v2.2.12) with LMTPA;
     Sat, 05 Jan 2008 09:14:16 -0500
Received: from holes.mr.itd.umich.edu (holes.mr.itd.umich.edu [141.211.14.79])
    by flawless.mail.umich.edu () with ESMTP id m05EEFR1013674;
    Sat, 5 Jan 2008 09:14:15 -0500
Received: FROM paploo.uhi.ac.uk (app1.prod.collab.uhi.ac.uk [194.35.219.184])
    BY holes.mr.itd.umich.edu ID 477F90B0.2DB2F.12494 ; 
     5 Jan 2008 09:14:10 -0500
Received: from paploo.uhi.ac.uk (localhost [127.0.0.1])
    by paploo.uhi.ac.uk (Postfix) with ESMTP id 5F919BC2F2;
    Sat,  5 Jan 2008 14:10:05 +0000 (GMT)
Message-ID: <200801051412.m05ECIaH010327@nakamura.uits.iupui.edu>
Mime-Version: 1.0
Content-Transfer-Encoding: 7bit
Received: from prod.collab.uhi.ac.uk ([194.35.219.182])
          by paploo.uhi.ac.uk (JAMES SMTP Server 2.1.3) with SMTP ID 899
          for <source@collab.sakaiproject.org>;
          Sat, 5 Jan 2008 14:09:50 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (nakamura.uits.iupui.edu [134.68.220.122])
    by shmi.uhi.ac.uk (Postfix) with ESMTP id A215243002
    for <source@collab.sakaiproject.org>; Sat,  5 Jan 2008 14:13:33 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (localhost [127.0.0.1])
    by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11) with ESMTP id m05ECJVp010329
    for <source@collab.sakaiproject.org>; Sat, 5 Jan 2008 09:12:19 -0500
Received: (from apache@localhost)
    by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id m05ECIaH010327
    for source@collab.sakaiproject.org; Sat, 5 Jan 2008 09:12:18 -0500
Date: Sat, 5 Jan 2008 09:12:18 -0500
X-Authentication-Warning: nakamura.uits.iupui.edu: apache set sender to stephen.marquard@uct.ac.za using -f
To: source@collab.sakaiproject.org
From: stephen.marquard@uct.ac.za
Subject: [sakai] svn commit: r39772 - content/branches/sakai_2-5-x/content-impl/impl/src/java/org/sakaiproject/content/impl
斯蒂芬的
。marquard@uct.ac.za2008年1月5日星期六09:14:16
返回路径:
收到:来自谋杀案(mail.umich.edu[141.211.14.90])
由frankenstein.mail.umich.edu(Cyrus v2.3.8)与LMTPA合作编写;
2008年1月5日星期六09:14:16-0500
X筛:CMU筛2.3
收到:来自谋杀([unix套接字])
使用LMTPA通过mail.umich.edu(Cyrus v2.2.12)发送;
2008年1月5日星期六09:14:16-0500
收到:来自holes.mr.itd.umich.edu(holes.mr.itd.umich.edu[141.211.14.79])
通过带有ESMTP id m05EEFR1013674的fertless.mail.umich.edu()发送;
2008年1月5日星期六09:14:15-0500
收到:来自paploo.uhi.ac.uk(app1.prod.collab.uhi.ac.uk[194.35.219.184])
BY.mr.itd.umich.edu ID 477F90B0.2DB2F.12494;
2008年1月5日09:14:10-0500
收到:来自paploo.uhi.ac.uk(localhost[127.0.0.1])
由paploo.uhi.ac.uk(后缀)编写,ESMTP id为5F919BC2F2;
2008年1月5日星期六14:10:05+0000(格林尼治标准时间)
消息ID:
Mime版本:1.0
内容传输编码:7bit
收到:来自prod.collab.uhi.ac.uk([194.35.219.182])
由paploo.uhi.ac.uk(JAMES SMTP服务器2.1.3)提供,SMTP ID为899
对于
2008年1月5日星期六14:09:50+0000(格林尼治标准时间)
收到:来自nakamura.uits.iupui.edu(nakamura.uits.iupui.edu[134.68.220.122])
由shmi.uhi.ac.uk(后缀)编写,ESMTP id为A215243002
对于2008年1月5日星期六14:13:33+0000(格林尼治标准时间)
收到:来自nakamura.uits.iupui.edu(localhost[127.0.0.1])
由nakamura.uits.iupui.edu(8.12.11.20060308/8.12.11)编写,ESMTP id为m05ECJVp010329
对于2008年1月5日星期六09:12:19-0500
收到:(来自apache@localhost)
作者:nakamura.uits.iupui.edu(8.12.11.20060308/8.12.11/提交)id m05ECIaH010327
对于source@collab.sakaiproject.org; 2008年1月5日星期六09:12:18-0500
日期:2008年1月5日星期六09:12:18-0500
X-Authentication-Warning:nakamura.uits.iupui.edu:apache将发送方设置为stephen。marquard@uct.ac.za使用-f
致:source@collab.sakaiproject.org
发件人:斯蒂芬。marquard@uct.ac.za
主题:[sakai]svn提交:r39772-content/branchs/sakai_2-5-x/content-impl/impl/src/java/org/sakai项目/content/impl

希望能有帮助

看看这个:您基本上需要将用户代理设置为浏览器。web服务器的安全设置通常是bot
From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Received: from murder (mail.umich.edu [141.211.14.90])
     by frankenstein.mail.umich.edu (Cyrus v2.3.8) with LMTPA;
     Sat, 05 Jan 2008 09:14:16 -0500
X-Sieve: CMU Sieve 2.3
Received: from murder ([unix socket])
     by mail.umich.edu (Cyrus v2.2.12) with LMTPA;
     Sat, 05 Jan 2008 09:14:16 -0500
Received: from holes.mr.itd.umich.edu (holes.mr.itd.umich.edu [141.211.14.79])
    by flawless.mail.umich.edu () with ESMTP id m05EEFR1013674;
    Sat, 5 Jan 2008 09:14:15 -0500
Received: FROM paploo.uhi.ac.uk (app1.prod.collab.uhi.ac.uk [194.35.219.184])
    BY holes.mr.itd.umich.edu ID 477F90B0.2DB2F.12494 ; 
     5 Jan 2008 09:14:10 -0500
Received: from paploo.uhi.ac.uk (localhost [127.0.0.1])
    by paploo.uhi.ac.uk (Postfix) with ESMTP id 5F919BC2F2;
    Sat,  5 Jan 2008 14:10:05 +0000 (GMT)
Message-ID: <200801051412.m05ECIaH010327@nakamura.uits.iupui.edu>
Mime-Version: 1.0
Content-Transfer-Encoding: 7bit
Received: from prod.collab.uhi.ac.uk ([194.35.219.182])
          by paploo.uhi.ac.uk (JAMES SMTP Server 2.1.3) with SMTP ID 899
          for <source@collab.sakaiproject.org>;
          Sat, 5 Jan 2008 14:09:50 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (nakamura.uits.iupui.edu [134.68.220.122])
    by shmi.uhi.ac.uk (Postfix) with ESMTP id A215243002
    for <source@collab.sakaiproject.org>; Sat,  5 Jan 2008 14:13:33 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (localhost [127.0.0.1])
    by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11) with ESMTP id m05ECJVp010329
    for <source@collab.sakaiproject.org>; Sat, 5 Jan 2008 09:12:19 -0500
Received: (from apache@localhost)
    by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id m05ECIaH010327
    for source@collab.sakaiproject.org; Sat, 5 Jan 2008 09:12:18 -0500
Date: Sat, 5 Jan 2008 09:12:18 -0500
X-Authentication-Warning: nakamura.uits.iupui.edu: apache set sender to stephen.marquard@uct.ac.za using -f
To: source@collab.sakaiproject.org
From: stephen.marquard@uct.ac.za
Subject: [sakai] svn commit: r39772 - content/branches/sakai_2-5-x/content-impl/impl/src/java/org/sakaiproject/content/impl