如何写所有< p>标签到文本文件 [英] how to write all the <p> tags to text file

查看:81
本文介绍了如何写所有< p>标签到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写Qt / C ++代码来提取所有的p标签,将每个p标签写入.txt文件,例如,如果我有以下HTML页面:

I need to write Qt/C++ code to extract all the p tags to write each p tag to .txt file, For example if I have the following HTML page:

        <!DOCTYPE html>
        <html>
         <body>

         <h1>My First Heading</h1>

         <p>My first paragraph.</p>
         <p>My second paragraph.</p>

         </body>
          </html>

我需要代码创建2个.txt文件,第一个将包括我的第一段。第二个将包括我的第二段。

I need the code to create 2 .txt file the first one will include My first paragraph. and the second will include My second paragraph.

我的问题如何解析html并获取标签之间的txt,这里是我的代码

my problem how to parse the html and get the txt between the tags, here my code

         int main(int argc, char *argv[])
          {
            QCoreApplication a(argc, argv);

           QEventLoop loop;

            QNetworkRequest request;
             request.setUrl(QUrl("http://en.wikipedia.org/wiki/Cars"));
               QNetworkAccessManager* networkMgr = new QNetworkAccessManager();
                QNetworkReply* reply = networkMgr->get(request);

             QObject::connect(reply, SIGNAL(finished()),&loop,SLOT(quit()));

                        loop.exec();

                 QFile file ("/Users/David/Desktop/text123.txt");
                   file.open(QIODevice::WriteOnly);
                   file.write(reply->readAll());

                         delete reply;

                   return a.exec();
                     }

非常感谢您的帮助


  1. 列出项目


推荐答案

请使用QRegularExpression,如下所示。

you can use QRegularExpression for this see example below.

QString txt = reply->readAll();
QRegularExpression regex("< *[pP] *>(.*)< *\\/ *[pP] *>");
QRegularExpressionMatchIterator it = regex.globalMatch(txt);
int i = 0;
while(it.hasNext())
{
    QRegularExpressionMatch match = it.next();
    QString filename = QString("e:/folder/file%1.txt").arg(i);
    QFile file (filename);
    file.open(QIODevice::WriteOnly);
    file.write(match.captured(1).toUtf8());
    file.close();
    ++i;
}

这篇关于如何写所有&lt; p&gt;标签到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