【问题标题】:Python - Requests pulling HTML instead of JSON - 2020 editionPython - 请求提取 HTML 而不是 JSON - 2020 版
【发布时间】:2023-04-04 18:20:01
【问题描述】:

我想通过请求的内置 json 解析器从银行的公共 API 服务中提取一些汇率值。但是,以下代码返回 HTML 而不是 json 响应。如果我将 URL 放在浏览器中,则会收到正确的 json 响应。我在 Windows 10 上运行 python 3.6.3。

import requests
from datetime import datetime

requestdate = datetime.now()

requesturl = 'https://www.isbank.com.tr/_vti_bin/DV.Isbank/PriceAndRate/PriceAndRateService.svc/GetFxRates'

payload = {'Lang': 'tr', 'fxRateType': 'INTERACTIVE', 'date': requestdate.strftime("%Y-%m-%d")}

theheader = {
   'Accepts': 'application/json', 
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36'
   }

r = requests.get(requesturl, params=payload, headers=theheader)

received_data = r.json()["Data"]

r.text 变量包含以下内容:

'<!DOCTYPE html>\r\n<html><head>\r\n<meta http-equiv="Pragma" content="no-cache"/>\r\n<meta http-equiv="Expires" content="-1"/>\r\n<meta http-equiv="CacheControl" content="no-cache"/>\r\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\r\n<link rel="shortcut icon" href="data:;base64,iVBORw0KGgo="/>\r\n\r\n<script>\r\n\r\n(function(){\r\nwindow["bobcmn"] = "111110101010102000000022000000052000000002257e5d0f200000096300000000300000000300000006/TSPD/300000008TSPD_101300000005https3000000b008d71b590fab2000505c395556f07e0e931c104e72c02f7263ef18b3b4008bfc8d00602baa984f6208848a2bb30a2800bcc38dda75b9810c1d79afa859034c2fe90f88f6031a55aa21de789d6c5feb34f723079ceb7b7d39200000000200000000";\r\n\r\nwindow.lEQ=!!window.lEQ;try{(function(){(function(){var _=-1,_={I:++_,oi:"false"[_],l:++_,Lz:"false"[_],il:++_,lll:"[object Object]"[_],zZ:(_[_]+"")[_],Oz:++_,OZ:"true"[_],zl:++_,Il:++_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?2:1,iI+=(I(383),3);l_=Z_+s_+_i+ii+Ii+Oi+si+iI;window.sZ===l_&&(window.sZ=++l_)}catch(JI){window.sZ=l_}var lI=!0;function l(_,J){_+=J;return _.toString(36)}\nfunction LI(_){var J=14;!_||document[O(J,132,119,129,119,112,119,122,119,130,135,97,130,111,130,115)]&&document[z(J,132,119,129,119,112,119,122,119,130,135,97,130,111,130,115)]!==O(J,132,119,129,119,112,122,115)||(lI=!1);return lI}function z(_){var J=arguments.length,L=[];for(var s=1;s<J;++s)L.push(arguments[s]-_);return String.fromCharCode.apply(String,L)}function O(_){var J=arguments.length,L=[],s=1;while(s<J)L[s-1]=arguments[s++]-_;return String.fromCharCode.apply(String,L)}function SI(){}\nLI(window[SI[O(i_,188,175,187,179)]]===SI);LI(typeof ie9rgb4!==l(1242178186121,i_));LI(RegExp("\\x3c")[l(1372127,i_)](function(){return"\\x3c"})&!RegExp(l(42811,i_))[l(1372127,i_)](function(){return"\'x3\'+\'d\';"}));\nvar ij=window[z(i_,175,194,194,175,177,182,147,196,179,188,194)]||RegExp(O(i_,187,189,176,183,202,175,188,178,192,189,183,178),l(-60,i_))[O(i_,194,179,193,194)](window["\\x6e\\x61vi\\x67a\\x74\\x6f\\x72"]["\\x75\\x73e\\x72A\\x67\\x65\\x6et"]),Ij=+new Date+(I(151)?6E5:569976),jj,Jj,lj,Oj=window[O(i_,193,179,194,162,183,187,179,189,195,194)],zj=ij?I(678)?31457:3E4:I(421)?6E3:4585;\ndocument[O(i_,175,178,178,147,196,179,188,194,154,183,193,194,179,188,179,192)]&&document[O(i_,175,178,178,147,196,179,188,194,154,183,193,194,179,188,179,192)](O(i_,196,183,193,183,176,183,186,183,194,199,177,182,175,188,181,179),function(_){var J=12;document[z(J,130,117,127,117,110,117,120,117,128,133,95,128,109,128,113)]&&(document[z(J,130,117,127,117,110,117,120,117,128,133,95,128,109,128,113)]===O(J,116,117,112,112,113,122)&&_[O(J,117,127,96,126,129,127,128,113,112)]?lj=!0:document[z(J,130,117,\n127,117,110,117,120,117,128,133,95,128,109,128,113)]===l(68616527654,J)&&(jj=+new Date,lj=!1,sj()))});function sj(){if(!document[O(90,203,207,191,204,211,173,191,198,191,189,206,201,204)])return!0;var _=+new Date;if(_>Ij&&(I(592)?6E5:467242)>_-jj)return LI(!1);var J=LI(Jj&&!lj&&jj+zj<_);jj=_;Jj||(Jj=!0,Oj(function(){Jj=!1},I(125)?1:0));return J}sj();var Sj=[I(51)?17795081:18390932,I(130)?27611931586:2147483647,I(517)?1558153217:1955737085];\nfunction jJ(_){var J=5;_=typeof _===z(J,120,121,119,110,115,108)?_:_[O(J,121,116,88,121,119,110,115,108)](I(942)?34:36);var L=window[_];if(!L[z(J,121,116,88,121,119,110,115,108)])return;var s=""+L;window[_]=function(_,J){Jj=!1;return L(_,J)};window[_][z(J,121,116,88,121,119,110,115,108)]=function(){return s}}for(var JJ=(I(979),0);JJ<Sj[l(1294399127,i_)];++JJ)jJ(Sj[JJ]);LI(!1!==window[z(i_,186,147,159)]);window.Zs={iS:"08db3c215f017800c95fcb89d8f5022feb931e00baa5d03c9d2b489b10395feca92925bc45a63637cbc3827ddbed83c42d750e2b9c814a433ec0f264a2e87032376d2d04e3aa604193ca130913c32f725829241f7ff499463909f095ae09264210c33f3d9ba40c02d9e93b569a89c69aca1323a45369182ec2972612e5589060"};\nfunction lJ(_){var J=+new Date,L;!document[O(13,126,130,114,127,134,96,114,121,114,112,129,124,127,78,121,121)]||J>Ij&&(I(860)?553817:6E5)>J-jj?L=LI(!1):(L=LI(Jj&&!lj&&jj+zj<J),jj=J,Jj||(Jj=!0,Oj(function(){Jj=!1},I(687)?0:1)));return!(arguments[_]^L)}function I(_){return 639>_}(function(_){_||setTimeout(function(){var _=setTimeout(function(){},250);for(var L=0;L<=_;++L)clearTimeout(L)},500)})(!0);})();}catch(x){}finally{ie9rgb4=void(0);};function ie9rgb4(a,b){return a>>b>>0};\n\r\n})();\r\n\r\n</script>\r\n\r\n<script type="text/javascript" src="/TSPD/08d71b590fab200073f723eefb2859c773f4a9738c93cff6c057edec60af8de9d1e847346bd23db9?type=7"></script>\r\n<noscript>Please enable JavaScript to view the page content.<br/>Your support ID is:   562830526560985430.</noscript>\r\n</head><body>\r\n</body></html>'

r.headers 返回以下内容:

{'P3P': 'CP="{}"', 'Content-Length': '6549', 'Cache-Control': 'no-store, must-revalidate, no-cache, max-age=0', 'Content-Type': 'text/html'}

【问题讨论】:

  • 我认为问题不在您的代码中。即使您指定内容类型并接受标头,Url 也不会返回 json。

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