2017-06-03 4 views
0

ich eine Funktion haben Artikel Text von wikimedia apiseltsame Verhalten Ausführen von Code über die Funktion

def get_content(lang, title): 
    url = "https://"+ lang +".wikipedia.org/w/api.php" 
    params={'action':'query', 'format':'json', 'titles': title, 'prop': 
     'linkshere','redirects':'true','prop':'extracts','exlimit':'max','explaintext':'true'} 
    response = requests.get(url=url,params=params).json() 
    content = response["query"]["pages"] 
    content = six.next(six.itervalues(content))['extract'] 
    return content 

aber bekommen, wenn ich diese Funktion aufrufen, Fehler auftreten:

Traceback (most recent call last): 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/adapters.py", line 423, in send 
timeout=timeout 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/packages/urllib3/connectionpool.py", line 640, 
in urlopen 
_stacktrace=sys.exc_info()[2]) 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/packages/urllib3/util/retry.py", line 287, in i 
ncrement 
raise MaxRetryError(_pool, url, error or ResponseError(cause)) 
requests.packages.urllib3.exceptions.MaxRetryError: 
HTTPSConnectionPool(host='xn--mesut%20zil-57a.wikipedia.org', 
port=443): Max retries exceeded with url: /w/api.php? 
exlimit=max&titles=en&action=query&format=json&prop=extract 
s&redirects=true&explaintext=true (Caused by 
NewConnectionError('<requests.packages.urllib3.connection.VerifiedHT 
TPSConnection object at 0x7f7a5e74c0b8>: Failed to establish a new 
connection: [Errno -2] Name or service not kno 
wn',)) 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
File "get_data.py", line 165, in <module> 
print(get_content('Mesut Özil','en')) 
File "get_data.py", line 147, in get_content 
response = request_get_data(url=url,params=params).json() 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/api.py", line 70, in get 
return request('get', url, params=params, **kwargs) 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/api.py", line 56, in request 
return session.request(method=method, url=url, **kwargs) 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/sessions.py", line 475, in request 
resp = self.send(prep, **send_kwargs) 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/sessions.py", line 596, in send 
r = adapter.send(request, **kwargs) 
File "/home/klux/anaconda3/lib/python3.5/site- 
packages/requests/adapters.py", line 487, in send 
raise ConnectionError(e, request=request) 
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='xn-- 
mesut%20zil-57a.wikipedia.org', port=443): Max 
retries exceeded with url: /w/api.php? 
exlimit=max&titles=en&action=query&format=json 
&prop=extracts&redirects=true&explaintext=true (Caused by 
NewConnectionError 
('<requests.packages.urllib3.connection.VerifiedHTTPSConnection o 
bject at 0x7f7a5e74c0b8>: Failed to establish a new connection: [Errno 
-2] Name or service not known',)) 

Zuerst dachte ich, ich war blocked by wikimedia für den Aufruf zu viele Anfragen, aber wenn ich diesen Code in "Hauptfunktion" ausführen, funktioniert es perfekt so.

if __name__ == '__main__': 
    lang = 'en' 
    title = 'Mesut Özil' 
    url = "https://"+ lang +".wikipedia.org/w/api.php" 
    params={'action':'query', 'format':'json', 'titles': title, 'prop': 'linkshere', 
      'redirects':'true','prop':'extracts','exlimit':'max','explaintext':'true'} 
    response = requests.get(url=url, params=params).json() 
    content = response["query"]["pages"] 
    content = six.next(six.itervalues(content))['extract'] 
    print(content) 

Ausgang:

Mesut Özil (German pronunciation: [ˈmeːzut ˈøːzil], Turkish: [meˈsut 
ˈøzil]; born 15 October 1988) is a German fo 
otballer who plays for English club Arsenal and the German national 
team... 

Ich weiß nicht, wie dieses seltsame Verhalten zu lösen, habe ich versucht, von Visual Studio-Code zu debuggen und response Variable in get_content Funktion wandte sich an undefined. Also hat jemand eine Lösung für diese Situation?

+0

Leider Ihr Code-Snippet fehlt der für die Reproduktion oder Lösung des Problems notwendige Kontext. In ihrem gegenwärtigen Zustand ist es unmöglich, eine konstruktive Lösung zu schreiben, und als solche ist die Frage für andere Leser nicht nützlich. Ich stimme für die Schließung der Frage. Wenn Sie sich entscheiden, es zu verbessern, wird die Frage automatisch der Wiedereröffnung hinzugefügt. Bitte konsultieren Sie diese zwei Seiten für weitere Informationen: https://stackoverflow.com/help/mcve https://stackoverflow.com/help/reopen-questions – FireAphis

Antwort

1

Ihre Funktion wird erklärt:

def get_content(lang, title): 

Aber ich sehe in Ihrem Traceback:

print(get_content('Mesut Özil','en')) 

ich ziemlich sicher bin, dass es keine solche Stelle ist als https://Mesut Özil.wikipedia.org

+0

Opps, dieser dumme Fehler verursachte mir Kopfschmerzen, vielen Dank. –