当前位置: 代码迷 >> 数据仓库 >> 新浪微博数据挖掘食谱之二: 话题篇 (selenium)
  详细解决方案

新浪微博数据挖掘食谱之二: 话题篇 (selenium)

热度:126   发布时间:2016-05-05 15:41:20.0
新浪微博数据挖掘菜谱之二: 话题篇 (selenium)
#!/usr/bin/python # -*- coding: utf-8 -*-'''Created on 2014-12-29@author: beyondzhou@name: discovery_trend_topic.py'''# Get trend topic of sina discoverydef discovery_trend_topic():        from discovery import weibo_discovery    from entities import discovery_entities        hour_discovery = weibo_discovery()        # Decode entities    (mids, names, texts) = discovery_entities(hour_discovery)        # Output mids    print '\nOutput mids!'    for mid in mids:        print mid            # Output names    print '\nOutput names!'    for name in names:        print name            # Output texts    print '\nOutput texts!'    for text in texts:        print text    if __name__ == '__main__':    discovery_trend_topic()
import selenium.webdriver.support.ui as uiimport timefrom selenium import webdriver# use non-api to access sina searchdef weibo_discovery():    driver = webdriver.PhantomJS(executable_path=r'E:\eclipse\Weibo\tool\phantomjs')    wait = ui.WebDriverWait(driver,30)    driver.get('http://passport.weibo.com/')    now_handle = driver.current_window_handle    print 'now_handle:', now_handle    print driver.current_url    username = driver.find_element_by_id('username')    password = driver.find_element_by_id('password')    sbtn = driver.find_element_by_class_name('smb_btn')    #save_btn = driver.find_element_by_id('save_btn')    safe_login = driver.find_element_by_id('safe_login')    username.send_keys('') #send username    password.send_keys('') #send password    safe_login.click()     sbtn.submit()     time.sleep(5)     page = driver.page_source.encode('utf-8')    open(r'e:\automation\login.html','w').write(page)     driver.find_element_by_link_text(u"微博").click()    time.sleep(5)    # deal with handles    all_handles = driver.window_handles    print 'all_handles:', all_handles    # switch to new windows    #driver.switch_to_window(driver.window_handles[-1])    for handle in all_handles:        if handle is not now_handle:            driver.switch_to_window(handle)    page = driver.page_source.encode('utf-8')    open(r'e:\automation\weibo.html','w').write(page)    time.sleep(10)    driver.find_element_by_xpath("//div[@class='gn_position']/div/ul/li[2]/a/em[2]")    time.sleep(10)    page = driver.page_source.encode('utf-8')    open(r'e:\automation\disvoery.html','w').write(page)    driver.close()        print 'discovery done!'    return page
# decode entities from sina weibo html sourcesdef discovery_entities(subject):        # Generate soup    from BeautifulSoup import BeautifulSoup    soup = BeautifulSoup(subject,fromEncoding="utf-8")        # decode weibo mid    mids = []    mids_source = soup.findAll("div", attrs={"action-type":"feed_list_item"})    for index in range(len(mids_source)):        mids.append(mids_source[index]['mid'])    print 'mids entities done!'           names = []    names_source = soup.findAll("a", attrs={"class":"W_f14 W_fb S_txt1"})    for index in range(len(names_source)):        names.append(names_source[index].text)    print 'names entities done!'        texts = []    texts_source = soup.findAll("div", attrs={"class":"WB_text W_f14"})    for index in range(len(texts_source)):        texts.append(texts_source[index].text)    print 'texts entities done!'        return mids, names, texts  


Result:

