import re
from lxml import etree
import json


class ParseSearchTermUs(object):

    def __init__(self, page_source, driver=None, search_term=None, page=1, site_name='us',
                                         time_batch=None, sp_all=True, etree_html=None):
        self.etree_html = etree_html if etree_html is not None else etree.HTML(page_source)
        self.search_term = search_term
        self._totalResultCount_raw = re.findall(r'totalResultCount.*?\);</script>', page_source) if page_source else []
        print(self._totalResultCount_raw)
        self.page = page
        self.time_batch = time_batch
        self.site_name = site_name
        self.sp_all = sp_all
        # zr和sp才有page和page_row
        self.zr_list = []
        self.sp_list = []
        self.sb_list = []
        self.ac_list = []
        self.bs_list = []
        self.er_list = []
        self.tr_list = []
        self.hr_list = []
        self.buy_text_list = []
        """
        search_term, asin, page, page_row, cate_type,
        title, img, price, rating, reviews
        """
        self.sb_list_all = []
        self.sp_list_all = []
        # 获取搜索的所有结果--只针对page=1
        self.sold_list = []
        # 建立 asin → data-index 位置映射，用于 SP/ZR 的 page_row 排序
        # data-index 是 Amazon 页面上每个搜索结果的真实位置编号
        # 同一个 ASIN 可能出现多次，只取第一次出现的位置
        items = self.etree_html.xpath('//div[@data-index and @data-asin]')
        self.asin_position_map = {}
        for item in items:
            asin = item.get('data-asin', '').strip()
            index = item.get('data-index', '')
            if asin and index:
                if asin not in self.asin_position_map:
                    self.asin_position_map[asin] = int(index)

    def parse_sold_quantity(self):
        if self.page == 1:
            ele_span_list = self.etree_html.xpath(
                '//span[@data-component-type="s-result-info-bar"]//h2/span/text()|//div[@class="a-section a-spacing-small a-spacing-top-small"]//span/text()')
            print("产品总数:", ele_span_list)
            quantity_being_sold_list = []
            if len(ele_span_list) > 0:
                ele_span_list[0] = ele_span_list[0].replace('rgebnissen oder', '').replace('suggestions for',
                                                                                           '').replace(" ", "").replace(
                    "1-48", "").replace("1-16", "").replace(self.search_term, '')
                if len(ele_span_list[0]) < 80:
                    ele_text = ele_span_list[0].replace(".", "").replace(",", "").replace(" ", "").replace("\xa0", "")
                    ele_a = re.findall("\d+-\d+", ele_text)
                    if len(ele_a) == 0:
                        ele_a = re.findall("\d+–\d+", ele_text)
                    if ele_a:
                        ele_text = ele_text.replace(ele_a[0], '')

                    quantity_being_sold_list = re.findall("(\d+)", ele_text)
                    if len(quantity_being_sold_list) > 1:
                        quantity_being_sold_list = [quantity_being_sold_list[-1]]
            else:
                ele_span_list1 = self.etree_html.xpath('//h1//span/text()')
                print('产品总数::::', ele_span_list1)
                if len(ele_span_list1) > 1:
                    ele_span_list1[0] = ele_span_list1[0].replace('Ergebnissen oder', '').replace('suggestions for',
                                                                                                  '').replace(" ",
                                                                                                              "").replace(
                        "1-48", "").replace("1-16", "").replace(self.search_term, '')
                    if len(ele_span_list1[0]) < 80:
                        ele_text = ele_span_list1[0].replace(".", "").replace(",", "").replace(" ", "")
                        ele_b = re.findall("\d+-\d+", ele_text)
                        if len(ele_b) == 0:
                            ele_b = re.findall("\d+–\d+", ele_text)
                        if ele_b:
                            ele_text = ele_text.replace(ele_b[0], '')
                        quantity_being_sold_list = re.findall("(\d+)", ele_text)
            quantity_being_sold = quantity_being_sold_list[0] if quantity_being_sold_list else len(self.zr_list)
            try:
                if ('one result for' in ele_text) or (len(quantity_being_sold) > 7):
                    quantity_being_sold = 1
            except:
                pass
            if ele_span_list:
                quantity_being_sold_str = ele_span_list[0]
            else:
                quantity_being_sold_str = None
            totalResultCount_list = self._totalResultCount_raw
            if totalResultCount_list:
                try:
                    result_count = '{"' + totalResultCount_list[0].replace(');</script>', '')
                    result_dict = json.loads(result_count.replace('\\', ''))
                    quantity_being_sold = result_dict.get('totalResultCount')
                except:
                    result_count = None
            else:
                result_count = None

            print("**********************xxxxxxxxxxxxxxxxxx:", self.search_term, quantity_being_sold, result_count)
            departments_list = self.etree_html.xpath(
                "//div[@id='departments']//span[@class='a-declarative']//a/span/text()")
            if departments_list:
                departments = '|-|'.join(departments_list)
            else:
                departments = None
            self.sold_list.append(
                [self.search_term, quantity_being_sold, quantity_being_sold_str, result_count, departments])

    def parse_asin_zr(self):
        """
        通过 data-index + data-asin 且不含 AdHolder 的 div 获取ZR自然排名ASIN
        """
        # 保留原始全量列表给 parse_buy 用
        asin_all_raw = self.etree_html.xpath('//div[@data-asin]/@data-asin')
        asin_all_str = "-".join(asin_all_raw).replace('/', '')
        self.asin_all = re.findall("(\w+)", asin_all_str)
        # ZR: 主搜索结果中没有 AdHolder 的项
        zr_items = self.etree_html.xpath('//div[@data-index and @data-asin and not(contains(@class, "AdHolder"))]')
        zr_asin_list = []
        for item in zr_items:
            asin = item.get('data-asin', '').strip()
            if asin and len(asin) >= 9 and asin not in zr_asin_list:
                zr_asin_list.append(asin)
        return zr_asin_list

    def parse_type_common(self, asin_list=None, cate_type=None):
        """
        asin_list: list
        """
        asin_list = list(dict.fromkeys(asin_list))  # 去重保序，保持xpath返回的页面顺序
        asin_detail_all_list = []
        cate_type_copy = 1
        asin_detail_dict = {
            "title": None,
            "img": None,
            "price": None,
            "rating": None,
            "reviews": None
        }
        if len(asin_list):
            for idx, asin in enumerate(asin_list):
                asin_detail_list = []
                page_row = idx + 1  # 类型内排名
                asin_detail_list.extend(
                    (self.search_term, asin, self.page, page_row, cate_type_copy,self.time_batch
                                         ))

                asin_detail_list.extend(asin_detail_dict.values())  # 详情字段不入库，统一填None
                asin_detail_all_list.append(asin_detail_list)
        else:
            pass
        return asin_detail_all_list

    def parse_other_type_common(self, cate_type='er', asin=None, page_row=None):
        """
        Parse cate_type: er, tr. hr
        """
        div_list = self.etree_html.xpath('//div[@data-asin="" and @data-index]')
        asin_detail_all_list = []
        for div in div_list:
            h3_list = div.xpath(
                './/h3/text()|.//div[contains(@class,"s-text-uppercase")]//text()|.//div[@class="a-fixed-left-grid"]//span//text()|.//div[@class="sg-row"]//span[contains(@class,"a-size-medium-plus")]/text()')
            if h3_list:
                content = ''.join(h3_list).lower()
                flag = False
                if cate_type == 'er':
                    if self.site_name == 'us':
                        flag = ("editorial" in content) or ("recommendations" in content)
                    elif self.site_name == 'de':
                        flag = ("empfehlungen" in content) or ("verlagspartner" in content)
                    elif self.site_name == 'uk':
                        flag = ("recommended" in content) or ("article" in content)
                    elif self.site_name == 'fr':
                        flag = ("article" in content) or ("recommand" in content)
                    elif self.site_name == 'it':
                        flag = ("articolo" in content) or ("consigliato" in content)
                    elif self.site_name == 'es':
                        flag = ("artículo" in content) or ("recomendado" in content)
                    cate_type = 1
                elif cate_type == 'tr':
                    # us
                    if ("top rated" in content) and ("from our brand" not in content):
                        cate_type = 1
                        flag = True
                    elif ("top rated" in content) and ("from our brand" in content):
                        cate_type = 2
                        flag = True
                    # es
                    elif ("lo mejor de" in content) and ("nuestras marcas" in content):
                        cate_type = 2
                        flag = True
                    # fr
                    elif ("mieux noté parmi" in content) and ("nos marques" in content):
                        cate_type = 2
                        flag = True
                    # it
                    elif ("dai nostri" in content) and ("marchi più amati" in content):
                        cate_type = 2
                        flag = True
                    # de
                    elif ("bestbewertete" in content) and ("eigenprodukte" in content):
                        cate_type = 2
                        flag = True
                    else:
                        flag = False
                elif cate_type == 'ac':
                    flag = ("amazon's" in content) and ("choice" in content)
                    cate_type = 1
                elif cate_type == 'hr':
                    # us uk
                    if ("highly rated" in content):
                        flag = True
                    # de
                    elif ("hoch bewertet" in content):
                        flag = True
                    # fr
                    elif ("bonnes évaluations" in content):
                        flag = True
                    # es
                    elif ("mejor valorados" in content):
                        flag = True
                    # it
                    elif ("valutazione alta" in content):
                        flag = True
                else:
                    flag = False
                if flag:
                    li_list = div.xpath('.//li[@class="a-carousel-card"]')
                    if li_list:
                        for li in li_list:
                            asin_list = li.xpath('.//div[@data-asin]/@data-asin')
                            if asin_list:
                                for asin in asin_list:
                                    asin_detail_list = [self.search_term, asin, self.page, page_row, cate_type,self.time_batch]
                                    asin_detail_list.extend([None, None, None, None, None])
                                    asin_detail_all_list.append(asin_detail_list)
                            break
        return asin_detail_all_list

    def parse_zr(self):
        try:
            asin_list = self.parse_asin_zr()
            self.zr_list.extend(self.parse_type_common(asin_list=asin_list, cate_type='zr'))
        except Exception as e:
            pass

    def parse_buy(self):
        ## 四月替换以下 同时更改表字段
        # 预建 ASIN -> 节点映射，避免每次全树搜索
        asin_node_map = {}
        for node in self.etree_html.xpath('//div[@data-asin]'):
            asin = node.get('data-asin', '').strip()
            if asin and asin not in asin_node_map:
                asin_node_map[asin] = node

        for i in self.asin_all:
            node = asin_node_map.get(i)
            if node is None:
                continue
            buy_text_list = node.xpath(
                './/span[contains(text(),"bought in past")]/text()')
            if len(buy_text_list) == 0:
                buy_text_list = node.xpath(
                    './/div[@class="a-row a-size-base"]/span[@class="a-size-base a-color-secondary"]/text()')
                if len(buy_text_list) == 0:
                    if self.site_name == 'us' or self.site_name == 'uk':
                        buy_text_list = node.xpath(
                            './/span[contains(text(),"past ")]/text()')
                    else:
                        buy_text_list = node.xpath(
                            './/span[contains(text()," im letzten")]/text()')
            label_text_list = node.xpath(
                ".//div[contains(@class,'a-size-base a-color-base')]/a/text()")
            if buy_text_list:
                if len(buy_text_list[0]) < 2:
                    buy_text_list = [None]
            else:
                buy_text_list = [None]

            asin_brand_list = node.xpath('.//h2/following-sibling::div/span/text()|.//div[@data-cy="title-recipe"]//h2/span[@class="a-size-base-plus a-color-base"]/text()')
            if asin_brand_list:
                asin_brand = asin_brand_list[0]
            else:
                asin_brand = None
            if label_text_list:
                if len(label_text_list[0]) < 2:
                    label_text_list = [None]
            else:
                label_text_list = [None]

            if label_text_list[0]:
                while ' ' in label_text_list:
                    label_text_list.remove(' ')
                while '' in label_text_list:
                    label_text_list.remove('')
                label_data = '&&&'.join(label_text_list)
            else:
                label_data = label_text_list[0]
            if label_data or buy_text_list[0]:
                self.buy_text_list.append([self.search_term, i, self.page, buy_text_list[0], label_data, asin_brand,self.time_batch])

    def parse_sp(self):
        """
        通过 AdHolder class 识别 SP 广告位（替代原 label-popover-default 方式）
        AdHolder 只存在于真正的 SP 广告 DOM 上，不会误匹配标签广告位和品牌广告
        """
        try:
            sp_asin_list = []
            # 有 data-index + data-asin + AdHolder 的就是 SP 广告
            items = self.etree_html.xpath('//div[@data-index and @data-asin and contains(@class, "AdHolder")]')
            for item in items:
                asin = item.get('data-asin', '').strip()
                print(self.search_term,' 页数：',self.page,'广告asin：',asin)
                if asin and len(asin) >= 9 and asin not in sp_asin_list:
                    sp_asin_list.append(asin)

            # 标签下的SP广告位（如 Customers frequently viewed、Today's deals）
            if self.site_name == 'de':
                sp_label = 'Gesponsert'
            elif self.site_name == 'us' or self.site_name == 'uk':
                sp_label = 'Sponsored'
            else:
                sp_label = 'Sponsored'
            tag_asin_list = self.etree_html.xpath(
                f'//span[@class="a-declarative"]/span[contains(text(),"{sp_label}")]/../../../../../../../../div/following-sibling::span[2]//div/@data-asin|//span/a[contains(text(),"{sp_label}")]/../../../../../../../../div/following-sibling::span[2]//div/@data-asin')
            if tag_asin_list:
                for asin in tag_asin_list:
                    if asin and len(asin) >= 9 and asin not in sp_asin_list:
                        sp_asin_list.append(asin)

            self.sp_list_all = sp_asin_list.copy()  # 供 parse_asin_zr 排除用
            if sp_asin_list:
                sp_asin_list.sort(key=lambda a: self.asin_position_map.get(a, 9999))
                self.sp_list.extend(self.parse_type_common(asin_list=sp_asin_list, cate_type='sp'))
        except Exception as e:
            pass

    def parse_sb(self, asin=None, page_row=None):
        # 解析头部sb, cate_type = 1 2024-5-9 修改最新获取。一次性获取所有视频后带着三个商品 的asin，
        # 1 获取头部品牌 包括 视频后的asin 2 只获取尾部。3 只获取视频里链接
        # //div[@data-type="brandLogo"]/a//@href  解析品牌视频
        try:
            cate_type = 1
            asin_list = self.etree_html.xpath('//div[@data-index="6"]//div[@class="_bGlmZ_content_2rsXy"]//@data-asin')
            if len(asin_list) == 0:
                asin_list = self.etree_html.xpath(
                    '//div[@data-avar="desc"]//@data-asin|//div[@data-avar="page"]//@data-asin|//div[@data-avar="deal"]//@data-asin')
            # 解析头部视频后面三个asin，只需要三个asin
            _sbv_list = []
            sbv1_asin_list = self.etree_html.xpath('//li//div[@data-type="productContainer"]/@data-asin')
            if sbv1_asin_list:
                if len(sbv1_asin_list) > 0:
                    asin_list.extend(sbv1_asin_list)

                    self.sb_list_all.extend([asin.replace('/', '') for asin in list(set(sbv1_asin_list))])
            else:
                # 既是视频又是品牌
                sbv2_asin_list = self.etree_html.xpath(
                    "//a[@data-type='brandedLogoHeaderLink']/@href|//div[@data-type='brandLogo']/a/@href")
                if sbv2_asin_list:
                    sbv1_asin_href = sbv2_asin_list[0]
                    svb_asins_list = re.findall("https://www.*/dp/(.*?)\?", sbv1_asin_href)
                    if len(svb_asins_list) == 0:
                        svb_asins_list = re.findall("asins=(.*?)&", sbv1_asin_href)
                        if svb_asins_list:
                            if len(svb_asins_list[0]) > 10:
                                svb_asins_list[0] = svb_asins_list[0][:10]
                        else:
                            svb_asins_list = re.findall("asins=(.*?)%2", sbv1_asin_href)
                            if svb_asins_list:
                                if len(svb_asins_list[0]) > 10:
                                    svb_asins_list[0] = svb_asins_list[0][:10]
                    else:
                        if len(svb_asins_list[0]) > 10:
                            svb_asins_list[0] = svb_asins_list[0][:10]
                    _sbv_list.extend(svb_asins_list)
                    self.sb_list_all.extend([asin.replace('/', '') for asin in list(set(svb_asins_list))])

            print('头部品牌:', asin_list, '视频后面三个asin:', sbv1_asin_list, '头部视频品牌:', _sbv_list)
            if asin_list:
                self.sb_list_all.extend([asin.replace('/', '') for asin in list(set(asin_list))])
                if _sbv_list:
                    asin_list.extend(_sbv_list)
                for asin in asin_list:
                    asin_detail_list = [self.search_term, asin, self.page, page_row, cate_type,self.time_batch]
                    asin_detail_list.extend([None, None, None, None, None])
                    self.sb_list.append(asin_detail_list)
        except Exception as e:
            pass
        # 解析尾部sb, cate_type = 2 -- 不符合
        try:
            cate_type = 2
            asin_list = self.etree_html.xpath('//div[@class="sb_1LIJTce6"]//a//@href')
            if not asin_list:
                asin_list = self.etree_html.xpath(
                    '//div[@data-iid="oDeh4GD5fRXSDxLNSBN1Og"]//a[@class="a-link-normal _bXVsd_link_2cNGK _bXVsd_hidden_FUOrV"]/@href|//div[@data-slot="desktop-hsa-3psl"]//div/a[@tabindex="-1"]/@href'
                )
            if not asin_list:
                asin_list = self.etree_html.xpath(
                    '//div[@data-a-display-strategy="sb-search-grid"]//div/@data-asin'
                )
                if asin_list:
                    if len(asin_list) > 6:
                        asin_list = asin_list[6:9]
                    elif len(asin_list) > 3:
                        asin_list = asin_list[3:6]
            else:
                asin_list = [
                    re.findall(r"lp_asins=(.*?)&", a)[0].split('%2C')[0].replace('/', '')
                    for a in asin_list if 'lp_asins' in a
                ]
            print('尾部sb品牌:', asin_list)
            if asin_list:
                self.sb_list_all.extend([asin.replace('/', '') for asin in list(set(asin_list))])  #
                for asin in asin_list:
                    asin_detail_list = [self.search_term, asin.replace('/', ''), self.page, page_row, cate_type,self.time_batch]
                    asin_detail_list.extend([None, None, None, None, None])
                    self.sb_list.append(asin_detail_list)
        except Exception as e:
            pass
        # 解析视频sb, cate_type = 3
        try:
            cate_type = 3
            # 解析视频 只有一个asin的
            if self.site_name == 'us' or self.site_name == 'uk':
                aria_label = 'Sponsored video'
            elif self.site_name == 'de':
                aria_label = 'Gesponsertes Video'
            else:
                aria_label = 'Sponsored video'

            sp_href = self.etree_html.xpath(
                f'//div[@class="a-section a-spacing-none faceout-product-title"]//a//@href|//a[contains(@aria-label,"{aria_label}")]/@href|//div[contains(@class,"sbv-video")]/a/@href|'
                f'//div[@class="a-section a-spacing-none puis-padding-right-small s-title-instructions-style faceout-product-title"]//a//@href|//video[contains(@aria-label,"Sponsored video")]/parent::a/@href')

            # print("解析视频sb, cate_type = 3:", sp_href)
            asin_list = []
            if len(sp_href):
                for href in sp_href:
                    asins = re.findall("https://www.*/dp/(.*?)\?", href)
                    if len(asins) == 0:
                        asins = re.findall("asins=(.*?)&", href)
                        if asins:
                            if len(asins[0]) > 10:
                                asins[0] = asins[0][:10]
                        else:
                            # 优化sbv 正则只匹配英文 数字，判断
                            asins = re.findall("asins=(.*?)%2", href)
                            if asins:
                                if len(asins[0]) > 10:
                                    asins[0] = asins[0][:10]
                    else:
                        if len(asins[0]) > 10:
                            asins[0] = asins[0][:10]
                    asin_list.extend(asins)
                print("解析视频sb sb_3:", asin_list)
                if asin_list:
                    self.sb_list_all.extend([asin.replace('/', '') for asin in list(set(asin_list))])
                    for i in asin_list:
                        asin_detail_list = [self.search_term, i.replace('/', ''), self.page, page_row, cate_type,self.time_batch]
                        asin_detail_list.extend([None, None, None, None, None])
                        self.sb_list.append(asin_detail_list)
            else:
                pass
        except Exception as e:
            import traceback
            print("error:", traceback.format_exc())
        # 解析品牌广告 "Sponsored ad from" / "from Amazon brands"
        # 原属于 parse_sp，但品牌广告本质是 SB 类型，移到此处
        # 前3个 cate_type=1（头部），超出3个的 cate_type=2（尾部）
        try:
            if self.site_name == 'us' or self.site_name == 'uk':
                brand_ad_list = self.etree_html.xpath(
                    '//span[contains(text(),"from Amazon brands")]/../../../../../../../../div//div/@data-csa-c-asin')
            else:
                brand_ad_list = self.etree_html.xpath(
                    '//span[contains(text(),"von Amazon-Marken")]/../../../../../../../../div//div/@data-csa-c-asin')
            if not brand_ad_list:
                # 备用 xpath：通过 aria-label 匹配
                brand_ad_list = self.etree_html.xpath('//a[contains(@aria-label,"Sponsored ad from ")]/..//@data-asin')
                if brand_ad_list:
                    if len(brand_ad_list) == 6:
                        brand_ad_list = brand_ad_list[3:6]
            if brand_ad_list:
                self.sb_list_all.extend([asin.replace('/', '') for asin in list(set(brand_ad_list))])
                for idx, asin in enumerate(brand_ad_list):
                    cate_type = 1 if idx < 3 else 2  # 前3个头部，后续尾部
                    asin_detail_list = [self.search_term, asin.replace('/', ''), self.page, page_row, cate_type,
                                        self.time_batch]
                    asin_detail_list.extend([None, None, None, None, None])
                    self.sb_list.append(asin_detail_list)
        except Exception as e:
            pass

    def parse_ac(self):
        try:
            asin_list = self.etree_html.xpath(
                '//span[@data-a-badge-color="sx-gulfstream" and @aria-hidden="true" ]//@id|//span[@data-a-badge-color="sx-gulfstream"]//@id|//span[contains(@id,"-amazons-choice-label")]/@id|//span[contains(@id,"-amazons-choice")]/@id')
            print('ac_asin_list:::',asin_list)
            if len(asin_list):
                asin_list = [asin.split("-")[0] for asin in asin_list if len(asin.split("-")[0]) >= 9]  # 有重复
                self.ac_list.extend(self.parse_type_common(asin_list=asin_list, cate_type='ac'))
            self.ac_list.extend(self.parse_other_type_common(cate_type='ac'))  # 横栏部分
        except Exception as e:
            pass

    def parse_bs(self):
        try:
            bsr_asin_xpath_list = [
                                   "//div[@data-csa-c-content-id='BEST_SELLER']/parent::div/parent::div/parent::div/parent::span/parent::div//@data-csa-c-asin",
                                   "//div[@data-csa-c-content-id='BEST_SELLER']/parent::div/parent::div/parent::div/parent::div/parent::div//@data-csa-c-asin",
                                   "//div[@data-csa-c-content-id='BEST_SELLER']/parent::div/parent::div/parent::div//@data-csa-c-item-id",
                                   "//div[@data-csa-c-content-id='BEST_SELLER']/parent::div/parent::div/parent::div/parent::span/parent::div//@data-csa-c-item-id",
                                   "//div[@data-csa-c-content-id='BEST_SELLER']/parent::div/parent::div/parent::div/parent::div/parent::div//@data-csa-c-item-id"]
            for bsr_asin_xpath in bsr_asin_xpath_list:
                asin_list = self.etree_html.xpath(bsr_asin_xpath)
                print('############## bsr_asin::', asin_list)
                bsr_asin_list = []
                if len(asin_list):
                    asin_list = [asin.split("-")[0] for asin in asin_list if len(asin.split("-")[0]) >= 9]
                    for asin in asin_list:
                        if len(asin)>10:
                            pattern = re.compile(r'(?<=amzn1\.asin\.)[A-Z0-9]{10}', re.I)
                            asins = pattern.findall(asin)
                            if asins:
                                bsr_asin_list.append(asins[0])
                        else:
                            bsr_asin_list.append(asin)
                    print('############## bsr_asin::', bsr_asin_list)

                    self.bs_list.extend(self.parse_type_common(asin_list=bsr_asin_list, cate_type='bs'))
                    break
        except Exception as e:
            pass

    def parse_er(self):
        self.er_list.extend(self.parse_other_type_common(cate_type='er'))

    def parse_tr(self):
        self.tr_list.extend(self.parse_other_type_common(cate_type='tr'))

    def parse_hr(self):
        self.hr_list.extend(self.parse_other_type_common(cate_type='hr'))

    def parse_detail(self, asin=None, cate_type=None):
        asin_detail_dict = {
            "title": None,
            "img": None,
            "price": None,
            "rating": None,
            "reviews": None
        }
        try:
            asin_img = self.etree_html.xpath('//div[@data-asin="' + asin + '"]//img//@src')  # 可以解析
            if len(asin_img):
                asin_detail_dict['img'] = asin_img[0]
        except Exception as e:
            pass
        try:
            if cate_type == 'sb':
                asin_title = self.etree_html.xpath(
                    f'//div[@data-asin="{asin}"]//a[@data-type="productTitle"]/text()')  # 可以解析
            else:
                asin_title = self.etree_html.xpath(
                    '//div[@data-asin="' + asin + '"]//h2/@aria-label')  # 可以解析
                if (len(asin_title) == 0):
                    asin_title = self.etree_html.xpath(
                        '//div[@data-asin="' + asin + '"]//div[@data-cy="title-recipe"]//text()')  # 失效
            asin_detail_dict['title'] = asin_title[0]
        except Exception as e:
            pass
        try:
            if cate_type == 'sb':
                asin_review = self.etree_html.xpath(
                    f'//a[contains(@aria-label,"Sponsored video") or contains(@href,"{asin}")]//span[contains(@class,"a-size-base")]/text()')
                if len(asin_review) == 0:
                    asin_review = self.etree_html.xpath(
                        f'//a[contains(@href,"{asin}")]//span[contains(@class,"a-size-base")]/text()')
            else:
                asin_review = self.etree_html.xpath(
                    '//div[@data-asin="' + asin + '"]//span[@class="a-size-base"]//text()')
                if len(asin_review) == 0:
                    asin_review = self.etree_html.xpath(
                        '//div[@data-asin="' + asin + '"]//span[@class="a-color-link"]//text()')
                    if len(asin_review) == 0:
                        asin_review = self.etree_html.xpath(
                            f'//div[@data-asin="{asin}"]//div[@data-csa-c-slot-id="alf-reviews"]/a/@aria-label')
            asin_detail_dict['reviews'] = asin_review[0]
        except Exception as e:
            pass
        try:
            if cate_type == 'sb':
                asin_rating = self.etree_html.xpath(
                    f'//a[contains(@href,"{asin}")]/parent::div/parent::div/parent::div/parent::div//span/@aria-label')
            else:
                asin_rating = self.etree_html.xpath(
                    '//div[@data-asin="' + asin + '"]//span[@class="a-icon-alt"]//text()')
            asin_detail_dict['rating'] = asin_rating[0]
        except Exception as e:
            pass
        try:
            if cate_type == 'sb':
                asin_price = self.etree_html.xpath(f'//a[contains(@href,"{asin}")]/span[@class="a-price"]/span/text()')
            else:
                asin_price = self.etree_html.xpath(
                    '//div[@data-asin="' + asin + '"]//span[@class="a-offscreen"]//text()')
            asin_detail_dict['price'] = asin_price[0]
        except Exception as e:
            pass
        return asin_detail_dict

    def run(self):
        self.parse_sb()  # 优先级1 -- 排除时只排除头部广告位的asin
        self.parse_sp()  # 优先级2 -- 排除包含sp的asin
        self.parse_zr()  # 优先级3 -- 通过data-asin取到的所有asin，排除sb、sp
        self.parse_ac()
        self.parse_bs()
        self.parse_er()
        self.parse_tr()
        self.parse_hr()
        self.parse_buy()
        self.parse_sold_quantity()
        print("self.list_zr:", len(self.zr_list), '页数：', self.page, 'page')
        print("self.list_sp:", len(self.sp_list))
        print('self.hr_list::', len(self.hr_list))
        # print("self.list_sb:", self.sb_list)
        "https://www.amazon.co.uk/dp/B09FLQD7VN?pd_rd_i=B09FLQD7VN&pd_rd_w=GwsFh&pf_rd_p=88aa1216-6e73-4bd1-9903-e6883ff8dae3&pd_rd_wg=2kZM8&pf_rd_r=P8P1KCGMPXS9XWH1NFQV&pd_rd_r=a7c81c84-a2aa-47ad-8bd9-055c75c99a28"
        "https://www.amazon.co.uk/dp/B09FLQD7VN?pd_rd_i=B09FLQD7VN&pd_rd_w=GwsFh&pf_rd_p=88aa1216-6e73-4bd1-9903-e6883ff8dae3&pd_rd_wg=2kZM8&pf_rd_r=P8P1KCGMPXS9XWH1NFQV&pd_rd_r=a7c81c84-a2aa-47ad-8bd9-055c75c99a28"
        return (self.zr_list, self.sp_list, self.sb_list, self.ac_list,
                self.bs_list, self.er_list, self.tr_list, self.sold_list, self.buy_text_list, self.hr_list)

if __name__ == '__main__':
    with open(r'C:\Users\ASUS\Desktop\text.html','r',encoding='utf-8')as f:
        response = f.read()
    parse_search_term = ParseSearchTermUs(page_source=response, driver=None, search_term='keywords',
                                          page=1, site_name='us')
    st_list = parse_search_term.run()
    zr_list, sp_list, sb_list, ac_list, bs_list, er_list, tr_list, sort_list, buy_text_list, hr_list = st_list
    print( 'sp_list:',sp_list)
    print('zr_list:' ,zr_list)
    print('sb_list:' ,sb_list)



