简体   繁体   中英

how to solve requests.exceptions.ConnectionError in get request - Python

when i run get_word function, i will get this error:

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

import requests
    
base_url = "https://www.ldoceonline.com/dictionary/"
    
def get_word(word: str):
    return requests.get(base_url + word)

Please Answer this problem!

It works for me after I added a User-Agent header

import requests

base_url = "https://www.ldoceonline.com/dictionary/"

def get_word(word: str):
    return requests.get(base_url + word, headers={"User-Agent":"Mozilla/5.0"})

response = get_word('apple')
print(response.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM