python美团数据分析 (python怎么爬取外卖数据)

关键词:美团 Python Excel

0.程序是针对美团中的美食部分数据按好评排序采集。

要抓取保存的数据为:

商家名 类型 地理位置 评论人数 均价 最低价格

1.首先编写网页数据采集函数,使用request采集网页源码,具体实现如下

  1. def getHtml(url):

  2. headers = (’User-Agent’,

  3. ’Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11’)

  4. opener = urllib.request.build_opener

  5. opener.addheaders = [headers]

  6. htmldata = opener.open(url).read

  7. htmldata=htmldata.decode(’utf-8’)

  8. return htmldata

2.根据网页源码解析获取已上线城市的url

  1. part = (’gaevent’,’changecity/build’)

  2. urldic = {}

  3. def handle_starttag(self, tag, attrs):

  4. if tag==’a’ and (self.part in attrs):

  5. for att,value in attrs:

  6. if att==’href’:

  7. self.urldic.__setitem__(value, value+’/category/meishi/all/rating’)

  8. def getUrl(self):

  9. return self.urldic

3.获取分页url

  1. pagelist = list

  2. temphref = str

  3. flg = 0

  4. initurl = str

  5. def setInitUrl(self,url):

  6. self.initurl = url

  7. def handle_starttag(self, tag, attrs):

  8. if tag==’a’:

  9. for attr,value in attrs:

  10. if attr==’href’ and (’page’ in value):

  11. self.temphref = self.initurl + value

  12. if self.temphref not in self.pagelist:

  13. self.pagelist.append(self.temphref)

  14. def getList(self):

  15. return self.pagelist

4.解析网页源码 获取有效信息

  1. tempstr = str

  2. divsum = int

  3. def handle_starttag(self, tag, attrs):

  4. if tag==’div’:

  5. for attr,value in attrs:

  6. if attr==’class’ and value.find(’poi-tile-nodeal’)!=-1:

  7. self.tempstr=’’

  8. self.divsum = 0

  9. def handle_data(self, data):

  10. if(data.isspace==False):

  11. data = data.replace(’·’, ’·’)

  12. if data==’¥’:

  13. if ’¥’ not in self.tempstr:

  14. self.tempstr+=’无’ +’\t’

  15. self.tempstr+=data

  16. elif data==’¥’:

  17. if ’¥’ not in self.tempstr:

  18. self.tempstr+=’无’ +’\t’

  19. self.tempstr+=’¥’

  20. elif data==’人评价’:

  21. self.tempstr=self.tempstr[0:-1]+data+’\t’

  22. elif data==’人均 ’:

  23. self.tempstr+=’人均’

  24. elif data[0]==’起’:

  25. self.tempstr=self.tempstr[0:-1]+’起’

  26. else:

  27. self.tempstr+=data+’\t’

  28. def handle_endtag(self, tag):

  29. if tag==’div’:

  30. self.divsum+=1

  31. if self.divsum==6:

  32. if (self.tempstr.find(’¥’))!=-1:

  33. if (re.split(r’\t’, self.tempstr).__len__)==5:

  34. teststr = str

  35. flg = 0

  36. for stmp in re.split(r’\t’,self.tempstr):

  37. if flg==2:

  38. teststr+=’无位置信息’+’\t’

  39. teststr+=stmp+’\t’

  40. flg+=1

  41. self.tempstr=teststr

  42. if (re.split(r’\t’, self.tempstr).__len__)==6:

  43. arraystr.append(self.tempstr)

  44. self.divsum=0

  45. self.tempstr=’’

5.将信息存放于Excel中

  1. def SaveExcel(listdata):

  2. head=[’商家名’,’类型’,’地理位置’,’评论人数’,’均价’,’最低价格’]

  3. wbk=xlwt.Workbook

  4. sheet1=wbk.add_sheet("sheet1")

  5. ii=0

  6. for testhand in head:

  7. sheet1.write(0,ii,testhand)

  8. ii+=1

  9. i=1

  10. j=0

  11. for stt in listdata:

  12. j=0

  13. lis = re.split(r’\t’,stt)

  14. for ls in lis:

  15. sheet1.write(i,j,ls)

  16. j=j+1

  17. i+=1

  18. wbk.save(’test.xls’)

以下是Excel中的数据:

怎么用python爬美团数据,excel爬虫抓取美团数据

来自:数据挖掘入门与实战

↓↓↓ 【精彩推荐】

1、 大咖看过来,数据分析网专栏作家招募!!

2、 加入大数据交流QQ群,开启数据江湖!!

3、 有缘人看过来,数据分析网志愿者招募!!

↓↓↓【点击阅读原文访问数据分析网】