使用Qt Creator从互联网页面的源代码中提取信息(Extract an information from the source code of an internet page with Qt

编程入门 行业动态 更新时间:2024-10-26 07:36:30
使用Qt Creator从互联网页面的源代码中提取信息(Extract an information from the source code of an internet page with Qt Creator)

我正在做一个小项目,这就是我要做的事情:

我使用Qt ui和webView小部件,我想从网页的源代码中提取特定信息。 这是组件的订购代码。 在这个例子中,我要提取的部分是1758990:

<div id="productDescription"> <div class="brandLogo"> <a href="http://uk.farnell.com/multicomp"> <img id="supplier_logo" src="http://uk.farnell.com/productimages/promo/en_GB/2217096.jpg" alt="MULTICOMP"/> </a> </div> <ul> <li> <strong>Manufacturer:</strong> <a href="http://uk.farnell.com/multicomp"> MULTICOMP </a> </li> <li> <strong>Order Code:</strong> 1758990 </li> <li> <strong>Manufacturer Part No</strong> MC0402B821K500CT </li> </ul> <ul id="technicalData"> <li> <a href="http://www.farnell.com/datasheets/1859359.pdf" target="_blank" ><span class="pfIcon pfPdf" title="Technical Data Sheet (877.82KB) EN">&nbsp;</span>Technical Data Sheet (877.82KB) EN</a> </li> </ul> </div>

我想使用这个订单代码并将其放在一个变量中。 其余的我会管理,但我找不到正确的工具来找到代码(这是永远不会相同)。 我设法使用toHtml()函数从页面中提取整个源代码,并尝试将其导出到textEdit但我真的不知道它可能有什么用处。 我该怎么用?

先谢谢你。

(对不起,我很擅长提问,所以请原谅我,如果我提出写作惯例)

编辑:我正在尝试使用findFirstElement()函数,但我找不到正确的查询只提取1758990.我可以使用findFirstElement提取“订单代码:1758990”(“#productDescription li:nth-​​child(2) “)但我只想要订单代码:。(

I'm doing a little project and here's what I'm trying to do :

I use the Qt ui with a webView widget and I would like to extract a specific information from the source code of a web page. It's the Order Code of a component. The part I would like to extract is 1758990 in this example:

<div id="productDescription"> <div class="brandLogo"> <a href="http://uk.farnell.com/multicomp"> <img id="supplier_logo" src="http://uk.farnell.com/productimages/promo/en_GB/2217096.jpg" alt="MULTICOMP"/> </a> </div> <ul> <li> <strong>Manufacturer:</strong> <a href="http://uk.farnell.com/multicomp"> MULTICOMP </a> </li> <li> <strong>Order Code:</strong> 1758990 </li> <li> <strong>Manufacturer Part No</strong> MC0402B821K500CT </li> </ul> <ul id="technicalData"> <li> <a href="http://www.farnell.com/datasheets/1859359.pdf" target="_blank" ><span class="pfIcon pfPdf" title="Technical Data Sheet (877.82KB) EN">&nbsp;</span>Technical Data Sheet (877.82KB) EN</a> </li> </ul> </div>

I would like to use this order code and put it in a variable. The rest I would manage but I can't find the correct tools to find the code (which is never the same). I managed to extract the entire source code from the page with a toHtml() function and tried to export it to a textEdit but I don't really know what use it could have. What should I use ?

Thank you in advance.

(also I'm sorry, I'm quite new at asking questions so forgive me if I f.. up the writing conventions)

EDIT: I'm trying to use the findFirstElement() function but I can't find the correct query to extract only 1758990. I can extract "Order Code: 1758990" with findFirstElement("#productDescription li:nth-child(2)") but I only want the order Code :.(

最满意答案

好吧,我找到了一种方法(当然不是最干净的但是......)

我有一个按钮来检索代码:

void browser::on_Addtodbbtn_clicked() { QWebElement code = ui->webView->page()->mainFrame()->findFirstElement("#productDescription li:nth-child(2)"); QString OrderCode = code.toPlainText(); // at this point, OrderCode = "Order Code: 1758990" OrderCode.remove("Order Code: "); // I remove the part I don't want qDebug() << "OrderCode:" << OrderCode; // Here you go }

这页帮助了我很多: http : //www.w3schools.com/cssref/css_selectors.asp

Okay I found a way (surely not the cleanest one but well...)

I have a button to retrieve the code :

void browser::on_Addtodbbtn_clicked() { QWebElement code = ui->webView->page()->mainFrame()->findFirstElement("#productDescription li:nth-child(2)"); QString OrderCode = code.toPlainText(); // at this point, OrderCode = "Order Code: 1758990" OrderCode.remove("Order Code: "); // I remove the part I don't want qDebug() << "OrderCode:" << OrderCode; // Here you go }

this page helped me a lot: http://www.w3schools.com/cssref/css_selectors.asp

更多推荐

本文发布于:2023-07-30 06:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   源代码   页面   信息   Creator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!