Как найти тег комментария с BeautifulSoup?

Кроме того, обратите внимание, что при использовании встроенных шаблонов для добавления классов к папке, она будет по умолчанию помещена в пространство имен, которое отражает иерархию папок.

классы будет легче найти и что один должны быть достаточно хорошие причины.

правила, за которыми мы следуем:

  • Проект/имя сборки совпадает с корневым пространством имен, за исключением .dll окончание
  • , Только исключением к вышеупомянутому правилу является проект с окончанием.Core.Core разделяется прочь
  • , Папки равняются пространствам имен
  • , Один тип на файл (класс, структура, перечисление, делегат, и т.д.) облегчает находить правильный файл
17
задан 1stsage 19 May 2011 в 17:34
поделиться

1 ответ

Pyparsing позволяет вам искать комментарии HTML, используя встроенное выражение htmlComment, и прикреплять обратные вызовы времени анализа для проверки и извлечения различных полей данных в комментарии:

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
ответ дан 30 November 2019 в 13:26
поделиться
Другие вопросы по тегам:

Похожие вопросы: