我为什么会收到错误:AtributeError:在`with`语句中使用request.get()时出现__enter__

时间:2019-06-22 22:54:36

标签: python python-3.x python-requests

我正在尝试使用get()方法获取页面,而请求库却给了我这个例外:

Exception has occurred: AttributeError
__enter__


url = "https://google.com"
with requests.get(url, stream=True) as r:
    r.raise_for_status()
    with open(name, 'wb') as f:
        for chunk in r.iter_content(chunk_size=8192):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)
                # f.flush()

1 个答案:

答案 0 :(得分:1)

尝试安装:

requests==2.22.0
requests-html==0.6.6

对我来说很好,这是代码的输出:

import requests
url = "https://google.com"
with requests.get(url, stream=True) as r:
    r.raise_for_status()
    with open("/home/rosafi/PycharmProjects/test1/templates/test", 'wb') as f:
        for chunk in r.iter_content(chunk_size=8):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)
                # f.flush()

输出:

<!doctype html><html dir="rtl" itemscope="" itemtype="http://schema.org/WebPage" lang="ar-TN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="xzZeW0/A1PQZBJ6vrTpl1A==">(function(){window.google={kEI:'9rIOXZbiCoXMwQL834TwCA',kEXPI:'0,1353804,1958,1640,782,1225,731,223,510,1065,3152,378,206,397,508,112,176,940,91,209,68,240,21,1134112,1197703,329514,1344,12383,4855,32692,8161,7086,867,12163,12919,3602,364,3319,5505,2442,260,5107,575,835,284,2,204,374,728,2431,1362,4323,4968,773,2256,4738,2898,220,2595,2287,1314,669,1050,1808,1397,81,7,492,10952,5297,796,101,1119,661,297,681,65,7,120,37,1180,1364,1611,2736,2630,437,2,625,3240,44,4783,2607,632,1139,1089,656,20,318,1592,617,2,1159,777,1,369,1316,1461,98,36,2,366,18,399,992,509,598,10,168,8,109,1018,142,900,453,1061,80,48,459,94,25,10,413,244,612,1042,789,381,25,177,168,155,5,1245,7,298,543,123,200,193,532,370,166,416,38,703,117,1661,338,281,251,67,22,152,666,109,151,1189,2,7,7,1495,554,606,636,1195,8,159,25,366,229,508,434,240,17,503,21,340,141,556,18,268,472,121,10,247,203,103,196,27,216,71,113,228,120,798,328,1172,113,36,63,135,282,507,267,60,157,121,251,427,193,319,62,62,763,155,80,641,40,568,245,228,509,108,435,101,266,379,41,229,615,8,117,74,244,670,51,864,336,303,49,1,5920651,2964,8797376,4,1572,549,333,444,1,2,80,1,900,583,9,304,1,8,1,2,2132,1,1,1,1,1,414,1,748,141,59,726,3,7,563,1,2482,105,30,46,91,5,2,8,38,2,38,31,2,1,1',authuser:0,kscs:'c9c918f0_9rIOXZbiCoXMwQL834TwCA',kGL:'TN'};google.sn='webhp';google.kHL='ar-TN';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.time=function(){return(new Date).getTime()};google.log=function(a,b,e,c,g){if(a=google.logUrl(a,b,e,c,g)){b=new Image;var d=google.lc,f=google.li;d[f]=b;b.onerror=b.onload=b.onabort=function(){delete d[f]};google.vel&&google.vel.lu&&google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,e,c,g){var d="",f=google.ls||"";e||-1!=b.search("&ei=")||(d="&ei="+google.getEI(c),-1==b.search("&lei=")&&(c=google.getLEI(c))&&(d+="&lei="+c));c="";!e&&google.cshid&&-1==b.search("&cshid=")&&"slh"!=a&&(c="&cshid="+google.cshid);a=e||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+d+f+"&zx="+google.time()+c;/^http:/i.test(a)&&google.https()&&(google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a};}).call(this);(function(){google.y={};google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};}).call(this);google.f={};var a=window.location,b=a.href.indexOf("#");if(0<=b){var c=a.href.substring(b+1);/(^|&)q=/.test(c)&&-1==c.indexOf("#")&&a.replace("/search?"+c.replace(/(^|&)fp=[^&]*/g,"")+"&cad=h")};</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:left}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-left:.5em;vertical-align:top}#gbar{float:right}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}
</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#36c}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-right:4px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}body{background:#fff;color:black}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff !important}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-right:13px;font-size:11px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #ccc #999 #999;height:30px}.lsbb{display:block}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}.tiah{width:458px}</style><script nonce="xzZeW0/A1PQZBJ6vrTpl1A=="></script></head><body bgcolor="#fff"><script nonce="xzZeW0/A1PQZBJ6vrTpl1A==">(function(){var src='/images/nav_logo229.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}
}
})();</script><div id="mngb"> <div id=gbar><nobr><b class=gb1>&#1576;&#1581;&#1579;</b> <a class=gb1 href="https://maps.google.tn/maps?hl=ar&tab=wl">&#1582;&#1585;&#1575;&#1574;&#1591;</a> <a class=gb1 href="https://www.youtube.com/?gl=TN&tab=w1">YouTube</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 href="https://www.google.com/calendar?tab=wc">&#1578;&#1602;&#1608;&#1610;&#1605;</a> <a class=gb1 href="https://translate.google.tn/?hl=ar&tab=wT">&#1578;&#1585;&#1580;&#1605;&#1577;</a> <a class=gb1 href="https://books.google.tn/bkshp?hl=ar&tab=wp">&#1575;&#1604;&#1603;&#1578;&#1576;</a> <a class=gb1 style="text-decoration:none" href="https://www.google.tn/intl/ar/about/products?tab=wh"><u>&#1575;&#1604;&#1605;&#1586;&#1610;&#1583;</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.tn/history/optout?hl=ar" class=gb4>&#1587;&#1616;&#1580;&#1604; &#1576;&#1581;&#1579; &#1575;&#1604;&#1608;&#1610;&#1576;</a> | <a  href="/preferences?hl=ar" class=gb4>&#1575;&#1604;&#1573;&#1593;&#1583;&#1575;&#1583;&#1575;&#1578;</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=ar&passive=true&continue=https://www.google.com/" class=gb4>&#1578;&#1587;&#1580;&#1610;&#1604; &#1575;&#1604;&#1583;&#1582;&#1608;&#1604;</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div> </div><center><br clear="all" id="lgpd"><div id="lga"><img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo" onload="window.lol&&lol()"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="ar-TN" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><div style="position:relative;zoom:1"><input style="color:#000;margin:0;padding:5px 6px 0 8px;vertical-align:top;padding-left:38px" autocomplete="off" class="lst tiah" value="" title="&#1576;&#1581;&#1579; Google" maxlength="2048" name="q" size="57"><img src="/textinputassistant/tia.png" style="position:absolute;cursor:pointer;left:5px;top:4px;z-index:300" data-script-url="/textinputassistant/11/ar_tia.js" alt="" height="23" onclick="var s=document.createElement('script');s.src=this.getAttribute('data-script-url');(document.getElementById('xjsc')||document.body).appendChild(s);" width="27"></div></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="&#1576;&#1581;&#1579; Google" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" value="&#1590;&#1585;&#1576;&#1577; &#1581;&#1592;" name="btnI" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'" type="submit"></span></span></td><td class="fl sblc" align="right" nowrap="" width="25%"><a href="/advanced_search?hl=ar-TN&amp;authuser=0">&#1576;&#1581;&#1579; &#1605;&#1578;&#1602;&#1583;&#1605;</a><a href="/language_tools?hl=ar-TN&amp;authuser=0">&#1571;&#1583;&#1608;&#1575;&#1578; &#1575;&#1604;&#1604;&#1594;&#1577;</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"><script nonce="xzZeW0/A1PQZBJ6vrTpl1A==">(function(){var a,b="1";if(document&&document.getElementById)if("undefined"!=typeof XMLHttpRequest)b="2";else if("undefined"!=typeof ActiveXObject){var c,d,e=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(c=0;d=e[c++];)try{new ActiveXObject(d),b="2"}catch(h){}}a=b;if("2"==a&&-1==location.search.indexOf("&gbv=2")){var f=google.gbvu,g=document.getElementById("gbv");g&&(g.value=a);f&&window.setTimeout(function(){location.href=f},0)};}).call(this);</script></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id="gws-output-pages-elements-homepage_additional_languages__als"><style>#gws-output-pages-elements-homepage_additional_languages__als{font-size:small;margin-bottom:24px}#SIvCob{display:inline-block;line-height:28px;}#SIvCob a{padding:0 3px;}.H6sW5{display:inline-block;margin:0 2px;white-space:nowrap}.z4hgWe{display:inline-block;margin:0 2px}</style><div id="SIvCob">&#1605;&#1581;&#1585;&#1617;&#1603; &#1576;&#1581;&#1579; Google &#1605;&#1578;&#1608;&#1601;&#1617;&#1585; &#1576;&#1575;&#1604;&#1604;&#1594;&#1577;:  <a dir="ltr" href="https://www.google.com/setprefs?sig=0_ta1C5Ix5HzeRSba6gaQ5-L250eQ%3D&amp;hl=fr&amp;source=homepage&amp;sa=X&amp;ved=0ahUKEwiWtfPRmP7iAhUFZlAKHfwvAY4Q2ZgBCAU">Fran�ais</a>  </div></div></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="fll"><a href="/intl/ar/ads/">&#1575;&#1604;&#1576;&#1585;&#1606;&#1575;&#1605;&#1580; &#1575;&#1604;&#1573;&#1593;&#1604;&#1575;&#1606;&#1610;</a><a href="http://www.google.com/intl/ar/services/">&#1581;&#1604;&#1608;&#1604; &#1575;&#1604;&#1588;&#1585;&#1603;&#1575;&#1578;</a><a href="/intl/ar/about.html">&#1603;&#1604; &#1605;&#1575; &#1578;&#1581;&#1576; &#1605;&#1593;&#1585;&#1601;&#1578;&#1607; &#1593;&#1606; Google &#1607;&#1606;&#1575;</a><a dir="ltr" href="https://www.google.com/setprefdomain?prefdom=TN&amp;prev=https://www.google.tn/&amp;sig=K_iFfx_UjIJOG_UTjsLICqakaukes%3D">Google.tn</a></div></div><p style="color:#767676;font-size:8pt">&copy; 2019 - <a href="/intl/ar/policies/privacy/">&#1575;&#1604;&#1582;&#1589;&#1608;&#1589;&#1610;&#1577;</a> - <a href="/intl/ar/policies/terms/">&#1575;&#1604;&#1576;&#1606;&#1608;&#1583;</a></p></span></center><script nonce="xzZeW0/A1PQZBJ6vrTpl1A==">(function(){window.google.cdo={height:0,width:0};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b){var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body;a=d.clientWidth;b=d.clientHeight}a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);}).call(this);})();(function(){var u='/xjs/_/js/k\x3dxjs.hp.en.Ew4N-E2XG6Y.O/m\x3dsb_he,d/am\x3dABbUBA/d\x3d1/rs\x3dACT90oGhD0sQhtbjcwMj8ewkcjrLmCBFSA';setTimeout(function(){var a=document.createElement("script");a.src=u;google.timers&&google.timers.load&&google.tick&&google.tick("load","xjsls");document.body.appendChild(a)},0);})();(function(){window.google.xjsu='/xjs/_/js/k\x3dxjs.hp.en.Ew4N-E2XG6Y.O/m\x3dsb_he,d/am\x3dABbUBA/d\x3d1/rs\x3dACT90oGhD0sQhtbjcwMj8ewkcjrLmCBFSA';})();function _DumpException(e){throw e;}
function _F_installCss(c){}
(function(){google.spjs=false;google.snet=true;google.em=[];google.emw=false;})();google.sm=1;(function(){var pmc='{\x22Qnk92g\x22:{},\x22RWGcrA\x22:{},\x22U5B21g\x22:{},\x22YFCs/g\x22:{},\x22ZI/YVQ\x22:{},\x22d\x22:{},\x22mVopag\x22:{},\x22sb_he\x22:{\x22agen\x22:true,\x22cgen\x22:true,\x22client\x22:\x22heirloom-hp\x22,\x22dh\x22:true,\x22dhqt\x22:true,\x22ds\x22:\x22\x22,\x22ffql\x22:\x22en\x22,\x22fl\x22:true,\x22host\x22:\x22google.com\x22,\x22isbh\x22:28,\x22jsonp\x22:true,\x22msgs\x22:{\x22cibl\x22:\x22&#1605;&#1581;&#1608; &#1575;&#1604;&#1576;&#1581;&#1579;\x22,\x22dym\x22:\x22&#1607;&#1604; &#1578;&#1602;&#1589;&#1583; :\x22,\x22lcky\x22:\x22&#1590;&#1585;&#1576;&#1577; &#1581;&#1592;\x22,\x22lml\x22:\x22&#1605;&#1586;&#1610;&#1583; &#1605;&#1606; &#1575;&#1604;&#1605;&#1593;&#1604;&#1608;&#1605;&#1575;&#1578;\x22,\x22oskt\x22:\x22&#1571;&#1583;&#1608;&#1575;&#1578; &#1575;&#1604;&#1573;&#1583;&#1582;&#1575;&#1604;\x22,\x22psrc\x22:\x22&#1578;&#1605;&#1578; &#1573;&#1586;&#1575;&#1604;&#1577; &#1607;&#1584;&#1575; &#1575;&#1604;&#1576;&#1581;&#1579; &#1605;&#1606; \\u003Ca href\x3d\\\x22/history\\\x22\\u003E&#1587;&#1616;&#1580;&#1604; &#1575;&#1604;&#1576;&#1581;&#1579;\\u003C/a\\u003E.\x22,\x22psrl\x22:\x22&#1573;&#1586;&#1575;&#1604;&#1577;\x22,\x22sbit\x22:\x22&#1575;&#1604;&#1576;&#1581;&#1579; &#1576;&#1581;&#1587;&#1576; &#1575;&#1604;&#1589;&#1608;&#1585;\x22,\x22srch\x22:\x22&#1576;&#1581;&#1579; Google\u200f\x22},\x22ovr\x22:{},\x22pq\x22:\x22\x22,\x22refpd\x22:true,\x22rfs\x22:[],\x22sbpl\x22:24,\x22sbpr\x22:24,\x22scd\x22:10,\x22sce\x22:5,\x22stok\x22:\x22-RUitIsXPB_oGvdZZQvR9nmhu-U\x22,\x22uhde\x22:false}}';google.pmc=JSON.parse(pmc);})();</script>        </body></html>