1. 程式人生 > >百度風雲榜相關

百度風雲榜相關

技術分享 with image pri 暢銷書 sea 數據 n+1 col

時間:2017-8-5

目標:http://top.baidu.com/population?fr=topindex

配置:py3.4 + win7

目的:練習json數據抓取,及滿足個人數據癮

擴展:可選擇性別、年齡分類(時間精力有限就沒分類排序),可導入excel或是數據庫,但我只是閑暇看看,就導入txt了。

完整代碼:

 1 ‘‘‘
 2 2017-8-5 0:41  by:羽凡
 3 百度風雲榜相關
 4 py3.4 + win7
 5 結果導入txt文件(方便查看,另可導入excel,數據庫等(自行探索))
 6 練習json數據抓取
 7 ‘‘‘
 8 
 9 
10 
11 
12 import
urllib.request 13 import urllib.parse 14 import json 15 di = {26:電影,4:電視劇, 19:綜藝,23:動漫,491:歐美明星, 16 255:體壇人物,261:財經人物,3:美女,22:帥哥,18:女演員, 17 17:男演員,16:女歌手,15:男歌手,454:主持人,257:互聯網人物, 18 353:玄幻奇幻,355:都市言情,354:武俠仙俠
,356:驚悚懸疑, 19 359:歷史軍事,461:完結,460:免費,342:民生熱點,344:娛樂熱點, 20 11:體育熱點,12:高校,302:旅遊城市,14:風景名勝,280:團購, 21 270:奢侈品,291:美食,24:寵物,450:暢銷書,2:熱門搜索,396:世說新詞} 22 print( 23 ‘‘‘----------------------------------------------------------------------
24 26電影 4電視劇 19綜藝 23動漫 491歐美明星 255體壇人物 261財經人物 25 3 美女 22帥哥 18女演員 17男演員 16女歌手 15男歌手 454主持人 257互聯網人物 26 353玄幻奇幻 355都市言情 354武俠仙俠 356驚悚懸疑 359歷史軍事 461完結 460免費 27 342民生熱點 344娛樂熱點 11體育熱點 28 12高校 302旅遊城市 14風景名勝 280團購 270奢侈品 291美食 24寵物 450暢銷書 29 2熱門搜索 396世說新詞 30 ----------------------------------------------------------------------‘‘‘) 31 choice = input(選擇你感興趣的話題編號:) 32 choice_int = int(choice) 33 url = http://top.baidu.com/population/toplist 34 header = {User-Agent:Mozilla/5.0} 35 data = {boardid:choice_int, 36 divids[]:0} 37 data_encode = urllib.parse.urlencode(data).encode(utf-8) 38 req = urllib.request.Request(url,data=data_encode,headers=header) 39 res = urllib.request.urlopen(req) 40 content = res.read().decode(gb2312) 41 content = json.loads(content) 42 for n,i in enumerate(content[topWords][0]): 43 result = str(n+1) + +i[keyword]+-----+關註度:+str(i[searches]) 44 print(result) 45 with open(di[choice]+.txt,a+) as f: 46 f.write(result+\n) 47 print(Done)

運行結果:

技術分享

百度風雲榜相關