source_url_list = {
#ddone'ちふれ':'https://www.amazon.co.jp/s?k=%E3%81%A1%E3%81%B5%E3%82%8C&rh=p_89%3A%E3%81%A1%E3%81%B5%E3%82%8C%7C%E3%81%A1%E3%81%B5%E3%82%8C%28CHIFURE%29%7C%E3%81%A1%E3%81%B5%E3%82%8C%E5%8C%96%E7%B2%A7%E5%93%81&dc&page='
#done'インテグレート':'https://www.amazon.co.jp/s?k=%E3%82%A4%E3%83%B3%E3%83%86%E3%82%B0%E3%83%AC%E3%83%BC%E3%83%88&i=beauty&rh=p_89%3AINTEGRATE%28%E3%82%A4%E3%83%B3%E3%83%86%E3%82%B0%E3%83%AC%E3%83%BC%E3%83%88%29%7C%E3%82%A4%E3%83%B3%E3%83%86%E3%82%B0%E3%83%AC%E3%83%BC%E3%83%88%28INTEGRATE%29&dc&page='
#ddone'excel':'https://www.amazon.co.jp/s?k=%E3%82%A8%E3%82%AF%E3%82%BB%E3%83%AB&i=beauty&page='
#ddone'キャンメイク':'https://www.amazon.co.jp/s?k=%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF&rh=p_89%3A%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF&dc&page='
#ddone'ナチュリエ':'https://www.amazon.co.jp/s?k=%E3%83%8A%E3%83%81%E3%83%A5%E3%83%AA%E3%82%A8&rh=p_89%3A%E3%83%8A%E3%83%81%E3%83%A5%E3%83%AA%E3%82%A8&dc&page='
#ddone'ニベア':'https://www.amazon.co.jp/s?k=%E3%83%8B%E3%83%99%E3%82%A2&rh=p_89%3ANIVEA%7C%E3%83%8B%E3%83%99%E3%82%A2&dc&page='
#ddone'ヒロインメイク':'https://www.amazon.co.jp/s?k=%E3%83%92%E3%83%AD%E3%82%A4%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF&i=beauty&rh=p_89%3A%E3%83%92%E3%83%AD%E3%82%A4%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF&dc&page='
#ddone'フィアンセ':'https://www.amazon.co.jp/s?k=%E3%83%95%E3%82%A3%E3%82%A2%E3%83%B3%E3%82%BB&i=beauty&rh=p_89%3A%E3%83%95%E3%82%A3%E3%82%A2%E3%83%B3%E3%82%BB&dc&page='

#ddone'PAUL & JOE BEAUTE':'https://www.amazon.co.jp/s?k=%E3%83%9D%E3%83%BC%E3%83%AB%E3%82%A2%E3%83%B3%E3%83%89%E3%82%B8%E3%83%A7%E3%83%BC&i=beauty&rh=p_89%3APaul+%26+Joe%7C%E3%83%9D%E3%83%BC%E3%83%AB%26%E3%82%B8%E3%83%A7%E3%83%BC%28P%26J%29%7C%E3%83%9D%E3%83%BC%E3%83%AB%EF%BC%86%E3%82%B8%E3%83%A7%E3%83%BC&dc&page='
#ddone'リンメル':'https://www.amazon.co.jp/s?k=%E3%83%AA%E3%83%B3%E3%83%A1%E3%83%AB&i=beauty&rh=p_89%3ARimmel+%28%E3%83%AA%E3%83%B3%E3%83%A1%E3%83%AB%29&dc&page='
#ddone'無印良品':'https://www.amazon.co.jp/s?k=%E7%84%A1%E5%8D%B0%E8%89%AF%E5%93%81&i=beauty&page='
#ddone'A PIEU':'https://www.amazon.co.jp/s?k=A%27PIEU&rh=p_89%3A%E3%82%A2%E3%83%94%E3%83%A5%28Apieu%29%7C%E3%82%AA%E3%83%94%E3%83%A5%28A%27PIEU%29&dc&page='
#ddone'ADDICTION':'https://www.amazon.co.jp/s?k=ADDICTION&i=beauty&rh=p_89%3AADDICTION%28%E3%82%A2%E3%83%87%E3%82%A3%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%29%7C%E3%82%A2%E3%83%87%E3%82%A3%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3&dc&page='
#done'B.A':'https://www.amazon.co.jp/s?k=B.A&i=beauty&rh=p_89%3AB.A&dc&page='
#ddone'banilaco':'https://www.amazon.co.jp/s?k=banilaco&rh=p_89%3ABANILA+CO%7CBanilaco&dc&page='
#ddone'CandyDoll':'https://www.amazon.co.jp/s?k=CandyDoll&rh=p_89%3ACandyDoll&dc&page='
#ddone'CEZANNE':'https://www.amazon.co.jp/s?k=CEZANNE&i=beauty&rh=p_89%3A%E3%82%BB%E3%82%B6%E3%83%B3%E3%83%8C&dc&page='
#ddone'CHANEL':'https://www.amazon.co.jp/s?k=CHANEL&i=beauty&rh=p_89%3ACHANEL%7CCHANEL%28%E3%82%B7%E3%83%A3%E3%83%8D%E3%83%AB%29&dc&_page='
#ddone'CLIO':'https://www.amazon.co.jp/s?k=CLIO&i=beauty&rh=p_89%3ACLIO&dc&page='
#ddone'CNP Laboratory':'https://www.amazon.co.jp/s?k=CNP+Laboratory&rh=p_89%3ACNP+LABORATORY+CNP&dc&page='
#done'Dior':'https://www.amazon.co.jp/s?k=Dior&rh=p_89%3ADior&dc&page='
#done'ettusais':'https://www.amazon.co.jp/s?k=ettusais&i=beauty&rh=p_89%3Aettusais%28%E3%82%A8%E3%83%86%E3%83%A5%E3%82%BB%29&dc&page='
#ddone'ETUDE':'https://www.amazon.co.jp/s?k=ETUDE&rh=p_89%3Aetude%7C%E3%82%A8%E3%83%81%E3%83%A5%E3%83%BC%E3%83%89%E3%83%8F%E3%82%A6%E3%82%B9%28Etude+House%29&dc&page='
#ddone,'ETVOS':'https://www.amazon.co.jp/s?k=ETVOS&i=beauty&rh=p_6%3AA2BQXUAFPEDG4L&dc&page='
#ddone'innisfree':'https://www.amazon.co.jp/s?k=innisfree&i=beauty&rh=p_89%3Ainnisfree%7C%E3%82%A4%E3%83%8B%E3%82%B9%E3%83%95%E3%83%AA%E3%83%BC%28innisfree%29&dc&page='
#ddone'JILL STUART':'https://www.amazon.co.jp/s?k=JILL+STUART&i=beauty&rh=p_89%3AJILL+STUART&dc&page='
#ddone'Kailijumei':'https://www.amazon.co.jp/s?k=Kailijumei&rh=p_89%3Akailijumei%28%E3%82%AB%E3%82%A4%E3%83%AA%E3%82%B8%E3%83%A5%E3%83%A1%E3%82%A4%29%7C%E3%82%AB%E3%82%A4%E3%83%AA%E3%82%B8%E3%83%A5%E3%83%A1%E3%82%A4&dc&page='
#ddone'KATE':'https://www.amazon.co.jp/s?k=KATE&i=beauty&rh=p_89%3AKATE%28%E3%82%B1%E3%82%A4%E3%83%88%29%7CKate&dc&page='
#ddone'M・A・C':'https://www.amazon.co.jp/s?k=M%E3%83%BBA%E3%83%BBC&i=beauty&rh=p_89%3AMAC%E3%83%BB%E3%83%9E%E3%83%83%E3%82%AF%7CM%E3%83%BBA%E3%83%BBC+%28%E3%83%9E%E3%83%83%E3%82%AF%29&dc&page='
#ddone'MAJOLICA MAJORCA':'https://www.amazon.co.jp/s?k=MAJOLICA+MAJORCA&rh=p_89%3AMAJOLICA+MAJORCA%28%E3%83%9E%E3%82%B8%E3%83%A7%E3%83%AA%E3%82%AB+%E3%83%9E%E3%82%B8%E3%83%A7%E3%83%AB%E3%82%AB%29&dc&page='
#ddone,'MAYBELLINE NEW YORK':'https://www.amazon.co.jp/s?k=MAYBELLINE+NEW+YORK&i=beauty&rh=p_89%3AMAYBELLINE%7C%E3%83%A1%E3%82%A4%E3%83%99%E3%83%AA%E3%83%B3&dc&page='
#'MEDIHEAL':'https://www.amazon.co.jp/s?k=MEDIHEAL&rh=p_89%3AMEDIHEAL%7C%E3%83%A1%E3%83%87%E3%82%A3%E3%83%92%E3%83%BC%E3%83%AB%28Mediheal%29&dc&page='
#ddone,'MISSHA':'https://www.amazon.co.jp/s?k=MISSHA&rh=p_89%3A%E3%83%9F%E3%82%B7%E3%83%A3%28MISSHA%29&dc&page='
#ddone'OPERA':'https://www.amazon.co.jp/s?k=OPERA&i=beauty&rh=p_89%3AOPERA&dc&page='
#ddone,'REVLON(レブロン)':'https://www.amazon.co.jp/s?k=REVLON%28%E3%83%AC%E3%83%96%E3%83%AD%E3%83%B3%29&i=beauty&rh=p_89%3ARevlon%7C%E3%83%AC%E3%83%96%E3%83%AD%E3%83%B3&dc&page='
#ddone,'RMK':'https://www.amazon.co.jp/s?k=RMK&i=beauty&rh=p_89%3A%E3%82%A2%E3%83%BC%E3%83%AB%E3%82%A8%E3%83%A0%E3%82%B1%E3%83%BC%28RMK%29&dc&page='
#ddone,'the SAEM':'https://www.amazon.co.jp/s?k=the+SAEM&i=beauty&rh=p_89%3A%E3%82%B6+%E3%82%BB%E3%83%A0%7C%E3%82%B6+%E3%82%BB%E3%83%A0%28The+Saem%29&dc&page='
#ddone'THREE':'https://www.amazon.co.jp/s?k=THREE&i=beauty&rh=p_89%3ATHREE%28%E3%82%B9%E3%83%AA%E3%83%BC%29&dc&page='
#ddone'UZU BY FLOWFUSHI':'https://www.amazon.co.jp/s?k=UZU+BY+FLOWFUSHI&i=beauty&rh=p_6%3AA2V0VB04MRA6Z3&dc&page='
#done'Visee':'https://www.amazon.co.jp/s?k=Visee&rh=p_89%3AVisee%28%E3%83%B4%E3%82%A3%E3%82%BB%29&dc&page='
#ddone,'YVES SAINT LAURENT BEAUTE':'https://www.amazon.co.jp/s?k=YVES+SAINT+LAURENT+BEAUTE&i=beauty&rh=p_89%3AYves+Saint+Laurent&dc&page='
#ddone'ZEESEA':'https://www.amazon.co.jp/s?k=ZEESEA&i=beauty&page='
#done'La Roche-Posay(ラロッシュポゼ)':'https://www.amazon.co.jp/s?k=%E3%83%A9+%E3%83%AD%E3%83%83%E3%82%B7%E3%83%A5+%E3%83%9D%E3%82%BC&rh=p_89%3ALa+Roche-Posay%28%E3%83%A9%E3%83%AD%E3%83%83%E3%82%B7%E3%83%A5%E3%83%9D%E3%82%BC%29&dc&page='
'マキアージュ':'https://www.amazon.co.jp/s?k=%E3%83%9E%E3%82%AD%E3%82%A2%E3%83%BC%E3%82%B8%E3%83%A5&rh=p_89%3A%E3%83%9E%E3%82%AD%E3%82%A2%E3%83%BC%E3%82%B8%E3%83%A5+%28MAQUILLAGE%29&dc&page='
#ddone'3CE':'https://www.amazon.co.jp/s?k=3CE&rh=p_89%3A3CE&dc&page='
#ddone'ALBION':'https://www.amazon.co.jp/s?k=ALBION&rh=p_89%3AALBION%7C%E3%82%A2%E3%83%AB%E3%83%93%E3%82%AA%E3%83%B3%28ALBION%29&dc&page='
#ddone'B IDOL':'https://www.amazon.co.jp/s?k=B+IDOL&i=beauty&__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&ref=nb_sb_noss'
#ddone,'BOBBI  BROWN':'https://www.amazon.co.jp/s?k=BOBBI+BROWN&i=beauty&rh=p_89%3ABOBBI+BROWN%EF%BC%88%E3%83%9C%E3%83%93%E3%82%A4%E3%83%96%E3%83%A9%E3%82%A6%E3%83%B3%EF%BC%89&dc&page='
#ddone'CLARINS':'https://www.amazon.co.jp/s?k=CLARINS&rh=p_89%3A%E3%82%AF%E3%83%A9%E3%83%A9%E3%83%B3%E3%82%B9%28CLARINS%29&dc&page='
#ddone'CLINIQUE':'https://www.amazon.co.jp/s?k=CLINIQUE&rh=p_89%3A%E3%82%AF%E3%83%AA%E3%83%8B%E3%83%BC%E3%82%AF+%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A1%E3%83%B3%7C%E3%82%AF%E3%83%AA%E3%83%8B%E3%83%BC%E3%82%AF%28CLINIQUE%29&dc&page='
#ddone,'COSME DECORTE':'https://www.amazon.co.jp/s?k=COSME+DECORTE&rh=p_89%3A%E3%82%B3%E3%82%B9%E3%83%A1%E3%83%87%E3%82%B3%E3%83%AB%E3%83%86%28COSME+DECORTE%29&dc&page='
#ddone,'Curel':'https://www.amazon.co.jp/s?k=Curel&rh=p_89%3ACurel&dc&__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&qid=1589361220&rnid=2321255051&ref=sr_nr_p_89_2'
#ddone'Elégance':'https://www.amazon.co.jp/s?k=El%C3%A9gance&i=beauty&rh=p_89%3A%E3%82%A8%E3%83%AC%E3%82%AC%E3%83%B3%E3%82%B9&dc&page='
#ddone,'ESTEE LAUDER':'https://www.amazon.co.jp/s?k=estee+lauder&i=beauty&rh=p_89%3A%E3%82%A8%E3%82%B9%E3%83%86%E3%82%A3%E3%83%AD%E3%83%BC%E3%83%80%E3%83%BC%28ESTEE+LAUDER%29%7C%E3%82%A8%E3%82%B9%E3%83%86%E3%82%A3%E3%83%BC%E3%83%AD%E3%83%BC%E3%83%80%E3%83%BC&dc&page='
#ddone,'IPSA':'https://www.amazon.co.jp/s?k=IPSA&rh=p_89%3A%E3%82%A4%E3%83%97%E3%82%B5%28IPSA%29&dc&page='
#ddone,'LANCOME':'https://www.amazon.co.jp/s?k=LANCOME&rh=p_89%3ALancome%7C%E3%83%A9%E3%83%B3%E3%82%B3%E3%83%A0%28LANCOME%29&dc&page='
#ddone'NARS':'https://www.amazon.co.jp/s?k=NARS&rh=p_89%3ANARS%EF%BC%88%E3%83%8A%E3%83%BC%E3%82%BA%EF%BC%89%7CNar%27s%7C%E3%83%8A%E3%83%BC%E3%82%BA&dc&page='
#ddone,'rom&nd':'https://www.amazon.co.jp/s?k=rom%26nd&page='
#ddone,'shu uemura':'https://www.amazon.co.jp/s?k=shu+uemura&rh=p_89%3A%E3%82%B7%E3%83%A5%E3%82%A6%E3%82%A6%E3%82%A8%E3%83%A0%E3%83%A9%7C%E3%82%B7%E3%83%A5%E3%82%A6%E3%82%A6%E3%82%A8%E3%83%A0%E3%83%A9%28shu+uemura%29&dc&page='
#ddone,'SUQQU':'https://www.amazon.co.jp/s?k=SUQQU&rh=p_89%3ASUQQU%7CSUQQU+%E3%82%B9%E3%83%83%E3%82%AF&dc&page='
#ddone,'WHOMEE':'https://www.amazon.co.jp/s?k=WHOMEE&i=beauty&__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&ref=nb_sb_noss_2'
#ddone,"Witch's Pouch":'https://www.amazon.co.jp/s?k=Witch%27s+Pouch&i=beauty&rh=p_89%3AWitch%27s+Pouch&dc&page='
}



# 渡すURLをブランドの配列にする
from bs4 import BeautifulSoup
import requests
import time 
import re
import pickle
import csv
import os

ansin_dic = {}

#source_url = "https://www.amazon.co.jp/s?k=%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF&rh=p_89%3A%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF&dc&page="

for brand_name, source_url in source_url_list.items():
    brand_dic = {}
    #time.sleep(5*60)
    
    for i in range(1, 6):
        url = source_url + str(i)
        print(url)
        r = requests.get(url)
        time.sleep(5)
        
        print(r)
        if r.status_code == 200:
            soup = BeautifulSoup(r.content, "html.parser")
            sourse_links = soup.find_all("a", class_="a-link-normal a-text-normal", target="_blank")
            for source_link in sourse_links:
                # 商品のリンクを取得
                product_name = source_link.text
                product_name = product_name.strip("\n")
                # print(product_name)
                link = "https://www.amazon.co.jp" + source_link.get("href")
                r = requests.get(link)
                soup = BeautifulSoup(r.content, "html.parser")
                try : 
                    cerberus_data_metrics = soup.find("div", id="cerberus-data-metrics")
                    asin = cerberus_data_metrics.get("data-asin")
                    brand_dic[asin] = product_name
                except:
                    print("なし")
                
            ansin_dic[brand_name] = brand_dic
    #f = open( file_name,"wb")
    #pickle.dump(ansin_dic,f)
    file_name = brand_name + ".csv"
    path = "./data/" + file_name
    
    if os.path.exists(path):
        print ("存在.")
        with open(path, 'a',encoding='utf-8') as f: #追記する
            writer = csv.writer(f, delimiter=",")
            try:
                data = set(ansin_dic[brand_name].keys())
                data_list =  list(data)
                for data in data_list:
                    writer.writerow([data])
            except:
                print("データなし")
    else:
        print ("非存在")
    
        with open(path, 'w',encoding='utf-8') as f: #追記する
            writer = csv.writer(f, delimiter=",")
            try:
                data_list = list(set(ansin_dic[brand_name].keys()))
                writer.writerow(["ASIN"])
                for data in data_list:
                    writer.writerow([data])
            except:
                print("データなし")
                


