2

我正在尝试访问 Rap Stats 的数据:http ://rapstats.herokuapp.com/

检查代码后,我确定代码正在调用以获取用于制作图表的数据(http://rapstats.herokuapp.com/articles/ngram_calculator?utf8=%E2%9C%93&q=Jordan%2C +Pippen ),但是我似乎无法访问它,因为它会引发 500 错误。

我需要做什么才能访问此源?是 cookie 的问题吗?

4

1 回答 1

2

你需要设置标题"X-Requested-With":"XMLHttpRequest"。那么它将起作用。很多人忽略了标题,但它们很重要。

例子:

>>> url = "http://rapstats.herokuapp.com/articles/ngram_calculator?utf8=%E2%9C%93&q=east+side%2C+west+side"
>>> headers = {"X-Requested-With":"XMLHttpRequest"}
>>> requests.get(url,headers=headers).text


'{"terms":{"east side":[6.946470498339794e-06,4.55761509117509e-06,4.9083930248462854e-06,1.3582453643085717e-05,2.5561406277436835e-05,3.428688983622296e-05,3.540163610022839e-05,3.5163711050196196e-05,3.0537890234329724e-05,2.257819836960999e-05,1.6322098107779168e
-05,1.6251408236947628e-05,1.3362830544461107e-05,1.1304394411304002e-05,1.1716249999389779e-05,1.3291089058088017e-05,1.0356404400481257e-05,1.1197616430576122e-05,1.2119889951399242e-05,1.0589374960025109e-05,1.1409596651086667e-05,1.518223318841798e-05,2.334483704
650086e-05,2.420714459589029e-05,2.5458478453504195e-05,2.671296415643261e-05],"west side":[0.0,0.0,1.6361310082820951e-06,2.716490728617143e-06,2.8895502748406857e-05,4.052086980644531e-05,4.447897869003054e-05,4.315546356160442e-05,4.094853463239668e-05,2.318841994
7167016e-05,1.5233958233927222e-05,1.0484779507708148e-05,8.41363404651255e-06,8.84691736536835e-06,8.299010416234427e-06,1.0541208563311186e-05,1.2607796661455444e-05,1.6124567660029615e-05,1.795539252059147e-05,1.8637299929644192e-05,2.0458587098500227e-05,2.361680
7181983524e-05,2.633297618845297e-05,2.5259629143537692e-05,2.6608216190114062e-05,2.708657204673237e-05]},"years":[1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013],"smoothing":2}'
于 2013-10-06T12:24:34.680 回答