分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

百度搜索结果HTML分析

发布时间:2023-09-06 01:45责任编辑:蔡小小关键词:HTML

目的:

为了从搜索结果中提取所有网页,以备后续处理。

访问百度链接分析

名称 说明
wd 任意文字 关键字
rn 可以不指定,默认为10,最大为50,最小为1,可设置为任意值 一页包含的结果条目数
pn 百度默认显示760条,所以最后一页为pn=750 第一条结果的索引位置

示例:

https://www.baidu.com/s?wd=老虎&pn=10&rn=3

关键字:老虎,第10条记录,每页显示3条。所以打开的是以老虎为关键字,第四页的记录

HTML源文件分析

刚下载的html源文件格式非常混乱,可使用在线html格式化工具进行格式化,以便阅读。

根据我的需求,在HTML文件中,<script>元素与<style>元素可以直接跳过。找到搜索结果所在的位置即可。

提取搜索结果(QT实现)

在Qt中,使用QDomDocument 或 QXmlStreamReader 来解析 html 文件都失败了。经分析,其原因是:QDomDocument 或 QXmlStreamReader都是针对解析XML文件设计的。HTML与XML的区别

经过查找资料,TidyLib 库正好可以解决问题。

Tidy is a console application for Mac OS X, Linux, Windows, UNIX, and more. It corrects and cleans up HTML and XML documents by fixing markup errors and upgrading legacy code to modern standards. ?

libtidy is a C static and dynamic library that developers can integrate into their applications in order to bring all of Tidy’s power to your favorite tools. libtidy is used today in desktop applications, web servers, and more.

TidyLib使用代码如下:

bool HtmlParse::setDatas(const QByteArray &datas){ ???bool result = false; ???TidyBuffer output = {0}; ???TidyBuffer errbuf = {0}; ???int rc = -1; ???Bool ok; ???TidyDoc tdoc = tidyCreate(); ???????????????????????// Initialize "document" ???ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); ????// Convert to XHTML ???if ( ok ) ????????rc = tidySetErrorBuffer( tdoc, &errbuf ); ?????// Capture diagnostics ???if ( rc >= 0 ) ????????rc = tidyParseString( tdoc, datas.data() ); ???// Parse the input ???if ( rc >= 0 ) ????????rc = tidyCleanAndRepair( tdoc ); ??????????????// Tidy it up! ???if ( rc >= 0 ) ????????rc = tidyRunDiagnostics( tdoc ); ??????????????// Kvetch ???if ( rc > 1 ) ??????????????????????????????????????// If error, force output. ????????rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); ???if ( rc >= 0 ) ????????rc = tidySaveBuffer(tdoc, &output); ???????????// Pretty Print ???if ( rc >= 0 ) ???{ ???????if (doc.setContent(QByteArray((char *)output.bp))) ?// QDomDocument doc; ????????{ ???????????result = true; ???????} ???} ???tidyBufFree( &output ); ???tidyBufFree( &errbuf ); ???tidyRelease( tdoc ); ???return result;}

百度搜索结果HTML分析

原文地址:https://www.cnblogs.com/yang--/p/8576778.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved