text: # coding: utf-8
  import requests
  from bs4 import BeautifulSoup
  import re
  import json
  import os
  from xml.etree import ElementTree
  import time
  import io
  import pandas as pd
  from gotoeat_map.module import getLatLng, checkRemovedMerchant
  
  
  def main():
      merchantFilePath = os.path.dirname(
          os.path.abspath(__file__)) + "/merchants.json"
  
      if os.path.exists(merchantFilePath):
          json_open = open(merchantFilePath, "r", encoding="utf8")
          merchants = json.load(json_open)
      else:
          merchants = {
              "data": [],
              "names": []
          }
      findMerchants = []
  
      page = 0
      while True:
          page += 1
          print("----- Page {page} -----".format(page=page))
          html = requests.get(
              "https://www.goto-eat-ehime.com/page/{page}/?s&shop_area&shop_city".format(page=page))
          html.encoding = html.apparent_encoding
          soup = BeautifulSoup(html.content, "html.parser")
          lists = soup.find("ul", {"class": "shop_list"}).findChildren("li", recursive=False)
          if (len(lists) == 0):
              break
          for merchant in lists:
              merchant_name = merchant.find(
                  "dt").text.strip()
  
              merchant_type = []
              categories = merchant.find("p", {"class": "shop_cat"}).findChildren("span")
              for category in categories:
                  merchant_type.append(category.text.strip())
  
              rows = merchant.find("dd").find("ul").findChildren("li")
              for row in rows:
                  key = row.findChildren("span")[0].text.strip()
                  value = row.findChildren("span")[1].text.strip()
  
                  if key == "住所":
                      merchant_address = value
                  elif key == "TEL":
                      merchant_tel = value
  
              print(merchant_name + " - " + merchant_address)
              findMerchants.append(merchant_name)
  
              if merchant_name in merchants["names"]:
                  continue
  
              lat, lng = getLatLng(merchant_address)
              print(str(lat) + " " + str(lng))
  
              merchants["data"].append({
                  "name": merchant_name,
                  "type": merchant_type,
                  "address": merchant_address,
                  "tel": merchant_tel,
                  "lat": lat,
                  "lng": lng
              })
              merchants["names"].append(merchant_name)
  
              with open(merchantFilePath, mode="w", encoding="utf8") as f:
                  f.write(json.dumps(merchants, indent=4, ensure_ascii=False))
          if (soup.find("a", {"class": "nextpostslink"}) == None):
              break
          else:
              time.sleep(1)
  
      merchants = checkRemovedMerchant(merchants, findMerchants)
  
      with open(merchantFilePath, mode="w", encoding="utf8") as f:
          f.write(json.dumps(merchants, indent=4, ensure_ascii=False))
  
  
  main()
  