网页设计教程免费网站,html静态页面,淘宝网站建设方案毕业设计,115做网站场景
我有一个 页面如下#xff08;随便找的#xff09;#xff1a;
我的需求是拿到所有回答的链接#xff0c; 再或者我在找房子网上#xff0c;爬到所有的房产信息#xff0c;我们并不想做过多的处理#xff0c;我只要告诉程序#xff0c;请帮我爬一个类似 xxx 相似…场景
我有一个 页面如下随便找的
我的需求是拿到所有回答的链接 再或者我在找房子网上爬到所有的房产信息我们并不想做过多的处理我只要告诉程序请帮我爬一个类似 xxx 相似度为0.5的就可以了然后我自会写一小段代码去给数据清洗这就免去了每次不同网站写不同的一套脚本的痛苦。这里就用到了 余弦相似度。
余弦相似度
余弦相似度又称为余弦相似性是通过测量两个向量的夹角的余弦值来度量它们之间的相似性。两个方向完全相同的向量的余弦相似度为1而两个彼此相对的向量的相似度为-1。 注意它们的大小并不重要因为这是方向的度量。
余弦定理 所以余弦的计算公式如下 有向量 a,b 他们的余弦值的公式是 这个可能和k近邻算法听起来有些相似。但是也有不同
余弦相似度通常用于计算两个向量间的相似度尤其常见于文本处理中。它通过测量两个向量间夹角的余弦值来判断它们的相似度。而k近邻算法是一种基于实例的学习或非泛化学习它不试图构造一个通用内部模型而是简单地存储实例数据。在分类时新的数据点会被分配到它最近邻的类别。
所以余弦相似度更适合比较文本的相似程度而k近邻算法常用于分类问题
优缺点
余弦相似度是一种测量两个向量在方向上的相似度的度量。它广泛用于文本分析特别是在计算文档或文本片段之间的相似性时。
优点
1.不受大小影响余弦相似度仅考虑向量间的角度而不受其大小即向量的长度或幅度的影响这使得它特别适用于文本数据其中词频长度可能不是很重要。
2.效率较高在稀疏数据集如文本数据上计算余弦相似度通常比其他相似度测量更高效。 适合高维数据它适用于高维数据集例如文本数据其中每个维度代表一个不同的单词。
缺点
1.不考虑非共有特征仅考虑两个向量共有的特征即同时在两个向量中出现的元素这可能会忽略某些重要信息。
2.对数据分布敏感在某些情况下数据的分布会影响余弦相似度的结果尤其是当两个向量的长度相差悬殊时。
业务应用
1.获取html文本内容我有两个html文件获取html很容易自动化和http请求都可以做到但是要注意robot.txt协议h6是一个整体的大html h7是案例html我要拿的是 所有回答的链接所以h7就是随机一个链接的html
file_path D:/herche_ai/h6.html
with open(file_path, r, encodingutf-8) as file:html_content file.read()file_path D:/herche_ai/h7.html
with open(file_path, r, encodingutf-8) as file:target_html file.read()构建特征向量我们利用BeautifulSoup将所有元素都趴下来随后我们将其转为字符串表示
def build_feature_vector(html):构建特征向量soup BeautifulSoup(html, html.parser)elements soup.find_all()elements_str [element_to_string(el) for el in elements]return elements_str, elementsdef element_to_string(element):将元素转换为字符串表示return f{element.name} { .join([f{k}{v} for k, v in element.attrs.items()])}3.构建源html和目标html的特征
# 构建原始html特征向量html_elements_str, html_elements build_feature_vector(html)
# 构建目标html特征向量target_elements_str, _ build_feature_vector(target_html)4.处理文本 vectorizer CountVectorizer().fit(html_elements_str target_elements_str)CountVectorizer主要用于文本处理它通过计数每个单词在文本中出现的频率来将文本转换为数值向量。这个过程可以分为以下几个步骤 分词将每个文本在这种情况下是HTML元素的字符串表示分割成单词或标记。 构建词汇表从所有文本中提取出所有不同的单词构建一个词汇表。 计数对于每个文本计算词汇表中每个单词的出现次数。 转换为向量每个文本最终被转换为一个向量向量的每个元素代表词汇表中对应单词的出现次数。
5.将两个html文本转为向量数值 html_vec vectorizer.transform(html_elements_str)target_vec vectorizer.transform(target_elements_str)6.比较相似度并且拿出相似度大于0.5的元素
similarities cosine_similarity(target_vec, html_vec)similar_elements []for index, similarity in enumerate(similarities[0]):if similarity threshold:similar_elements.append(html_elements[index])return similar_elements7.顺利拿到h6 html中所有和h7相似的元素
结束
余弦相似度应用爬虫场景结束 文章转载自: http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.zqdzg.cn.gov.cn.zqdzg.cn http://www.morning.blqsr.cn.gov.cn.blqsr.cn http://www.morning.rtlth.cn.gov.cn.rtlth.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.prplf.cn.gov.cn.prplf.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.rgsnk.cn.gov.cn.rgsnk.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn