2011-05-19 12 views
17

मैंने सूप की कोशिश की। '(!! -') लेकिन यह काम नहीं कर रहा है। अग्रिम में धन्यवाद।सुंदर सूप के साथ टिप्पणी टैग <!--...--> कैसे खोजें?

संपादित करें: सभी टिप्पणियों को कैसे ढूंढें इस पर टिप के लिए धन्यवाद। मेरे पास एक फॉलो अप प्रश्न है। मैं विशेष रूप से एक टिप्पणी के लिए कैसे खोज करूं?

<!-- <span class="titlefont"> <i>Wednesday 110518</i>(05:00PM)<br /></span> -->

मैं वास्तव में सिर्फ इस सामान <i>Wednesday 110518</i> हैं:

उदाहरण के लिए, मैं निम्नलिखित टिप्पणी टैग की है। "110518" दिनांक वाई वाईएमएमडीडी है जो मैं अपने खोज लक्ष्य के रूप में उपयोग करने पर झुका रहा हूं। हालांकि, मुझे नहीं पता कि एक विशिष्ट टिप्पणी टैग में कुछ कैसे ढूंढें।

उत्तर

0

Pyparsing आप अंतर्निहित htmlComment अभिव्यक्ति का उपयोग करके HTML टिप्पणी के लिए खोज करने के लिए अनुमति देता है, और संलग्न पार्स समय कॉलबैक मान्य करने के लिए और टिप्पणी के भीतर विभिन्न डेटा फ़ील्ड निकालें:

from pyparsing import makeHTMLTags, oneOf, withAttribute, Word, nums, Group, htmlComment 
import calendar 

# have pyparsing define tag start/end expressions for the 
# tags we want to look for inside the comments 
span,spanEnd = makeHTMLTags("span") 
i,iEnd = makeHTMLTags("i") 

# only want spans with class=titlefont 
span.addParseAction(withAttribute(**{'class':'titlefont'})) 

# define what specifically we are looking for in this comment 
weekdayname = oneOf(list(calendar.day_name)) 
integer = Word(nums) 
dateExpr = Group(weekdayname("day") + integer("daynum")) 
commentBody = '<!--' + span + i + dateExpr("date") + iEnd 

# define a parse action to attach to the standard htmlComment expression, 
# to extract only what we want (or raise a ParseException in case 
# this is not one of the comments we're looking for) 
def grabCommentContents(tokens): 
    return commentBody.parseString(tokens[0]) 
htmlComment.addParseAction(grabCommentContents) 


# let's try it 
htmlsource = """ 
want to match this one 
<!-- <span class="titlefont"> <i>Wednesday 110518</i>(05:00PM)<br /></span> --> 

don't want the next one, wrong span class 
<!-- <span class="bodyfont"> <i>Wednesday 110519</i>(05:00PM)<br /></span> --> 

not even a span tag! 
<!-- some other text with a date in italics <i>Wednesday 110520</i>(05:00PM)<br /></span> --> 

another matching comment, on a different day 
<!-- <span class="titlefont"> <i>Thursday 110521</i>(05:00PM)<br /></span> --> 
""" 

for comment in htmlComment.searchString(htmlsource): 
    parsedDate = comment.date 
    # date info can be accessed like elements in a list 
    print parsedDate[0], parsedDate[1] 
    # because we named the expressions within the dateExpr Group 
    # we can also get at them by name (this is much more robust, and 
    # easier to maintain/update later) 
    print parsedDate.day 
    print parsedDate.daynum 
    print 

प्रिंटों:

Wednesday 110518 
Wednesday 
110518 

Thursday 110521 
Thursday 
110521 
+0

पाइपरिंग के नवीनतम संस्करण में 'withAttribute' ugliness को सरल बनाने के लिए अब' withClass' शामिल है। – PaulMcG

23

आप findAll विधि के माध्यम से किसी दस्तावेज़ में सभी टिप्पणियां पा सकते हैं। इस उदाहरण है कि कैसे आप Removing elements करने के लिए कोशिश कर रहे हैं वास्तव में क्या करना दिखा देखें:

comments = soup.findAll(text=lambda text:isinstance(text, Comment)) 

संपादित करें::

संक्षेप में, आप इस चाहते हैं आप कॉलम के खोज करने के लिए कोशिश कर रहे हैं, तो आप कर सकते हैं प्रयास करें:

import re 
comments = soup.findAll(text=lambda text:isinstance(text, Comment)) 
for comment in comments: 
    e = re.match(r'<i>([^<]*)</i>', comment.string).group(1) 
    print e 
+0

कैसे एक विशिष्ट टिप्पणी के लिए खोज के बारे में? मैं इसे HTML फ़ाइल में खोजने की कोशिश कर रहा हूं: बुधवार 110518 (05:00 अपराह्न)
-> 110518 पर ध्यान दें, यह केवल yymmdd की तारीख है , मैं उस टिप्पणी टैग के भीतर केवल जानकारी के लिए कैसे खोज सकता हूं, और विशेष रूप से केवल के भीतर? – 1stsage

+0

@ 1stsage शायद आप उस आवश्यकता को अपने प्रश्न में जोड़ना चाहते हैं। –

+0

1stsage, आपके विशिष्ट मामले के लिए मेरी पोस्ट अपडेट की गई। अगली बार, सुनिश्चित करें कि आपका प्रश्न उस कार्य को शामिल करता है जिसे आप करने का प्रयास कर रहे हैं। – yan

संबंधित मुद्दे