2016-06-01 4 views
0

Ich habe ein Problem festgestellt und ich verstehe nicht, warum es auf diese Weise ausgedruckt wurde.Liste der Schlüsselwörter von JSON erhalten

Unten ist mein Code, bitte vergib mir für die schlechte Formatierung, wie ich bin neu in die Programmierung, das eine Textdatei zu öffnen, die eine Reihe von Schlüsselwörtern hat

import urllib2 
import json 

f1 = open('CatList.text') 
lines = f1.readlines() 

for line in lines: 

    url ='https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle='+line+'&cmlimit=100' 

    print(url) 

    json_obj = urllib2.urlopen(url) 
    data = json.load(json_obj) 

    #to write the result 
    f2 = open('SubList.text', 'w') 

    f2.write(url) 

    for item in data['query']: 

      for i in data['query']['categorymembers']: 


       f2.write((i['title']).encode('utf8')+"\n") 

ich den Fehler:

Traceback (most recent call last): 
    File "Test2.py", line 16, in <module> 
    json_obj = urllib2.urlopen(url) 
    File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "/usr/lib/python2.7/urllib2.py", line 402, in open 
    req = meth(req) 
    File "/usr/lib/python2.7/urllib2.py", line 1113, in do_request_ 
    raise URLError('no host given') 
urllib2.URLError: <urlopen error no host given> 

Ich bin nicht sicher, was dieser Fehler bedeutet, aber ich habe dies versucht, die URL zu drucken.

import urllib2 
import json 

f1 = open('CatList.text') 
f2 = open('SubList.text', 'w') 
lines = f1.readlines() 

for line in lines: 

    url ='https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle='+line+'&cmlimit=100' 

    print(url) 
    f2.write(url+'\n') 

Die Ergebnisse, die ich bekommen habe, waren seltsam (unten Teil des Ergebnisses ist):

https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Branches of geography 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography by place 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography awards and competitions 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography conferences 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography education 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Environmental studies 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Exploration 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geocodes 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geographers 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geographical zones 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geopolitical corridors 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:History of geography 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Land systems 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Landscape 
&cmlimit=100 
https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography-related lists 
&cmlimit=100 

Beachten Sie, dass die URL oben in 2 Teile gebrochen

https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography-related lists 
&cmlimit=100 

statt

https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Geography-related lists&cmlimit=100 

Meine erste Frage ist, wie kann ich das beheben?

Zweitens, ist das, was das gibt mir den Fehler?

Mein CatList.text ist wie folgt:

Category:Branches of geography 
Category:Geography by place 
Category:Geography awards and competitions 
Category:Geography conferences 
Category:Geography education 
Category:Environmental studies 
Category:Exploration 
Category:Geocodes 
Category:Geographers 
Category:Geographical zones 
Category:Geopolitical corridors 
Category:History of geography 
Category:Land systems 
Category:Landscape 
Category:Geography-related lists 
Category:Lists of countries by geography 
Category:Navigation 
Category:Geography organizations 
Category:Places 
Category:Geographical regions 
Category:Surveying 
Category:Geographical technology 
Category:Geography terminology 
Category:Works about geography 
Category:Geographic images 
Category:Geography stubs 

Sorry für den langen Post. Ich schätze deine Hilfe sehr. Vielen Dank.

+0

Erhalten Sie den Fehler mit allen URLs? Es könnte sein, dass Leerzeichen in der URL vorhanden sind. Normalerweise benutze ich die Bibliothek _requests_ für http. Es ist benutzerfreundlicher als urllib2. –

+0

Ich vermute, es ist nicht in der Lage zu erkennen, weil die URL in 2 Zeile statt 1 geteilt wird, aber ich habe keine Ahnung, wie Sie diesen Teil beheben. – windboy

+0

streifen Räume von 'Linie'. Es ist ein Newline-Zeichen am Ende der Zeichenfolge. 'line = line.strip()' –

Antwort

1

Aktualisieren Sie die folgende Zeile

url ='https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle='+line+'&cmlimit=100' 

zu

url ='https://en.wikipedia.org/w/api.php?action=query&format=json&list=categorymembers&cmtitle='+line.strip()+'&cmlimit=100' 

Ihre line enthält Zeilenvorschub (\n) Zeichen, die .strip() entfernt werden verwendet, die von beiden Enden einer Schnur weiß-Leerzeichen entfernt .

+0

Es funktioniert, vielen Dank. – windboy

2

Freund, Allgemein '\ n' wird für neue Zeile verwendet. Gleicher Sinn, In einer Datei ist zwischen jeder Zeile ein '\ n'-Zeichen versteckt.

So bei Zeilen = f1.readlines() es enthält '\ n' am Ende aller Zeilen. Das ist das Problem. Um dies zu vermeiden, sollten Sie als f1.read.splitlines() lesen.

+0

Hallo, ich habe versucht, dass es mir einen seltsamen Fehler 'Builtin_function_or_method' Objekt hat kein Attribut 'Splitlines'. – windboy

+0

Er meinte wahrscheinlich. 'f1.read(). splitlines()' –

+0

Bitte versuchen Sie dies., Zeilen = f1.read(). Split ('\ n') ODER Zeilen = [Zeile [: - 1] für Zeile in Datei] – PriVictor

Verwandte Themen