Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyloner committed Mar 21, 2017
1 parent 2fd53f8 commit cfaf1ba
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions www.mcdonalds.com.cn/storelist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import requests
from bs4 import BeautifulSoup
import openpyxl
import json


headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0"}


def get_all_market():
url='http://www.mcdonalds.com.cn/ajaxs/get_all_marker'
html=requests.get(url,headers=headers).text
data=json.loads(html)
return data

def get_store_infor(store_id):
html=requests.get('http://www.mcdonalds.com.cn/ajaxs/get_marker_detail/'+str(store_id),headers=headers).text
data=json.loads(html)
result=[]
keys=['name','city','province','address','phone_number']
for key in keys:
try:
result.append(str(data[key]))
except:
result.append('')
return result

def write_to_excel(result):
excel=openpyxl.Workbook(write_only=True)
sheet=excel.create_sheet()
for line in result:
sheet.append(line)
excel.save('麦当劳.xlsx')

def main():
markets=get_all_market()
result=[]
for item in markets:
try:
line=get_store_infor(item['id'])
except:
failed=open('failed.txt','a')
failed.write(str(item)+'\n')
failed.close()
continue
print(item)
result.append(line)
write_to_excel(result)

main()

0 comments on commit cfaf1ba

Please sign in to comment.