2015-07-21 3 views
7

Ich versuche, eine BeyerSoup 4.4.0-Datei, die Tag-Namen in camelCase hat, in eine XML-Datei zu scannen, und find_all scheint sie nicht finden zu können. Beispielcode:find_all mit camelCase-Tag-Namen mit BeautifulSoup 4

from bs4 import BeautifulSoup 

xml = """ 
<hello> 
    world 
</hello> 
""" 
soup = BeautifulSoup(xml, "lxml") 

for x in soup.find_all("hello"): 
    print x 

xml2 = """ 
<helloWorld> 
    :-) 
</helloWorld> 
""" 
soup = BeautifulSoup(xml2, "lxml") 

for x in soup.find_all("helloWorld"): 
    print x 

Der Ausgang ich erhalte, ist:

$ python soup_test.py 
<hello> 
    world 
</hello> 

Was ist der richtige Weg, Kamel verrohrten/groß geschrieben Tag-Namen zu suchen?

Antwort

6

Für die Analyse von Groß- und Kleinschreibung mit BeautifulSoup möchten Sie im "xml"-Modus analysieren. Der Standardmodus (HTML-Analyse) interessiert sich nicht für Groß-/Kleinschreibung, da HTML keine Groß-/Kleinschreibung berücksichtigt. In Ihrem Fall, anstatt den "lxml" Modus zu verwenden, schalten Sie ihn auf "xml":

from bs4 import BeautifulSoup 

xml2 = """ 
<helloWorld> 
    :-) 
</helloWorld> 
""" 
soup = BeautifulSoup(xml2, "xml") 

for x in soup.find_all("helloWorld"): 
    print x