Python 使用lxml获取divs HTML内容

Python 使用lxml获取divs HTML内容,python,lxml,Python,Lxml,我使用python和lxml从大量链接中获取div.article的内容。我想要div的实际html标记。但到目前为止,我只能得到去掉标记的链接的文本内容() doc = html.fromstring(doc_text) article = doc.cssselect("div.article") if len(article) > 0: text = article[0].text_content() data = { 'product':prod

我使用python和lxml从大量链接中获取div.article的内容。我想要div的实际html标记。但到目前为止,我只能得到去掉标记的链接的文本内容()

doc = html.fromstring(doc_text)

article = doc.cssselect("div.article")

if len(article) > 0:
    text = article[0].text_content()

    data = {
        'product':product,
        'content': text,
    }
有人能帮我获取文章[0]的标记吗


谢谢

您只需使用节点的迭代功能,并以这种方式构建字符串即可

def innerHTML(node): 
    buildString = ''
    for child in node:
        buildString += html.tostring(child)
    return buildString

您只需使用节点的迭代特性,并以这种方式构建字符串

def innerHTML(node): 
    buildString = ''
    for child in node:
        buildString += html.tostring(child)
    return buildString