2012-06-26 11 views
54

मैं अपने नाम के आधार पर एक विशेषता मान मुद्रित करने के लिए चाहते हैं, उदाहरण के लिए लेपायथन: BeautifulSoup - नाम विशेषता के आधार पर एक विशेषता मान

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

मैं इस

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'] 
की तरह कुछ करना चाहता हूँ

उपरोक्त कोड KeyError: 'name' देता है, मेरा मानना ​​है कि ऐसा इसलिए है क्योंकि नाम BeatifulSoup द्वारा उपयोग किया जाता है, इसलिए इसे कीवर्ड तर्क के रूप में उपयोग नहीं किया जा सकता है।

>>> 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' 

एक टिप्पणी छोड़ दो अगर कुछ भी स्पष्ट नहीं है -

उत्तर

84

यह बहुत आसान है, निम्नलिखित का उपयोग करें।

+0

कैसे मैं यह कर सकता अगर मैं सभी उदाहरणों खोजना चाहते हैं, यानी अभी, soup.find ("मेटा", { "नाम": "शहर"}) ['सामग्री'] पहला परिणाम देता है, लेकिन कहें कि सूप में एक और पंक्ति थी जो <मेटा NAME = 'सिटी "सामग्री =" सैन फ्रांसिस्को "> थी। मैं कोड को कैसे संशोधित कर सकता हूं ताकि मुझे 'ऑस्टिन' और 'सैन फ्रांसिस्को' मिल सके? – overflowname

+0

पुराना सवाल, लेकिन अगर कोई और इसके लिए देख रहा है तो यहां एक आसान समाधान है: 'soup.findAll (" मेटा ", {" नाम ":" शहर "}) ['content']'। यह सभी घटनाओं को वापस कर देगा। –

6

सबसे पुराना जवाब सबसे अच्छा समाधान है, लेकिन एफवाईआई जो समस्या आप सामना कर रहे थे, उसे इस तथ्य के साथ करना है कि सुंदर सूप में एक टैग ऑब्जेक्ट पाइथन शब्दकोश की तरह कार्य करता है। यदि आप उस टैग पर टैग ['name'] एक्सेस करते हैं जिसमें 'नाम' विशेषता नहीं है, तो आपको एक KeyError मिल जाएगा।

12

सबसे पुराना प्रश्न का उत्तर दिया लेकिन यह वही काम करने का एक और तरीका है। इसके अलावा, आपके उदाहरण में आपके पास NAME में कैप्स हैं और आपके कोड में आपके पास लोअरकेस में नाम है।

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

मामले में विसंगति शायद जानबूझकर है क्योंकि सुंदर सूप टैग को डिफ़ॉल्ट रूप से लोअरकेस में परिवर्तित करता है। इस मामले में: सुंदर सूप ('<मेटा नाम = "शहर" सामग्री = "ऑस्टिन">') लौटाता है <मेटा सामग्री = "ऑस्टिन" नाम = "शहर" /> – tuckermi

0

एक भी इस समाधान की कोशिश कर सकते हैं:

मूल्य है, जो मेज

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> 

अजगर कोड की अवधि में लिखा है पता करने के लिए


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

निम्नलिखित काम करता है:

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'] 
संबंधित मुद्दे