forked from daveshap/latent_space_activation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki_test.py
37 lines (29 loc) · 917 Bytes
/
wiki_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
def search_wikipedia(query):
url = 'https://en.wikipedia.org/w/api.php'
search_params = {
'action': 'query',
'list': 'search',
'srsearch': query,
'format': 'json'
}
response = requests.get(url, params=search_params)
data = response.json()
# Get the title of the first result
title = data['query']['search'][0]['title']
content_params = {
'action': 'query',
'prop': 'extracts',
'exintro': True,
'explaintext': True,
'titles': title,
'format': 'json'
}
response = requests.get(url, params=content_params)
data = response.json()
# Get the page ID of the first page
page_id = list(data['query']['pages'].keys())[0]
# Print the content of the page
results = data['query']['pages'][page_id]['extract']
return results
search_wikipedia('Python programming')