now_handle: 21132260-8ee3-11e4-9039-edcc9bd30e5bhttp://passport.weibo.com/all_handles: [u'21132260-8ee3-11e4-9039-edcc9bd30e5b', u'25c11a10-8ee3-11e4-9039-edcc9bd30e5b']discovery done!mids entities done!names entities done!texts entities done!Output mids!379298082745026037929802400649463792990558230422379299351936269837929841491894963792980915394478379298071839193137929914849114543792982798912419379298266512133237929900509352733792987982854956379298896870581237929816704251143792981624854355Output names!胡歌胡歌新浪娱乐微天下彭晓芸坏男人日志游遍英国跑步心情冰箱上的pepperLifeTime新浪财经薛蛮子一句话领悟人生教你看穿男人的心容祖儿Output texts!之前公司给我下过死命令 喝了酒不许发微博 今天实在忍不住 我很难过 很难过 我忍不住 对不起 或许我真的不适合做演员 我 只想做真实的我另外 还有一件事 大本张 谢谢你一年来对我的帮助 你是一个好人 但是 我们的合作 要告一段落了 与你无关 是我的问题 如果彼此无法坦诚 那就没有必要再合作了 各自珍重[email protected]@江疏影恋情:爱过】29日凌晨,胡歌连发四条微博回应与江疏影绯闻,称“我想说,我爱过,我爱那个回忆,梦醒时分,遥望江胡,对不起,让大家失望了。”疑承认曾与江疏影相恋,但现在是单身状态,“我会继续享受一个人的时光”。详情→http://t.cn/RZwrVzk【亚航8501与马航370,两次失联有何不同?】突然从雷达消失,搜寻一天毫无结果,亚航8501的此次失联不得不让人联想起失踪近10月的马航370。表面看,两次失联有着诸多相似点,但分析人士指出,两者之间至少存在着四点不同:http://t.cn/RZwd9n5正直播#亚航客机失联#事件:http://t.cn/RZZWWGc(新浪)|【伦理与学理】李银河性取向缘何说不清在南都评论部主办的首届“跨界论坛”上的发言。中山大学翟振明教授从概念追根溯源对李银河事件进行了解读,并指出学术语言与维权语言混用可能造成的认知障碍。我侧重分析了公众一般认知与李银河自我认知之间缘何产生错位,以及这一事件对学术伦理的相关启示总有一天,你会站在最亮的地方,活成自己曾经渴望的模样。耶稣和肯德基开始了一段对话。。总有一天你会成为你想成为的人。总觉得事情是这样的网络大V动了谁的奶酪?官媒从薛蛮子嫖娼案到一些人“寻衅滋事”,官媒总是顺带恐吓大V。大V体制内外的人都有,没有任何议题出现大V们观点整齐一致。官媒恼羞成怒的根本原因在于嫉妒大V的网络话语权。“培养自己的大V”、“大V为我所用”等计谋,又是把网民当SB了。说人话,讲公道,官媒一样可以当大V。【财政部长力挺养老保险名义账户制 称改革不能再等】运行了17年的社会统筹加个人账户养老保险制度又一次走到了十字路口。过去17年,“做实”为主流,如今,承认空账的名义账户制获得了更多的共识。楼继伟以学者身份“力挺”名义账户制,更是加重了名义账户制在顶层设计中的权重。http://t.cn/RZwnHZh干货 我分享了|关于未来十年的十条思考心情不好的时候,就觉得全世界都完了,去你妈的感情,去你妈的生活;心情好的时候,又觉得生活真的太美好了,真的好想再活五百年。给自己定了这么多年的规则和底线,整了半天还不是他妈的"全看心情"。“没人会真正的感同身受到你的痛楚,也没人会真正的去在意你一路走来所遇过的坎与负过的伤,所以别再为了寻求安慰而四处同人诉说你的苦,因为旁人只看结果,也只关心结果。成长本就是一个孤立无援的过程,你必须得学会独当一面。”感激 !! 我真的很快樂,這個旅程充實無比,太值得了!這八日裡,我在兩個地方做了兩場great show,找到很美麗的地方拍了三個mv,還在昨晚跟歌迷見面簽名,很難得能和我媽媽過白色聖誕,每天相處,和隊友寓工作http://t.cn/RZwBlDS


  相关解决方案