2012-06-26 24 views
54

Voglio stampare un valore di attributo in base al suo nome, prendere ad esempioPython: BeautifulSoup - ottenere un valore di attributo in base all'attributo nome

<META NAME="City" content="Austin"> 

voglio fare qualcosa di simile

soup = BeautifulSoup(f) //f is some HTML containing the above meta tag 
for meta_tag in soup('meta'): 
    if meta_tag['name'] == 'City': 
     print meta_tag['content'] 

Il codice precedente fornisce un valore KeyError: 'name', credo che questo sia dovuto al fatto che il nome viene utilizzato da BeatifulSoup in modo che non possa essere utilizzato come argomento di parole chiave.

risposta

84

E 'piuttosto semplice, utilizzare la seguente -

>>> soup = BeautifulSoup('<META NAME="City" content="Austin">') 
>>> soup.find("meta", {"name":"City"}) 
<meta name="City" content="Austin" /> 
>>> soup.find("meta", {"name":"City"})['content'] 
u'Austin' 

Lascia un commento se qualcosa non è chiaro.

+0

come posso fare questo se voglio trovare tutte le istanze, vale a dire in questo momento, soup.find ("meta", { "name": "Città"}) ['contenuto'] dà il primo risultato, ma dice che c'era un'altra riga nel brodo che era

6

la risposta diharshest è la soluzione migliore, ma per tua conoscenza il problema che hai riscontrato riguarda il fatto che un oggetto Tag in Beautiful Soup si comporta come un dizionario Python. Se accedi al tag ['name'] su un tag che non ha un attributo 'name', otterrai un KeyError.

12

theharshest ha risposto alla domanda, ma qui è un altro modo di fare la stessa cosa. Inoltre, nel tuo esempio hai NOME in maiuscolo e nel tuo codice hai il nome in lettere minuscole.

s = '<div class="question" id="get attrs" name="python" x="something">Hello World</div>' 
soup = BeautifulSoup(s) 

attributes_dictionary = soup.find('div').attrs 
print attributes_dictionary 
# prints: {'id': 'get attrs', 'x': 'something', 'class': ['question'], 'name': 'python'} 

print attributes_dictionary['class'][0] 
# prints: question 

print soup.find('div').get_text() 
# prints: Hello World 
+0

La mancata corrispondenza nel caso è probabilmente intenzionale perché BeautifulSoup converte i tag in lettere minuscole per impostazione predefinita. In questo caso: BeautifulSoup ('') restituisce tuckermi

0

si può anche provare questa soluzione:

per trovare il valore, che è scritto in giro di tavolo

htmlContent


<table> 
    <tr> 
     <th> 
      ID 
     </th> 
     <th> 
      Name 
     </th> 
    </tr> 


    <tr> 
     <td> 
      <span name="spanId" class="spanclass">ID123</span> 
     </td> 

     <td> 
      <span>Bonny</span> 
     </td> 
    </tr> 
</table> 

codice Python


soup = BeautifulSoup(htmlContent, "lxml") 
soup.prettify() 

tables = soup.find_all("table") 

for table in tables: 
    storeValueRows = table.find_all("tr") 
    thValue = storeValueRows[0].find_all("th")[0].string 

    if (thValue == "ID"): # with this condition I am verifying that this html is correct, that I wanted. 
     value = storeValueRows[1].find_all("span")[0].string 
     value = value.strip() 

     # storeValueRows[1] will represent <tr> tag of table located at first index and find_all("span")[0] will give me <span> tag and '.string' will give me value 

     # value.strip() - will remove space from start and end of the string. 

    # find using attribute : 

    value = storeValueRows[1].find("span", {"name":"spanId"})['class'] 
    print value 
    # this will print spanclass 
2

le seguenti opere:

from bs4 import BeautifulSoup 

soup = BeautifulSoup('<META NAME="City" content="Austin">', 'html.parser') 

metas = soup.find_all("meta") 

for meta in metas: 
    print meta.attrs['content'], meta.attrs['name'] 
Problemi correlati