Skip to content

Commit e869f01

Browse files
committed
wikipedia_scratcher.py updated
1 parent 6870f7a commit e869f01

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

wikipedia/wikipedia_scratcher.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,29 @@ def get_response_elems(language="en"):
8787
session.mount("https://", HTTPAdapter(max_retries=max_retries))
8888
with session.get(request_url) as response:
8989
response.raise_for_status()
90-
search_data = requests.get(request_url).json()
91-
search_data_dict = search_data["query"]["statistics"]
92-
search_data_dict["language"] = language
93-
return search_data_dict
94-
except Exception as e:
90+
search_data = response.json()
91+
9592
if search_data is None:
9693
LOGGER.error(
9794
f"Received Result is None due to Language {language} absent as"
9895
"an available Wikipedia client. Will therefore return an empty"
9996
"dictionary for result, but will continue querying."
10097
)
101-
10298
return {}
103-
elif "query" not in search_data:
104-
LOGGER.error(f"Search data is: \n{search_data}")
105-
sys.exit(1)
106-
else:
107-
LOGGER.error(f"Error occurred during request: {e}")
108-
raise e
99+
100+
search_data_dict = search_data["query"]["statistics"]
101+
search_data_dict["language"] = language
102+
return search_data_dict
103+
104+
except requests.HTTPError as e:
105+
LOGGER.error(f"HTTP Error: {e}")
106+
raise
107+
except requests.RequestException as e:
108+
LOGGER.error(f"Request Exception: {e}")
109+
raise
110+
except KeyError as e:
111+
LOGGER.error(f"KeyError: {e}. Search data is: {search_data}")
112+
raise
109113

110114

111115
def set_up_data_file():

0 commit comments

Comments
 (0)