簡體   English   中英

A內的Gumbo HTML文字

[英]Gumbo HTML text inside A

我正在使用Gumbo解析CP1251的網頁。 我已將文本轉換為UTF-8 ,並將其發送到gumbo解析器。 我有問題,讓里面的文字A帶鏈接

node->v.text.text

當源正確顯示在控制台中時,輸出上出現奇怪的符號。 我正在使用Qt 5.2libiconv進行轉換。

我需要將節點文本轉換為本地代碼頁還是我做錯了什么?

CP1251獲取頁面

    QByteArray barrData = pf->getData();

    size_t dstlen = 1048576;
    char buf[dstlen];
    memset((char*)buf, 0, dstlen);

    char* pIn = barrData.data();
    char* pOut = (char*)buf;

    size_t srclen = barrData.size();


    iconv_t conv = iconv_open("UTF-8", "CP1251");
    iconv(conv, &pIn, &srclen, &pOut, &dstlen);
    iconv_close(conv);

    GumboOutput* output = gumbo_parse(buf);

    parsePage(output->root);
    gumbo_destroy_output(&kGumboDefaultOptions, output);

解析

if (node->v.element.tag == GUMBO_TAG_DIV && (_class = gumbo_get_attribute(&node->v.element.attributes, "class")))
{
    if (QString(_class->value) == "catalog-item-title")
    {
        qDebug() << "parsePage: found product, parsing...";

        GumboVector* children = &node->v.element.children;
        for (int i = 0; i < children->length; ++i)
        {
            GumboNode* node = static_cast<GumboNode*>(children->data[i]);

            GumboAttribute* href;
            GumboAttribute* id;

            if (node->v.element.tag == GUMBO_TAG_A &&
                (href = gumbo_get_attribute(&node->v.element.attributes, "href"))
            )
            {
                char buf[1024];
                memset(buf, 0, 1024);
                int i = node->v.text.original_text.length;
                memcpy(buf, node->v.text.original_text.data, i);


                QString strTitle = buf;
                Q_ASSERT(node->v.text.original_text.length > 0);
                qDebug() << "parsePage: found product" << strTitle << href->value;

                break;
            }
        }
    }
}

源頁面文本:

<div class="catalog-item-title"><a href="/textile/postelnoe-bele/korolevskoe-iskushenie-perkal/izmir_2/">Измир 2</a></div>

我終於抽了一些例子。 文本包含在子節點內。

            if (node->v.element.tag == GUMBO_TAG_A &&
                (href = gumbo_get_attribute(&node->v.element.attributes, "href"))
            )
            {
                QString strTitle;
                GumboNode* title_text = static_cast<GumboNode>*)(node->v.element.children.data[0]);
                if (title_text->type == GUMBO_NODE_TEXT)
                {
                    strTitle = title_text->v.text.text;
                }

                qDebug() << "parsePage: found product" << strTitle << href->value;

                break;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM