getchildren () в коде Beautifulsoup для LXML - PullRequest
0 голосов
/ 28 мая 2018

Я должен преобразовать код BeautifulSoup.По сути, я хочу получить все дочерние элементы узла body, выбрать текст, который им нужен, и сохранить его.Вот код с bs4:

def get_children(self, tag, dorecursive=False):

    children = []
    if not tag :
        return children

    for t in tag.findChildren(recursive=dorecursive):
        if t.name in self.text_containers \
                and len(t.text) > self.min_text_length \
                and self.is_valid_tag(t):
            children.append(t)
    return children

это прекрасно работает, когда я пробую это с lxml lib, потомки пустые:

def get_children(self, tag, dorecursive=False):

    children = []
    if not tag :
        return children

    tags = tag.getchildren()
    for t in tags:
        #print(t.tag)
        if t.tag in self.text_containers \
                and len(t.tail) > self.min_text_length \
                and self.is_valid_tag(t):
            children.append(t)
    return children

любая идея?

1 Ответ

0 голосов
/ 31 мая 2018

Код:

import lxml.html
import requests


class TextTagManager:
    TEXT_CONTAINERS = {
        'li',
        'p',
        'span',
        *[f'h{i}' for i in range(1, 6)]
    }
    MIN_TEXT_LENGTH = 60

    def is_valid_tag(self, tag):
        # put some logic here
        return True

    def get_children(self, tag, recursive=False):
        children = []

        tags = tag.findall('.//*' if recursive else '*')
        for t in tags:
            if (t.tag in self.TEXT_CONTAINERS and
                    t.text and
                    len(t.text) > self.MIN_TEXT_LENGTH and
                    self.is_valid_tag(t)):
                children.append(t)
        return children


manager = TextTagManager()

url = 'https://en.wikipedia.org/wiki/Comparison_of_HTML_parsers'
html = requests.get(url).text
doc = lxml.html.fromstring(html)

for child in manager.get_children(doc, recursive=True):
    print(child.tag, ' -> ', child.text)

Выход:

li  ->  HTML traversal: offer an interface for programmers to easily access and modify of the "HTML string code". Canonical example: 
li  ->  HTML clean: to fix invalid HTML and to improve the layout and indent style of the resulting markup. Canonical example: 

.getchildren() возвращает все прямые дочерние элементы.Если вы хотите иметь рекурсивную опцию, вы можете использовать .findall():

tags = tag.findall('.//*' if recursive else '*')

Этот ответ должен помочь вам понять разницу между .//tag иtag.

...