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

新浪微博数据挖掘食谱之三: 搜索篇 (selenium)

热度:122   发布时间:2016-05-05 15:41:18.0
新浪微博数据挖掘菜谱之三: 搜索篇 (selenium)
#!/usr/bin/python # -*- coding: utf-8 -*-'''Created on 2014-12-30@author: beyondzhou@name: decode_search_entities.py'''# Decode entities from search resultdef decode_search_entities():        # import     from search import weibo_search    from entities import weibo_entities        # Do the search (only get two pages)    subject = weibo_search(topic='iphone')        # Decode entities    (mids, names, texts, dates, reposts, comments, likes) = weibo_entities(subject)        # 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            # Output dates    print '\nOutput dates!'    for date_ in dates:        print date_    # Output reposts number    print '\nOutput reposts!'    for repost in reposts:        print repost            # Output comment number    print '\nOutput comments!'    for comment in comments:        print comment            # Output likes number    print '\nOutput likes!'    for like in likes:        print like        if __name__ == '__main__':    decode_search_entities()
#!/usr/bin/python # -*- coding: utf-8 -*-'''Created on 2014-12-30@author: beyondzhou@name: search.py'''    import selenium.webdriver.support.ui as uiimport timefrom selenium import webdriver# use non-api to access sina searchdef weibo_search(topic='weibo'):    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(5)    driver.find_element_by_xpath("//input[@node-type='searchInput']").clear()    driver.find_element_by_xpath("//input[@node-type='searchInput']").send_keys(topic)    driver.find_element_by_link_text("f").click()    time.sleep(5)    page = driver.page_source.encode('utf-8')    pages = page    open(r'e:\automation\searchResult.html','w').write(page)    # Get page 2, only can get 2 pages (this is a bug)    for i in range(1):        wait.until(lambda driver: driver.find_element_by_link_text(u"下一页"))        driver.find_element_by_link_text(u"下一页").click()        time.sleep(5)        page = driver.page_source.encode('utf-8')        pages += page        open(r'e:\automation\searchResult%s.html' % i,'w').write(driver.page_source.encode('utf-8'))            driver.quit()    print 'search done!'        return pages
# decode entities from sina weibo html sourcesdef weibo_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_texta W_fb"})    for index in range(len(names_source)):        names.append(names_source[index].text)    print 'names entities done!'        texts = []    texts_source = soup.findAll("p", attrs={"class":"comment_txt"})    for index in range(len(texts_source)):        texts.append(texts_source[index].text)    print 'texts entities done!'        dates = []    dates_source = soup.findAll("div", attrs={"class":"feed_from W_textb"})    for index in range(len(dates_source)):        try:            tmp = dates_source[index].a["title"]        except KeyError:            tmp = 'among 5 minutes'        dates.append(tmp)    print 'dates entities done!'        reposts = []    reposts_source = soup.findAll("a", attrs={"action-type":"feed_list_forward"})    for index in range(len(reposts_source)):        if reposts_source[index].em.string is None:            tmp = 0        else:            tmp = reposts_source[index].em.string        reposts.append(tmp)    print 'reposts entities done!'        comments = []    comments_source = soup.findAll("a", attrs={"action-type":"feed_list_comment"})    for index in range(len(comments_source)):        if comments_source[index].em is None or comments_source[index].em.string is None:            tmp = 0        else:            tmp = comments_source[index].em.string        comments.append(tmp)    print 'comments entities done!'        likes = []    likes_source = soup.findAll("a", attrs={"action-type":"feed_list_like"})    for index in range(len(likes_source)):        if likes_source[index].em is None or likes_source[index].em.string is None:            tmp = 0        else:            tmp = likes_source[index].em.string        likes.append(tmp)    print 'likes entities done!'        return mids, names, texts, dates, reposts, comments, likes
Result:                                                                                                                                                    now_handle: 1d316630-8fa5-11e4-b234-a1009e62cc2ehttp://passport.weibo.com/all_handles: [u'1d316630-8fa5-11e4-b234-a1009e62cc2e', u'2457d7f0-8fa5-11e4-b234-a1009e62cc2e']i= 0search done!mids entities done!names entities done!texts entities done!dates entities done!reposts entities done!comments entities done!likes entities done!Output mids!37933561925947443793271488756987379335618420553937933561842054793793356180010523379335616374086637933561553509193793356142032064379335613018080437933561084724233793356087723484379335608352875337933560753794973793356049746415379335604555171237933560331920783793356028773160379335600852480537933559910205353793355940683841379335591551551137933558735657703793355865399262379335584862007237933558232291193793355789893322379335572675040737933557227775963793355705998770379335570158253437933556645379723793355664537958379335566034287337933556221009073793355563154081379335555992498437933555549850523793355508622709379335550512734937933554676356133793355420533970Output names!_小圈圈FayeCometyee有时候有石候YSU92@<span style="white-space:pre">	</span>  柯镇恶aimarKitty猫bb小豆1689奔跑吧MiGin刘洪波爱抽风Candy-SL一个小漫画家兔女郎维维薇薇安姜淑兰千金我家惜君初长成@<span style="white-space:pre">	</span>  玩经理冰寒露轩国际范圣诞MieG鹤行千里路娅攀俩摆弄了一天-爱谁谁@<span style="white-space:pre">	</span>  人民网妍妮子baby小圈圈Faye庄淳凯爱美丽江鸟声悦耳WenQ唭冰情欲动的微博小窝AT小乐@<span style="white-space:pre">	</span>  林更新妍妮子-baby贵族Hen低调de性感高丽琴冰盈绿洁一直在-小圈圈faye小豆1682邱-春燕光合葉綠冰舞超凡要加油妍妮子_baby严厉的凯普斯@<span style="white-space:pre">	</span>  梦想世界官方微博Maggie文琪肖金枝88888锡箔@<span style="white-space:pre">	</span>  中邮展鸿电商平台锡箔@<span style="white-space:pre">	</span>  中国电信广东客服但小薇_777Output texts!卡西欧tr500 当天可以发货卡西欧tr150/卡西欧tr300 全新未拆封 发票包装齐全 卡西欧tr350 基本颜色齐全( 闲置 )( 转让 ) 卡西欧tr200/卡西欧tr350S出售苹果6iphone6 iphone6 plus全部颜色现货 全国联保 卡西欧数码相机 支持验货招收代理有意者请微信我:faye1912转发关注iphone6/tr//ipad/zr/烤面包机 随机 [email protected][email protected]??果6#自拍神器iPhone 6Plus卡西欧自拍神器TR500,tr350S~价格美丽,行货,全国联保TR150 TR200 TR350 TR350S全面到货店主64956648[更新]还发现了两个错的单词(而去用了5个就错了两个)。 精神病吧,打电话就打呀,至于截图上微博吗?信用卡不是自己刷的当然能拿到退款,至于把信封也晒一下吗?这个截图是要炫耀一下你有iPhone吗?那个信封时炫耀你有信用卡吗?昨天中午hiking完回来车被砸了。夏威夷堪比大德州!后面的truck也被打开了钱包和beatspill全拿走了。人生第一次打911。后来打电话给usaa挂失。被告知说我的卡已经刷了一百多了。经过确认他们很爽快的答应了refound给我。提醒大家节假日注意安全。再赞usaa一个!@北美省钱快报|Williston ...卡西欧TR150 TR200 TR350 TR350S TR500全面到货代理们还为没货而烦恼吗?iPhone6招收各种代理!!提供实体店供货!立刻返现 微信:2902515669热货 苹果6 现在已经三色齐全 均是国行/港行 16G /64G售后检测(全部颜色)未拆封 未激活.~自拍神器~~另赠送大礼包包括相机包和16g卡。TR150iPhone6 iPhone6PLUS tr100 tr200 tr300 iphone5s也同时大热dai理的首选 加本店微信号微信vip662233你的梦想#卡西欧TR350s#薄荷绿,粉色 卡西欧tr500,tr350 国行 (颜色齐全). 赠送大礼包包括相机包和SD卡。也同时大热TR150#iPhone6 #iPhone6PLUS tr200 tr300 iphone5s 卡西欧自拍神器 微信:keei12345678海外闲置dfqw#卡西欧##卡西欧自拍神器#TR150 TR200 TR350 TR350S TR500全面到货代理们还为没货而烦恼吗?苹果6iPhone6 iPhone5s招收各种代理!!提供仓库供货!立刻返现 微信:y666368支持分期付款#领年终奖啦#年终奖见!者!有!份!现金每人最高2015大洋!还有iPhone6、iPad、金条等等等等实物豪奖!——有钱就是这么任性!http://t.cn/RzcK4Ml圣诞送大礼不怕没惊喜#卡西欧自拍神器#国行港行#IPhone6#海外闲置自拍神器#iPhone 6Plus卡西欧自拍神器TR500,tr350S~价格美丽,行货,全国联保TR150 TR200 TR350 TR350S全面到货店主还为没货而烦恼吗?微信号赠送大礼物:7093112特别任性送大礼闪亮闪亮闪亮不再为拍照发愁 帮你解决问题#卡西欧TR500#TR350S(孔雀蓝 莹贝白 梦幻红 蜜糖橘.清新绿.天空蓝.熏衣草粉)音盒礼盒版本,带16g内存、Wi-Fi、随时随地分享照片!本店#iPhone6 #iPhone6 iPhone6Plus保修一年免邮费微信:2902515669号外 号外 引领 时尚潮流 的 卡西欧自拍神器 TR500 中学生,高中生 梦寐以求 的自拍神器 tr150 tr350 tr350S 现在#海外投资#特价#海外微商#搞活动iphone6 iphone5S 价格让你满意微信了解:2998808841我和小伙伴们说我中了一个iphone6,他们不信,让我中个给他们看看。【送iPhone6了】要求很简单,[email protected],转发此微博,就有机会获得iPhone6一台。2015年1月1日转发平台抽一个!随手转发,不费事,说不定iPhone6就是你的呢?!#卡西欧TR350s#卡西欧tr500,tr350 tr150 tr200全系列 国行货源充足(颜色齐全)赠送大礼包相机包和SD卡.苹果6iPhone6 iPhone6PLUS iphone5s卡西欧自拍神器微信:aa108866#自拍神器##卡西欧TR500#,tr350S~价格美丽,行货,全国联保~iPhone6 iPhone6Plus卡西欧TR150 TR200 TR350 TR350S全面到货代理们还为没货而烦恼吗?招收各种代理!微信:keei12345678你的梦想#卡西欧TR350s#薄荷绿,粉色 卡西欧tr500,tr350 国行 (颜色齐全). 赠送大礼包包括相机包和SD卡。也同时大热TR150#iPhone6 #iPhone6PLUS tr200 tr300 iphone5s 卡西欧自拍神器 微信:64956648海外闲置#卡西欧自拍神器#TR500 全新、国行未、拆封、海外闲置卡西欧tr350str350 港行 货源充足(颜色齐全)礼盒版本. 自拍神器 赠送大礼包包和SD卡。也同时大热TR150?#苹果专卖iPhone6 iPhone6PLUS#、tr100 tr200 tr300闲置自拍神器微信64956648任性分期#我们送iPhone6了 要求很简单#支持了,每天的心情又不一样,感觉亲切和熟悉了很多http://t.cn/RzM3xyv【我们送iPhone6了 要求很简单】真心回馈粉丝,小编觉得现在最好的奖品就是iPhone6了。今起到12月31日,关注我们,转发微博,就有机会获iPhone6(奖品可能需要等待)!每月抽一台。不费事,还是试试吧,万一中了呢http://t.cn/RzM3xyv全新国行 卡西欧 港行IPhone6 苹果6 iPhone6Plus卡西欧自拍神器 卡西欧TR500卡西欧tr350S~价格美丽 行货 全国联保卡西欧TR150 卡西欧TR200 卡西欧TR350 卡西欧TR350S 卡西欧TR300 卡西欧TR100 全面到货店主还为没货而烦恼吗招收各种代理微信:duyan8827全新国行 卡西欧 港行IPhone6 苹果6 iPhone6Plus卡西欧自拍神器 卡西欧TR500卡西欧tr350S~价格美丽 行货 全国联保卡西欧TR150 卡西欧TR200 卡西欧TR350 卡西欧TR350S 卡西欧TR300 卡西欧TR100全面到货店主还为没货而烦恼吗招收各种代理微信:faye1912全新国行#卡西欧神器#港行#IPhone6##苹果6#自拍神器#iPhone 6Plus卡西欧自拍神器TR500,tr350S~价格美丽,行货,全国联保TR150 TR200 TR350 TR350S全面到货店主还为没货而烦恼吗微信:y666368卡西欧TR150 TR200 TR350 TR350S TR500 ZR1000 ZR1200全面到货代理们还为没货而烦恼吗?iPhone6 iPhone5s招收各种代理!!提供实体店供货!立刻返现 微信:77093112超级大爆炸#卡西欧TR150#薄荷绿,粉色 卡西欧tr350S,tr350 国行货源充足(颜色齐全)完美包装 礼盒版本. 自拍神器 赠送大礼包包括相机包和SD卡。TR150iPhone6 iPhone6PLUS tr100 tr200 tr300 iphone5s 三星note4 低价贱卖代理的首选微信 keei12345678#卡西欧##卡西欧自拍神器#TR150 TR200 TR350 TR350S TR500 ZR1000 ZR1200全面到货代理们还为没货而烦恼吗?苹果6iPhone6 iPhone5s招收各种代理!!提供仓库供货!立刻返现微信:aa108866支持分期付款盗号的这么用心,下面的山寨iphone6Plus是怎么回事其实我就是失恋了而已,但是就算你怎么讨厌我我还是爱你的,老公!@王思聪卡西欧数码相机 苹果6 港版iPhone6(4.7英寸)/ iPhone6 plus(5.5英寸)16G/64G美颜相机卡西欧tr100 / 卡西欧tr150 / 卡西欧tr350 / 卡西欧tr200 / 卡西欧tr300 / 卡西欧tr350s / 卡西欧tr500赶紧抢购啊本人的货源为港版/国行本次为平价出售招收代理威信:duyan8827国行港行 苹果6iPhone6 iPhonePlus卡西欧自拍神器TR500 TR350S TR350,TR150国行(7种色)未激活.未拆封 全国联保 童叟无欺 tr200 tr300 tr100 ,本店招收代理 优惠更加惊爆.微信y666368全新国行 港行IPhone6苹果6 iPhone6Plus卡西欧自拍神器TR500,tr350S~价格美丽,行货,全国联保 卡西欧 TR150TR200TR350TR350S全面到货店主还为没货而烦恼吗微信:y666368#卡西欧##卡西欧自拍神器#TR150 TR200 TR350 TR350S TR500全面到货代理们还为没货而烦恼吗?苹果6iPhone6 iPhone5s招收各种代理!!提供仓库供货!立刻返现 微信:aa108866支持分期付款现货iphone6/iphone6 plus卡西欧tr350s / tr350 稳定货源代理们的首选 原包装配件齐全 16G原装内存卡原装相机包卡西欧TR500 卡西欧tr150 卡西欧tr200 ( 二手 )( 闲置 ) 卡西欧tr100 卡西欧tr300 全部支持专柜验货 美颜相机自拍神器招收代理威信:faye1912卡西欧数码相机 苹果6 港版iPhone6(4.7英寸)/ iPhone6 plus(5.5英寸)16G/64G 美颜相机卡西欧tr100 / 卡西欧tr150 / 卡西欧tr350 / 卡西欧tr200 / 卡西欧tr300 / 卡西欧tr350s / 卡西欧tr500赶紧抢购啊本人的货源为港版/国行本次为平价出售招收代理 微信:vip662233你要的一百块 吃完麻辣烫马上让#海外代购#给你 最近 卡西欧 自拍新品 tr500 霸气到货#海外新宠#tr350s tr150# 个人闲置帮转tr350 全国联保iphone6 全国三包 iphone6 plus 各色齐全 招代理 keei12345678微信 自拍神器 梦幻红 清新绿 一次满足你oliol看看,看看,再看看,iPhone、小米、包包、话费,好想要T T~~~!#对面#积分免费换礼物,通通都是我的!想要就戳这吧!自拍神器#卡西欧TR500#,tr350S~价格美丽,行货,全国联保~iPhone6 iPhone6Plus卡西欧TR150 TR200 TR350 TR350S全面到货招收各种代理!微信:2998808841国行港行 苹果6iPhone6 iPhonePlus卡西欧自拍神器TR500 TR350S TR350,TR150国行(7种色)未激活.未拆封全国联保 支持验货 tr200 tr300 tr100 白富美的象征^_^赠WIFI卡大礼包等等本店招收代理 优惠更加惊爆一直被模仿 从未被超越请加入实力团队吧 微信:duyan8827#《梦想世界2》庆公测迎新服?送iPhone6#期待着有小惊喜。剑出江湖,名动天下!东方武侠回合制网游《梦想世界2》火爆公测中,12月26日“卧虎藏龙”新服开启。现诚邀天下豪杰,共赴梦想江湖![email protected][email protected],就有机会赢取iPhone6大奖!新手礼包领取:http://t.cn/R7wstIQhttp://t.cn/Rzk7CYx热货TR500卡西欧tr350S,tr350相机均是国行的售后检测(全部颜色)未拆封 未激活.~自拍神器~~另赠送大礼包包括相机包和16g卡。TR150iPhone6 iPhone6PLUS tr100 tr200 tr300 iPhone5s也同时大热代理的首选 .微信 csoip6。或者可以私信号外 号外引领 时尚潮流 的 卡西欧自拍神器 TR500 中学生,高中生 梦寐以求 的自拍神器 tr150 tr350 tr350S 现在#海外投资#特价#海外微商#搞活动iphone6 iphone5S 价格让你满意让你梦不再远,随手可及 机会不多#二手闲置#一起微信了解:xiaomippp#中邮展鸿1号店12.21大抢节,转发赢iPhone#好活动实在是很多,导致让我迷失了双眼,今晚的努力,[email protected]#中邮展鸿1号店12.21大抢节,转发赢iPhone#即刻起至12月31日,关注@ 中邮展鸿电商平台,转发此条微博,并@ 1位好友,即可参与抽奖。http://t.cn/RzB3Yda#积分轻松兑,转发赢iPhone6(第二期)#不管中不中奖,我都来支持小编的工作,我们一起加油!!!@如花般破碎的回忆轻松抢,轻松兑,积分好礼轻松抢不停!2012年12月31日前产生的电信积分即将到期清零!好礼好积分,你准备到→http://t.cn/R7t0K41这里兑换了吗?转发此微博并@ 一位好友,即有机会免费赢#iPhone6##有钳就是任性#!http://t.cn/RzEUXM1据美国中文网报道,美国俄亥俄州男子梅尔斯近日在网络上传一段有趣视频,视频中他用自己的iPhone手机播放蠕虫画面成功吸引到饥饿的豹蛙。Output dates!2014-12-30 05:542014-12-30 00:172014-12-30 05:54among 5 minutes2014-12-30 05:542014-12-30 05:542014-12-30 05:542014-12-30 05:542014-12-30 05:542014-12-30 05:542014-12-30 05:532014-12-30 05:532014-12-30 05:53among 5 minutes2014-12-30 05:532014-12-30 05:532014-12-30 05:532014-12-30 05:532014-12-30 05:53among 5 minutes2014-12-30 05:532014-12-30 05:532014-12-30 05:532014-12-30 05:532014-12-30 05:532014-12-30 05:522014-12-30 05:52among 5 minutes2014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:522014-12-30 05:51among 5 minutes2014-12-30 05:512014-12-30 05:512014-12-30 05:51among 5 minutes2014-12-30 05:51among 5 minutes2014-12-30 05:512014-12-30 05:51Output reposts!03502000000000000000000000000000000000050000Output comments!00000000000000000000000000000000000020000Output likes!01560300000000010460000062000000008251600000000000260002505500
  相关解决方案