信息领域热词分析

今天了解一下什么是热词分析

例如今日头条:

能够将点击量最多的信息推送到顶部,方便观看。

python趴取热词

下面是一个小列子:

百度新闻页面(http://news.baidu.com/)上的百度热搜词部分的html是这个样子的

 
<a href="http://news.baidu.com/ns?cl=3&ct=9&rn=20&sp=hotquery&word=%C1%F5%CF%E8%20%BB%D8%B9%FA" target="_blank" mon="ct=1&a=30">刘翔回国</a>
 
<span style="font-size: small;"># -*- coding: UTF-8 -*-  
  
import urllib2  
from sgmllib import SGMLParser  
  
#继承自SGMLParser,用于抽取新闻热搜词的类  
class HotExtract(SGMLParser):  
      
    ''''' 
    20120814 
         经分析,百度新闻热搜词的dom结构是下边这个样子的 
    <a href="http://news.baidu.com/ns?cl=3&ct=9&rn=20&sp=hotquery&word=%C1%F5%CF%E8%20%BB%D8%B9%FA" target="_blank" mon="ct=1&a=30">刘翔回国</a> 
         于是按<a>标签抽取,属性mon的值等于“ct=1&a=30”时判定为新闻热搜词标签 
    '''  
    def __init__(self):  
        SGMLParser.__init__(self)  
        self.is_a = ""  
        self.hot = []  
          
    def start_a(self, attrs):  
        if len(attrs) == 0:  
            pass  
        else:  
            for (variable, value) in attrs:  
                if variable == "mon" and value == "ct=1&a=30":  
                    self.is_a = 1  
                    break  
                  
    def end_a(self):  
        self.is_a = ""  
          
    def handle_data(self, text):  
        if self.is_a == 1:  
            self.hot.append(text)   
  
#抓取html内容  
def getHtml(url):  
    html = urllib2.urlopen(url).read()  
    return html  
  
#抽取特定html标签中的内容(此处为抽取属性mon等于“ct=1&a=30”的a标签的text),重写HotExtract类可抽取其它内容  
def extract_hot(html):  
    hotExtract = HotExtract()  
    hotExtract.feed(html)  
    return hotExtract.hot  
  
html = getHtml("http://news.baidu.com/")  
hot_list = extract_hot(html)  
for hot in hot_list:  
    print hot</span>  

原文地址:https://www.cnblogs.com/1502762920-com/p/12331311.html