99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python 爬取了馬蜂窩的出行數(shù)據(jù),告訴你這個(gè)夏天哪里最值得去
Python 爬取了馬蜂窩的出行數(shù)據(jù),告訴你這個(gè)夏天哪里最值得去
2018-08-14
收藏

Python 爬取了馬蜂窩的出行數(shù)據(jù),告訴你這個(gè)夏天哪里最值得去

正值火辣的暑假,朋友圈已經(jīng)被大家的旅行足跡刷屏了,真的十分驚嘆于那些把全國(guó)所有省基本走遍的朋友們。與此同時(shí),也就萌生了寫篇旅行相關(guān)的內(nèi)容,本次數(shù)據(jù)來(lái)源于一個(gè)對(duì)于爬蟲十分友好的旅行攻略類網(wǎng)站:螞蜂窩。

獲得城市編號(hào)

螞蜂窩中的所有城市、景點(diǎn)以及其他的一些信息都有一個(gè)專屬的5位數(shù)字編號(hào),我們第一步要做的就是獲取城市(直轄市+地級(jí)市)的編號(hào),進(jìn)行后續(xù)的進(jìn)一步分析。

以上兩個(gè)頁(yè)面就是我們的城市編碼來(lái)源。需要首先從目的地頁(yè)面獲得各省編碼,之后進(jìn)入各省城市列表獲得編碼。

過(guò)程中需要Selenium進(jìn)行動(dòng)態(tài)數(shù)據(jù)爬取,部分代碼如下:


  1. def find_cat_url(url):
  2. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
  3. req=request.Request(url,headers=headers)
  4. html=urlopen(req)
  5. bsObj=BeautifulSoup(html.read(),"html.parser")
  6. bs = bsObj.find('div',attrs={'class':'hot-list clearfix'}).find_all('dt')
  7. cat_url = []
  8. cat_name = []
  9. for i in range(0,len(bs)):
  10. for j in range(0,len(bs[i].find_all('a'))):
  11. cat_url.append(bs[i].find_all('a')[j].attrs['href'])
  12. cat_name.append(bs[i].find_all('a')[j].text)
  13. cat_url = ['http://www.mafengwo.cn'+cat_url[i] for i in range(0,len(cat_url))]
  14. return cat_url


  15. def find_city_url(url_list):
  16. city_name_list = []
  17. city_url_list = []
  18. for i in range(0,len(url_list)):
  19. driver = webdriver.Chrome()
  20. driver.maximize_window()
  21. url = url_list[i].replace('travel-scenic-spot/mafengwo','mdd/citylist')
  22. driver.get(url)
  23. while True:
  24. try:
  25. time.sleep(2)
  26. bs = BeautifulSoup(driver.page_source,'html.parser')
  27. url_set = bs.find_all('a',attrs={'data-type':'目的地'})
  28. city_name_list = city_name_list +[url_set[i].text.replace('n','').split()[0] for i in range(0,len(url_set))]
  29. city_url_list = city_url_list+[url_set[i].attrs['data-id'] for i in range(0,len(url_set))]
  30. js="var q=document.documentElement.scrollTop=800"
  31. driver.execute_(js)
  32. time.sleep(2)
  33. driver.find_element_by_class_name('pg-next').click()
  34. except:
  35. break
  36. driver.close()
  37. return city_name_list,city_url_list





  38. url = 'http://www.mafengwo.cn/mdd/'
  39. url_list = find_cat_url(url)
  40. city_name_list,city_url_list=find_city_url(url_list)
  41. city = pd.DataFrame({'city':city_name_list,'id':city_url_list})


獲得城市信息

城市數(shù)據(jù)分別從以下幾個(gè)頁(yè)面獲取:

(a)小吃頁(yè)面

(b)景點(diǎn)頁(yè)面

(c)標(biāo)簽頁(yè)面

我們將每個(gè)城市獲取數(shù)據(jù)的過(guò)程封裝成函數(shù),每次傳入之前獲得的城市編碼,部分代碼如下:


  1. def get_city_info(city_name,city_code):
  2. this_city_base = get_city_base(city_name,city_code)
  3. this_city_jd = get_city_jd(city_name,city_code)
  4. this_city_jd['city_name'] = city_name
  5. this_city_jd['total_city_yj'] = this_city_base['total_city_yj']
  6. try:
  7. this_city_food = get_city_food(city_name,city_code)
  8. this_city_food['city_name'] = city_name
  9. this_city_food['total_city_yj'] = this_city_base['total_city_yj']
  10. except:
  11. this_city_food=pd.DataFrame()
  12. return this_city_base,this_city_food,this_city_jd




  13. def get_city_base(city_name,city_code):
  14. url = 'http://www.mafengwo.cn/xc/'+str(city_code)+'/'
  15. bsObj = get_static_url_content(url)
  16. node = bsObj.find('div',{'class':'m-tags'}).find('div',{'class':'bd'}).find_all('a')
  17. tag = [node[i].text.split()[0] for i in range(0,len(node))]
  18. tag_node = bsObj.find('div',{'class':'m-tags'}).find('div',{'class':'bd'}).find_all('em')
  19. tag_count = [int(k.text) for k in tag_node]
  20. par = [k.attrs['href'][1:3] for k in node]
  21. tag_all_count = sum([int(tag_count[i]) for i in range(0,len(tag_count))])
  22. tag_jd_count = sum([int(tag_count[i]) for i in range(0,len(tag_count)) if par[i]=='jd'])
  23. tag_cy_count = sum([int(tag_count[i]) for i in range(0,len(tag_count)) if par[i]=='cy'])
  24. tag_gw_yl_count = sum([int(tag_count[i]) for i in range(0,len(tag_count)) if par[i] in ['gw','yl']])
  25. url = 'http://www.mafengwo.cn/yj/'+str(city_code)+'/2-0-1.html '
  26. bsObj = get_static_url_content(url)
  27. total_city_yj = int(bsObj.find('span',{'class':'count'}).find_all('span')[1].text)
  28. return {'city_name':city_name,'tag_all_count':tag_all_count,'tag_jd_count':tag_jd_count,
  29. 'tag_cy_count':tag_cy_count,'tag_gw_yl_count':tag_gw_yl_count,
  30. 'total_city_yj':total_city_yj}


  31. def get_city_food(city_name,city_code):
  32. url = 'http://www.mafengwo.cn/cy/'+str(city_code)+'/gonglve.html'
  33. bsObj = get_static_url_content(url)
  34. food=[k.text for k in bsObj.find('ol',{'class':'list-rank'}).find_all('h3')]
  35. food_count=[int(k.text) for k in bsObj.find('ol',{'class':'list-rank'}).find_all('span',{'class':'trend'})]
  36. return pd.DataFrame({'food':food[0:len(food_count)],'food_count':food_count})


  37. def get_city_jd(city_name,city_code):
  38. url = 'http://www.mafengwo.cn/jd/'+str(city_code)+'/gonglve.html'
  39. bsObj = get_static_url_content(url)
  40. node=bsObj.find('div',{'class':'row-top5'}).find_all('h3')
  41. jd = [k.text.split('n')[2] for k in node]
  42. node=bsObj.find_all('span',{'class':'rev-total'})
  43. jd_count=[int(k.text.replace(' 條點(diǎn)評(píng)','')) for k in node]
  44. return pd.DataFrame({'jd':jd[0:len(jd_count)],'jd_count':jd_count})


數(shù)據(jù)分析

PART1:城市數(shù)據(jù)

首先我們看一下游記數(shù)量最多的TOP10城市:

游記數(shù)量TOP10數(shù)量基本上與我們?nèi)粘K私獾臒衢T城市相符,我們進(jìn)一步根據(jù)各個(gè)城市游記數(shù)量獲得全國(guó)旅行目的地?zé)崃D:

看到這里,是不是有種似曾相識(shí)的感覺(jué),如果你在朋友圈曬的足跡圖與這幅圖很相符,那么說(shuō)明螞蜂窩的數(shù)據(jù)與你不謀而合。

最后我們看一下大家對(duì)于各個(gè)城市的印象是如何的,方法就是提取標(biāo)簽中的屬性,我們將屬性分為了休閑、飲食、景點(diǎn)三組,分別看一下每一組屬性下大家印象最深的城市:

看來(lái)對(duì)于螞蜂窩的用戶來(lái)說(shuō),廈門給大家留下的印象是非常深的,不僅游記數(shù)量充足,并且能從中提取的有效標(biāo)簽也非常多。重慶、西安、成都也無(wú)懸念地給吃貨們留下了非常深的印象,部分代碼如下:


  1. bar1 = Bar("餐飲類標(biāo)簽排名")
  2. bar1.add("餐飲類標(biāo)簽分?jǐn)?shù)", city_aggregate.sort_values('cy_point',0,False)['city_name'][0:15],
  3. city_aggregate.sort_values('cy_point',0,False)['cy_point'][0:15],
  4. is_splitline_show =False,xaxis_rotate=30)

  5. bar2 = Bar("景點(diǎn)類標(biāo)簽排名",title_top="30%")
  6. bar2.add("景點(diǎn)類標(biāo)簽分?jǐn)?shù)", city_aggregate.sort_values('jd_point',0,False)['city_name'][0:15],
  7. city_aggregate.sort_values('jd_point',0,False)['jd_point'][0:15],
  8. legend_top="30%",is_splitline_show =False,xaxis_rotate=30)

  9. bar3 = Bar("休閑類標(biāo)簽排名",title_top="67.5%")
  10. bar3.add("休閑類標(biāo)簽分?jǐn)?shù)", city_aggregate.sort_values('xx_point',0,False)['city_name'][0:15],
  11. city_aggregate.sort_values('xx_point',0,False)['xx_point'][0:15],
  12. legend_top="67.5%",is_splitline_show =False,xaxis_rotate=30)

  13. grid = Grid(height=800)
  14. grid.add(bar1, grid_bottom="75%")
  15. grid.add(bar2, grid_bottom="37.5%",grid_top="37.5%")
  16. grid.add(bar3, grid_top="75%")
  17. grid.render('城市分類標(biāo)簽.html')


PART2:景點(diǎn)數(shù)據(jù)

我們提取了各個(gè)景點(diǎn)評(píng)論數(shù),并與城市游記數(shù)量進(jìn)行對(duì)比,分別得到景點(diǎn)評(píng)論的絕對(duì)值和相對(duì)值,并據(jù)此計(jì)算景點(diǎn)的人氣、代表性兩個(gè)分?jǐn)?shù),最終排名TOP15的景點(diǎn)如下:

螞蜂窩網(wǎng)友對(duì)于廈門真的是情有獨(dú)鐘,鼓浪嶼也成為了最具人氣的景點(diǎn),在城市代表性方面西塘古鎮(zhèn)和羊卓雍措位列前茅。暑假之際,如果擔(dān)心上排的景點(diǎn)人太多,不妨從下排的景點(diǎn)中挖掘那些人少景美的旅游地。

PART3:小吃數(shù)據(jù)

最后我們看一下大家最關(guān)注的的與吃相關(guān)的數(shù)據(jù),處理方法與PART2景點(diǎn)數(shù)據(jù)相似,我們分別看一下最具人氣和最具城市代表性的小吃。

出乎意料,螞蜂窩網(wǎng)友對(duì)廈門果真愛(ài)得深沉,讓沙茶面得以超過(guò)火鍋、烤鴨、肉夾饃躋身最具人氣的小吃。

在城市代表性方面,海鮮的出場(chǎng)頻率非常高,這點(diǎn)與大(ben)家(ren)的認(rèn)知也不謀而合,PART2與3的部分代碼如下:


  1. bar1 = Bar("景點(diǎn)人氣排名")
  2. bar1.add("景點(diǎn)人氣分?jǐn)?shù)", city_jd_com.sort_values('rq_point',0,False)['jd'][0:15],
  3. city_jd_com.sort_values('rq_point',0,False)['rq_point'][0:15],
  4. is_splitline_show =False,xaxis_rotate=30)

  5. bar2 = Bar("景點(diǎn)代表性排名",title_top="55%")
  6. bar2.add("景點(diǎn)代表性分?jǐn)?shù)", city_jd_com.sort_values('db_point',0,False)['jd'][0:15],
  7. city_jd_com.sort_values('db_point',0,False)['db_point'][0:15],
  8. is_splitline_show =False,xaxis_rotate=30,legend_top="55%")

  9. grid=Grid(height=800)
  10. grid.add(bar1, grid_bottom="60%")
  11. grid.add(bar2, grid_top="60%",grid_bottom="10%")
  12. grid.render('景點(diǎn)排名.html')

文中所有涉及到的代碼已經(jīng)發(fā)到Github上了,歡迎大家自?。?

https://github.com/shujusenlin/mafengwo_data。

作者:徐麟,知乎同名專欄作者,目前就職于上海唯品會(huì)產(chǎn)品技術(shù)中心,哥大統(tǒng)計(jì)數(shù)據(jù)狗,從事數(shù)據(jù)挖掘&分析工作,喜歡用R&Python玩一些不一樣的數(shù)據(jù)。


數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }