抓取美团商家信息

import requests
from bs4 import BeautifulSoup
import json url = 'http://bj.meituan.com/'
url_shop = 'http://bj.meituan.com/shop/{}'
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, sdch',
'Accept-Language':'zh-CN,zh;q=0.8',
'Cache-Control':'max-age=0',
'DNT':'1',
'Host':'bj.meituan.com',
'Proxy-Connection':'keep-alive',
'Referer':'http://bj.meituan.com/shop/286725?acm=UwunyailsW15518532529028663069.286725.1&mtt=1.index%2Fdefault%2Fpoi.pz.1.j4cijrmg&cks=58899',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
} # 得到所有的二级菜单url
def get_start_menu_links():
html = requests.get(url).text
soup = BeautifulSoup(html, 'lxml')
links = [link.find('div').find('div').find('dl').find('dt').find('a')['href'] for link in soup.find_all('div',class_='J-nav-item') ]
return links def get_shop_ids(url, headers=None):
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'lxml')
content_id = json.loads(soup.find('div', class_='J-scrollloader cf J-hub')['data-async-params'])
return json.loads(content_id.get('data')).get('poiidList') def main():
start_menu_links = get_start_menu_links()
for link in start_menu_links:
for pageNum in range(4,5):
category_url = link + '/all/page{}'.format(pageNum)
for shop_id in get_shop_ids(category_url, headers=headers):
html = requests.get(url_shop.format(shop_id), headers=headers).text
soup = BeautifulSoup(html, 'lxml')
shop_detail = soup.find('div', class_='summary biz-box fs-section cf')
print("==================================pageNum %d shop_id: %d===================================================" % (pageNum,shop_id ))
try:
shop_detail.find('div', class_='fs-section__left').find('h2').find('span').text
except:
continue
print("名称: " + shop_detail.find('div', class_='fs-section__left').find('h2').find('span').text)
print("地址: " + shop_detail.find('div', class_='fs-section__left').find('p', class_='under-title').find('span').text)
print("联系方式: " + shop_detail.find('div', class_='fs-section__left').find('p', class_='under-title').find_next_sibling().text) if '__main__' == __name__:
main()

最新文章

  1. Js 实现tab切换效果
  2. Python学习总结17:exec和eval执行求值字符串
  3. USB OTG
  4. vc++ 最小化到托盘的详细实现
  5. Oracle EBS-SQL (SYS-4):sys_职责查询.sql
  6. python自学笔记(八)python语句
  7. 安裝 Rails 開發環境
  8. QT QT creator QTsdk的区别
  9. 史上最完整的PS快捷键(绝对经典)
  10. 学习整理与细化(1)——Internet 的域名系统(domain name system)
  11. 匿名内部类使用外面的类为什么要用final型
  12. Python档案袋(线程 )
  13. 使用autohotkey修改方向键、回车和启动程序
  14. mssql sqlserver in 关键字在值为null的应用举例
  15. hibernate框架学习之数据查询(本地SQL)
  16. git 使用ssh密钥
  17. sharepoint 2010 记录管理 对象模型
  18. Linux下DIR,dirent,stat等结构体详解(转)
  19. containerdns配置说明
  20. django rest_framework 框架的使用03

热门文章

  1. 设置Schema-Registry的配置,以支持Schema变化
  2. Thymeleaf标签学习
  3. RSAUtils加密解密
  4. Android之布局androidmanifest.xml 资源清单 概述
  5. \_\_str\_\_和\_\_repr\_\_
  6. Java面试宝典2017
  7. itop4412开发版-安卓系统卸载默认apk使用文档
  8. Linux基础篇二:Bash shell(壳,命令解释器)介绍
  9. 一个http的Post请求问题,unable to resolve host <我的域名>:no address associated with hostnam
  10. unittest如何在循环遍历一条用例时生成多个测试结果