
#baidu_hotword.py
#get baidu hotword in news.baidu.com
import urllib2
import os
import re def getHtml(url):
page = urllib2.urlopen(url)
html = page.read()
page.close()
return html def getHotWord(html):
reg = '<li.*?hotwords_li_a.*?title="(.*?)".*?</li>'
hotwords = re.compile(reg).findall(html)
return hotwords if __name__ == '__main__':
html = getHtml('http://news.baidu.com/')
#print(html)
hotwds = getHotWord(html)
for i in hotwds:
print unicode(i, "gb2312")
输出时必须使用unicode(i, "gb2312"),否则输出不了中文,跟字符编码相关,暂时还没研究。