96SEO 2026-02-19 06:53 0
二、列表页智能解析算法三、智能分辨列表页和详细页四、完整的库4.1

代码中提取想要的内容算法会自动计算出目标内容在代码中的位置并将他们提取出来
对于资讯类网站除去一些特殊的页面如登入页面注册页面等剩下页面可以分为两大类——列表页和详细页前者提供多个详细页的索引导航信息后者则包含具体的内容针对这两类页面有以下几点需要解决
详细页中文章标题正文发布事件和提取算法的实现列表页中链接列表的提取算法和实现如何判断一个页面是详细页还是列表页
详细页是某个内容的展示页面通常包含醒目的标题发布事件和占据版面最大的正文部分。
另外详细页的侧栏通常会有一些关联或推荐的内容列表页面头部的导航链接评论区广告区等
一般来说详细页包含的信息非常多例如标题发布时间发布来源作者正文封面图评论数目评论内容等不过由于其中一些内容并不常用而且提取算法大同小异因此这里主要对三个信息进行提取标题正文和发布时间
渲染而成的因此通过请求获取的页面源代码不一定是在浏览器中看到的页面源代码因此解析的前提是要求我们提取的必须是渲染完整的
节点的文本做比对和后者相似度最高的内容很可能就是详细页的标题如果未在页面中找到
.join(element.xpath(//title//text())).strip()
element.xpath(//h1//text()|//h2//text()|//h3//text())
节点也不一定全是正文内容可能掺杂噪声如网站的版权信息发布人文末广告等这些都属于噪声正文内容所在的
等节点这些内容大部分属于正文中的特殊样式字符往往也需要归类到正文内容之中
基于文本及符号密度的网页正文提取方法的启发得到了两个比较有效的正文文本提取依据指标——文本密度和符号密度
文本密度不局限于纯文本和节点的大小比例还考虑到了文本中包含的超链接论文中定义如果
正文中一般会带标点符号而网页链接广告信息由于文字较少通常是不包含标点符号的因此还可以借助符号密度排除一些内容节点
论文的作者经过多次实验。
利用文本密度和符号密度相结合的方式提取正文信息能取得很不错的效果可以结合两者为每个节点分别计算一个分数分数最高的节点就为正文内容所在的节点分数的计算公式如下
ScoreilnSD×TDi×lg(PNumi2)×lnSbDi
因为没有了噪声和干扰数据变得比较规整了下一步来实现文本密度符号密度和最终分数的计算
HtmlElement包含很多字段代表某个节点的信息例如文本密度符号密度等Element
的定义GerapyAutoExtractor/gerapy_auto_extractor/schemas/element.py
None_a_descendants_group_text_length:
None_a_descendants_group_text_min_length:
None_a_descendants_group_text_max_length:
len(list(self.itersiblings(precedingTrue)))
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
self._parent_selectorpropertydef
gerapy_auto_extractor.utils.element
methodhtml).decode(utf-8)propertydef
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
number_of_charself._number_of_char
self._number_of_charpropertydef
number_of_a_descendants(self):get
self._number_of_a_descendantsfrom
gerapy_auto_extractor.utils.element
number_of_a_descendantsself._number_of_a_descendants
number_of_a_descendants(self)return
self._number_of_a_descendantspropertydef
gerapy_auto_extractor.utils.element
number_of_a_charself._number_of_a_char
self._number_of_a_charpropertydef
number_of_p_descendants(self):return
self._number_of_p_descendantsfrom
gerapy_auto_extractor.utils.element
number_of_p_descendantsself._number_of_p_descendants
number_of_p_descendants(self)return
self._number_of_p_descendantspropertydef
number_of_punctuation(self):get
self._number_of_punctuationfrom
gerapy_auto_extractor.utils.element
number_of_punctuationself._number_of_punctuation
number_of_punctuation(self)return
self._number_of_punctuationpropertydef
self._number_of_childrenself._number_of_children
self._number_of_childrenpropertydef
self._number_of_siblingsself._number_of_siblings
self._number_of_siblingspropertydef
number_of_descendants(self):get
self._number_of_descendantsfrom
gerapy_auto_extractor.utils.element
number_of_descendantsself._number_of_descendants
len(list(self.descendants))return
self._number_of_descendantspropertydef
density_of_punctuation(self):get
self._density_of_punctuationfrom
gerapy_auto_extractor.utils.element
density_of_punctuationself._density_of_punctuation
density_of_punctuation(self)return
self._density_of_punctuationpropertydef
gerapy_auto_extractor.utils.element
density_of_textself._density_of_text
self._density_of_textpropertydef
similarity_with_siblings(self):get
self._similarity_with_siblingsfrom
gerapy_auto_extractor.utils.element
similarity_with_siblingsself._similarity_with_siblings
similarity_with_siblings(self)return
self._similarity_with_siblingspropertydef
gerapy_auto_extractor.utils.element
a_descendantsself._a_descendants
gerapy_auto_extractor.utils.element
a_descendants_groupself._a_descendants_group
a_descendants_group(self)return
self._a_descendants_grouppropertydef
a_descendants_group_text_length(self):grouped
self._a_descendants_group_text_length
self._a_descendants_group_text_lengthresult
gerapy_auto_extractor.utils.element
self.a_descendants_group.items():lengths
element.number_of_charlengths.append(len(text(element)))mean_length
a_descendants_group_text_min_length(self):get
self._a_descendants_group_text_min_length
self._a_descendants_group_text_min_lengthvalues
self.a_descendants_group_text_length.values()self._a_descendants_group_text_min_length
self._a_descendants_group_text_min_lengthpropertydef
a_descendants_group_text_max_length(self):get
self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthvalues
self.a_descendants_group_text_length.values()self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthpropertydef
a_descendants_group_text_avg_length(self):get
self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthvalues
self.a_descendants_group_text_length.values()self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthdef
preprocesspreprocess4content(element)#
descendants]density_of_text_std
\np.log10(descendant.number_of_p_descendants
\np.log(descendant.density_of_punctuation)descendant.density_score
descendant_first.xpath(.//p//text())paragraphs
节点的信息提取时间提取结果大概率就是真实的发布事件可信度较高根据正则表达式提取时间如果匹配到一些置信度比较高的规则那么可以直接提取如果匹配到置信度不高的规则或者提取到多个事件信息则可以进行下一步的提取和筛选通过计算节点和正文的距离再结合其他相关信息筛选出最有节点作为结果
rnews:datePublished)]/content,//meta[starts-with(property,
article:published_time)]/content,//meta[starts-with(property,
og:published_time)]/content,//meta[starts-with(property,
og:release_date)]/content,//meta[starts-with(itemprop,
datePublished)]/content,//meta[starts-with(itemprop,
dateUpdate)]/content,//meta[starts-with(name,
OriginalPublicationDate)]/content,//meta[starts-with(name,
article_date_original)]/content,//meta[starts-with(name,
og:time)]/content,//meta[starts-with(name,
apub:time)]/content,//meta[starts-with(name,
publication_date)]/content,//meta[starts-with(name,
sailthru.date)]/content,//meta[starts-with(name,
PublishDate)]/content,//meta[starts-with(name,
publishdate)]/content,//meta[starts-with(name,
PubDate)]/content,//meta[starts-with(name,
pubtime)]/content,//meta[starts-with(name,
_pubtime)]/content,//meta[starts-with(name,
article:create_at)]/content,//meta[starts-with(pubdate,
rnews:datePublished)],//meta[starts-with(property,
article:published_time)],//meta[starts-with(property,
og:published_time)],//meta[starts-with(property,
og:release_date)],//meta[starts-with(itemprop,
datePublished)],//meta[starts-with(itemprop,
dateUpdate)],//meta[starts-with(name,
OriginalPublicationDate)],//meta[starts-with(name,
article_date_original)],//meta[starts-with(name,
og:time)],//meta[starts-with(name,
apub:time)],//meta[starts-with(name,
publication_date)],//meta[starts-with(name,
sailthru.date)],//meta[starts-with(name,
PublishDate)],//meta[starts-with(name,
publishdate)],//meta[starts-with(name,
PubDate)],//meta[starts-with(name,
pubtime)],//meta[starts-with(name,
_pubtime)],//meta[starts-with(name,
article:create_at)],//meta[starts-with(pubdate,
[(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]),(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]),(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时),(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]),(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]),(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时),(\d{4}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{4}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{4}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]),(\d{4}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]),(\d{4}年\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时),(\d{2}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{2}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{2}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]),(\d{2}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]),(\d{2}年\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时),(\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9]),(\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]),(\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]),(\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时),(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}),(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}),(\d{4}年\d{1,2}月\d{1,2}日),(\d{2}年\d{1,2}月\d{1,2}日),(\d{1,2}月\d{1,2}日)
.join(element.xpath(.//text()))
extract_by_regex(element)二、列表页智能解析算法
列表页包含一个个详细页的标题和链接点击其中某个链接就可以进入对应的详细页列表页页面主要区域里的列表都很醒目
列表页解析的目标是从当前列表页中把详细页的标题和链接提取出来并以列表的形式返回
来排列的因此我们需要找一个通用的提取模式可以观察得到列表中的标题通常是一组一组呈现的如果进观察一组可以发现组内包含多个连续并列的兄弟节点如果我们把这些连续并列的兄弟节点作为寻找目标就可以得到这样一个通用的规律
“成员节点”目标组节点和其他组节点最明显不同之处在于字数因此我们需要规定成员节点的最小平均字符数同时对于多个目标组节点我们可以通过合并的方式变为一个组节点再来提取
HtmlElement包含很多字段代表某个节点的信息例如文本密度符号密度等Element
的定义GerapyAutoExtractor/gerapy_auto_extractor/schemas/element.py
None_a_descendants_group_text_length:
None_a_descendants_group_text_min_length:
None_a_descendants_group_text_max_length:
len(list(self.itersiblings(precedingTrue)))
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
self._parent_selectorpropertydef
gerapy_auto_extractor.utils.element
methodhtml).decode(utf-8)propertydef
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
gerapy_auto_extractor.utils.element
number_of_charself._number_of_char
self._number_of_charpropertydef
number_of_a_descendants(self):get
self._number_of_a_descendantsfrom
gerapy_auto_extractor.utils.element
number_of_a_descendantsself._number_of_a_descendants
number_of_a_descendants(self)return
self._number_of_a_descendantspropertydef
gerapy_auto_extractor.utils.element
number_of_a_charself._number_of_a_char
self._number_of_a_charpropertydef
number_of_p_descendants(self):return
self._number_of_p_descendantsfrom
gerapy_auto_extractor.utils.element
number_of_p_descendantsself._number_of_p_descendants
number_of_p_descendants(self)return
self._number_of_p_descendantspropertydef
number_of_punctuation(self):get
self._number_of_punctuationfrom
gerapy_auto_extractor.utils.element
number_of_punctuationself._number_of_punctuation
number_of_punctuation(self)return
self._number_of_punctuationpropertydef
self._number_of_childrenself._number_of_children
self._number_of_childrenpropertydef
self._number_of_siblingsself._number_of_siblings
self._number_of_siblingspropertydef
number_of_descendants(self):get
self._number_of_descendantsfrom
gerapy_auto_extractor.utils.element
number_of_descendantsself._number_of_descendants
len(list(self.descendants))return
self._number_of_descendantspropertydef
density_of_punctuation(self):get
self._density_of_punctuationfrom
gerapy_auto_extractor.utils.element
density_of_punctuationself._density_of_punctuation
density_of_punctuation(self)return
self._density_of_punctuationpropertydef
gerapy_auto_extractor.utils.element
density_of_textself._density_of_text
self._density_of_textpropertydef
similarity_with_siblings(self):get
self._similarity_with_siblingsfrom
gerapy_auto_extractor.utils.element
similarity_with_siblingsself._similarity_with_siblings
similarity_with_siblings(self)return
self._similarity_with_siblingspropertydef
gerapy_auto_extractor.utils.element
a_descendantsself._a_descendants
gerapy_auto_extractor.utils.element
a_descendants_groupself._a_descendants_group
a_descendants_group(self)return
self._a_descendants_grouppropertydef
a_descendants_group_text_length(self):grouped
self._a_descendants_group_text_length
self._a_descendants_group_text_lengthresult
gerapy_auto_extractor.utils.element
self.a_descendants_group.items():lengths
element.number_of_charlengths.append(len(text(element)))mean_length
a_descendants_group_text_min_length(self):get
self._a_descendants_group_text_min_length
self._a_descendants_group_text_min_lengthvalues
self.a_descendants_group_text_length.values()self._a_descendants_group_text_min_length
self._a_descendants_group_text_min_lengthpropertydef
a_descendants_group_text_max_length(self):get
self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthvalues
self.a_descendants_group_text_length.values()self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthpropertydef
a_descendants_group_text_avg_length(self):get
self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthvalues
self.a_descendants_group_text_length.values()self._a_descendants_group_text_max_length
self._a_descendants_group_text_max_lengthdef
self.__str__()最后定义一个聚类方式聚类信息然后提取
similarity_thresholdSIMILARITY_THRESHOLD):
_probability_of_title_with_length(self,
list_extractor.probability_of_title_with_length(x)
descendant.a_descendants_group_text_min_length
descendant.a_descendants_group_text_max_length
descendant.similarity_with_siblings
descendants_tree[descendant.parent_selector].append(descendant)
sorted(list(descendants_tree.keys()))
last_selector.startswith(selector):
score[avg_similarity_with_siblings]
[element.similarity_with_siblings
score[probability_of_title_with_length]
self._probability_of_title_with_length(len(a_descendant.text))
itertools.chain(*[element.a_descendants
score[avg_similarity_with_siblings]
np.log10(score[number_of_elements]
clusters_score[cluster_id][probability_of_title_with_length]
result.append(sibling_selector)
self._evaluate_cluster(cluster)
clusters_score[cluster_id][clusters_score]
clusters_score[cluster_id][clusters_score]
clusters[clusters_score_arg_max]
probability_of_title_with_length
self._probability_of_title_with_length(len(descendant_text))
probability_of_title_with_descendants
self.probability_of_title_with_descendants(descendant)
probability_of_title_with_length
probabilities_of_title[path].append(probability_of_title)
probabilities_of_title.items()}
max(probabilities_of_title_avg.items(),
preprocess4list_extractor(element)
self._extend_cluster(best_cluster)
self._extract_cluster(best_cluster)
节点内部就包含几十上百个文字如果用单个节点内的文字数目来表示文本密度的话那么详情页的部分内容文本密度会很高。
超链接节点的数量和比例一般来说列表页通常会包含多个超链接而且很大比例都是超链接文本而详情页却有很多的文字并不是超链接比如正文。
符号密度一般来说列表页通常会是一些标题导航一般可能都不会包含句号而详情页的正文内容通常就会包含句号等内容如果按照单位文字所包含的标点符号数量来表示符号密度的话后者的符号密度也会高一些。
列表簇的数目一般来说列表页通常会包含多个具有公共父节点的条目多个条目构成一个列表簇虽然说详情页侧栏也会包含一些列表但至少这个数量也可以成为一个特征来判别。
meta
信息是列表页独有的比如只有详情页才会有发布时间而列表页通常是没有的。
正文标题和
self._max_number_of_p_children,
self.feature_funcs.keys()以上方法就是特征和对应的获取方法具体根据实际情况实现即可。
list(glob(f{DATASETS_LIST_DIR}/*.html))
list(glob(f{DATASETS_DETAIL_DIR}/*.html))x_data,
enumerate(list_file_paths):logger.log(inspect,
None:continuepreprocess4list_classifier(element)x
self.features_to_list(self.features(element))x_data.append(x)y_data.append(1)for
enumerate(detail_file_paths):logger.log(inspect,
file2element(detail_file_path)if
None:continuepreprocess4list_classifier(element)x
self.features_to_list(self.features(element))x_data.append(x)y_data.append(0)#
GridSearchCV(SVC(probabilityTrue),
fn{classification_report(y_true,
joblib.dump(grid.best_estimator_,
self.model_path)这里首先对数据进行预处理然后将每个
GeneralNewsExtractor/GeneralNewsExtractor:
(github.com)Gerapy/GerapyAutoExtractor:
面向不规则列表的网页数据抽取技术的研究基于文本及符号密度的网页正文提取方法基于块密度加权标签路径特征的Web新闻在线抽取基于DOM树和视觉特征的网页信息自动抽取
GeneralNewsExtractorReadability
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback