python抓取百度热词

时间:2023-03-09 18:22:35
python抓取百度热词
 #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"),否则输出不了中文,跟字符编码相关,暂时还没研究。

参考: http://sdu-wizard.iteye.com/blog/1631465

    http://bbs.chinaunix.net/thread-1623347-1-1.html