我试图在python3中废弃NYT API并存储在MongoDB中,但我得到了 401错误状态验证错误。我的代码如下。您提供的任何帮助对我都有很大的帮助。提前致谢
def scrape_arts(coll, last_date):
page = 0
while page <= 199:
API_KEY = "api key here"
print ('Page:', page)
payload = {'sort':'newest',
'end_date': get_end_date(last_date),
'api-key': API_KEY
}
r = call_api(NYT_URL, payload, page)
page += 1
if r.status_code != 200:
page = 0
last_date += relativedelta(days = -1)
print ('End date:', get_end_date(last_date))
print (r.status_code)
continue
meta, docs = get_response(r)
new_docs = remove_prev_scraps(coll, docs)
if not new_docs: continue
docs_with_body = get_body(new_docs)
for doc in docs_with_body:
try:
coll.insert_one(doc)
except:
continue
NYT_URL = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?q=new+york+times&page=2&sort=oldest&api-key='api key here''
with open(r'F:\\School\\2017-18\\Ingenious\\pi_key.txt') as handle:
API_KEY = handle.read()
scrape_arts(articles, last_date)
这是输出:
Page: 0
End date: 2017/09/19
401
Page: 0
End date: 2017/09/18
401
Page: 0
End date: 2017/09/17
401