# import sys
# import os
# import pandas as pd
# sys.path.append(os.path.dirname(sys.path[0]))  # 上级目录
# import requests
# import json
# from lxml import etree
# import re
# import random
# import pymysql
# import uuid
# import time
# # from amazon_params import py_ja3
# # from amazon_params.params import DB_CONN_DICT
# import urllib3
# import traceback
# urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#
# import requests
# import json
# import os
# os.environ['NO_PROXY'] = 'stackoverflow.com'
# sess = requests.Session()
# from sqlalchemy import Column, Integer, String, ForeignKey, create_engine
# from datetime import datetime, timedelta
# from all_connect import ConnectSpider
# Con = ConnectSpider()
# from switch_ip import pppoe_ip
# import threading
#
#
# def dict_cookie(cookie_str,keys_of_interest):
#     '''提取需要的cookie并转换成字典'''
#     cookie_pattern = re.compile(r'(\w+)=(.*?);')
#     cookie_part = {}
#     for match in cookie_pattern.finditer(cookie_str):
#         key, value = match.groups()
#         if key in keys_of_interest:
#             cookie_part[key] = value
#     return cookie_part
#
# def get_cookie():
#     cookies = {
#         '__uzma': '38b8c2c8-e0b1-4721-ae1b-130d45743b75',
#         '__uzmb': '1718249396',
#         '__uzme': '3867',
#         'AMP_MKTG_f93443b04c': 'JTdCJTdE',
#         '__ssds': '2',
#         '__ssuzjsr2': 'a9be0cd8e',
#         '__uzmaj2': '7f1344de-a158-4f1c-a065-f0a99eaf6cc8',
#         '__uzmbj2': '1718249406',
#         'ak_bmsc': '00D1B9A8275BABADAC8F456013074E31~000000000000000000000000000000~YAAQnetGaK+9AgaQAQAA6zFAEBi0aQGZtmZhHjJgG4BsoO2+z6/tPgDigr72j3a3rsbYmS22kuSqt4qjSwgUxVYUU+xh5jms5bTKtlam8wtiaY0AjtUMfEWla4BXlWUh0Zo8jTuPDZQaDwG/c/9LolLyDGlZIgCmTa/kOdectm63xx4KhnfawVobI9SvhuP86wh66t8YZq77GZBBBKE32/Jf23D3B6avL3lnWbVqqMLhPx9TAYy0M61OjnGG5Xr/RwE522JX9u1Hbc+LQVMBaRgMr/y74HkRihN+Bg44m0iVbJdy3dG138LhiPDn/CYEnfc0tF/eyFpD0wyjDmU4euwC2VePDs9FZyDBFILfPQXGPRQGus/Pa8UmFsFcJRrnY+eBelXDLA==',
#         '__gads': 'ID=4be7ae7de57de345:T=1718249413:RT=1718260935:S=ALNI_MZAdj6jZPRzdT1jGzZHWHf7HKDDKQ',
#         '__gpi': 'UID=00000e491b28f161:T=1718249413:RT=1718260935:S=ALNI_MY02_mr0eALTdxUUBrz-kFZb5X1OA',
#         '__eoi': 'ID=54886735921bf6af:T=1718249413:RT=1718260935:S=AA-AfjZPSRDtde6gs8JvbpXsfMAW',
#         '__deba': 'tX07czX9B8IZjOTrpzC69bX6NQLheDZaWUoVRZ92ET5HmhmRgxKrpsfNQBvaLMPZ3Py7dWJDjsWI46iesYyRC5KnYZh-QLQop7CfMgiydxHbBPQ_jjoPIJw1Mp2KAUXzfh_RC1uMH8PqP2XDcwllqw==',
#         '__uzmc': '853444025942',
#         '__uzmd': '1718260954',
#         '__uzmf': '7f60008f4bd826-72b7-4226-b940-90175e73cca7171824939609811558087-85b5e86cc8b014b240',
#         '__uzmcj2': '784073139079',
#         '__uzmdj2': '1718260954',
#         'AMP_f93443b04c': 'JTdCJTIyZGV2aWNlSWQlMjIlM0ElMjJjNDQ0YzllNy1iYjQxLTRkMjktYTg0ZS0xZGUwYzNiNTY2MTIlMjIlMkMlMjJzZXNzaW9uSWQlMjIlM0ExNzE4MjU4NTY2ODgwJTJDJTIyb3B0T3V0JTIyJTNBZmFsc2UlMkMlMjJsYXN0RXZlbnRUaW1lJTIyJTNBMTcxODI2MTA4NDI4OCUyQyUyMmxhc3RFdmVudElkJTIyJTNBOTMlMkMlMjJwYWdlQ291bnRlciUyMiUzQTM4JTdE',
#         'ebay': '%5Epsi%3DAVXR8uxg*%5Ejs%3D1%5Esbf%3D%23%5E',
#         'dp1': 'bpbf/%23e000e00000000000000000684bc87d^u1p/QEBfX0BAX19AQA**6a2cfbfd^bl/HK6a2cfbfd^',
#         'ns1': 'BAQAAAY/q2Fg8AAaAANgAU2hLyH1jNjl8NjAxXjE3MTgyNDk0MDU2MTdeXjFeM3wyfDV8NHw3fDEwfDQyfDQzfDExXl5eNF4zXjEyXjEyXjJeMV4xXjBeMV4wXjFeNjQ0MjQ1OTA3NQ42fODucFMIIGyJo2OqU8FeZ4vG',
#         's': 'CgAD4ACBma+Z9MGZhNTEyYTExOTAwYThkNjg2YmFkOTY1ZmZmZmFkZTagLiZz',
#         'nonsession': 'BAQAAAY/q2Fg8AAaAAMoAIGos+/0wZmE1MTJhMTE5MDBhOGQ2ODZiYWQ5NjVmZmZmYWRlNgAzAARoS8h9LFVLUgDLAAJmapwFMzIyu/5IujWfmmDQCz/eHWDxIZ7m4Q**',
#         'bm_sv': '502B7639F4A8B55DBB2A44EDCF86ADBB~YAAQG/AgF4BABfKPAQAABgBWEBgSEXJwAOfUOg5WS8dH4sEeLmSiWEIkWzeWM1EcIcstdVgxcBDfu7NtHK4JXx5yzx7o0/ClmM7x5obarqEh3WPP9+6oyiGu1a7Pvx4arUr9nV2Rdz5gljtXcUEuPnp3L+G0uR57JLmDMg+8AVQQsB7mzW81OBTs43hJMni4yKlepWU8t7t+mTuzWV4ZjKJykAGAZuSCxwwBoi/BaNatf55kXnxyaRs+3ekxy7Q=~1',
#         'ds2': 'asotr/b8_5azzzzzzz^sotr/b8_5azzzzzzz^',
#     }
#     headers = {
#         'accept': '*/*',
#         'accept-language': 'zh-CN,zh;q=0.9',
#         'content-type': 'text/plain',
#         'origin': 'https://www.ebay.com',
#         'referer': 'https://www.ebay.com/sch/i.html?_from=R40&_trksid=p4432023.m570.l1313&_nkw=fathers+day+gifts&_sacat=0',
#         'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
#         'sec-ch-ua-full-version': '"123.0.6312.59"',
#         'sec-ch-ua-mobile': '?0',
#         'sec-ch-ua-model': '""',
#         'sec-ch-ua-platform': '"Windows"',
#         'sec-ch-ua-platform-version': '"10.0.0"',
#         'sec-fetch-dest': 'empty',
#         'sec-fetch-mode': 'cors',
#         'sec-fetch-site': 'same-origin',
#         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
#     }
#
#     url = "https://www.ebay.com/gh/setuserpreference"
#     params = {
#         "_from": "R40",
#         "_pgn": "1",
#         "_nkw": "fathers+day+gifts",
#         "_sacat": "1",
#         "correlation": "si=0faf344d1900a0f2882bcc17fff86f88,c=1,operationId=2351460,trk-gflgs=QAE*",
#         "v": "2"
#     }
#     data = {
#         "userPreferedCountry": "USA"
#     }
#     data = json.dumps(data, separators=(',', ':'))
#     response = sess.post(url, headers=headers, cookies=cookies,data=data,params=params, verify=False)
#
#     ''' 获取's', 'ebay', 'ns1', 'nonsession', 'dp1', 'ds2', 'ak_bmsc','bm_sv'''
#     cookie_str = response.headers.get('set-cookie')
#     keys_of_interest = ['s', 'ebay', 'ns1', 'nonsession', 'dp1', 'ds2', 'ak_bmsc']
#     cookie_part1 = dict_cookie(cookie_str, keys_of_interest)
#     cookie_part2 = {key: re.search(rf'{key}=([^;]+);', cookie_str).group(1) for key in ['bm_sv'] if re.search(rf'{key}=([^;]+);', cookie_str)}
#     # 结合
#     cookie_dict = {**cookie_part1, **cookie_part2}
#     # print(cookie_dict)
#
#     '''获取 __uzma|__uzmb|__uzmc|__uzmd|__uzme|__uzmf '''
#     response = sess.get('https://www.ebay.com/sch/ajax/bos_items', cookies=cookie_dict, headers=headers)
#     cookie_string = response.headers['set-cookie']
#     pattern = re.compile(r'(__uzma|__uzmb|__uzmc|__uzmd|__uzme|__uzmf|__deba)=(.*?);')
#     uz_cookies = {match.group(1): match.group(2) for match in pattern.finditer(cookie_string)}
#     keys_of_interest = ['s', 'ebay', 'ns1', 'nonsession', 'dp1', 'ds2', 'ak_bmsc']
#     cookie_part3 = dict_cookie(cookie_string, keys_of_interest)
#     result = {**cookie_part3, **uz_cookies}
#     cookie_dict = {**cookie_dict, **result}
#     # print(cookie_dict)
#
#     '''获取 test_cookie '''
#     response = sess.get('https://securepubads.g.doubleclick.net/gampad/ads',  cookies=cookie_dict, headers=headers)
#     cookie_securepubads = response.headers['Set-Cookie']
#     keys_of_interest = ['test_cookie']
#     cookie_part4 = dict_cookie(cookie_securepubads, keys_of_interest)
#     cookie_securepubads = {**cookie_dict, **cookie_part4}
#     # print(cookie_securepubads)
#
#     params = ''
#     data = 'cid=bjp5&et=82&JSinfo=%7B%22j201%22%3A%228%22%2C%22j202%22%3A%222%22%2C%22j203%22%3A%22t%22%2C%22j204%22%3A%22t%22%2C%22j205%22%3A%22f1%22%2C%22j206%22%3A%22t%22%2C%22j207%22%3A%22gum%22%2C%22j257%22%3A%22gumWeb%22%2C%22j258%22%3A%22f1%22%2C%22j259%22%3A%22f1%22%2C%22j208%22%3A%22t%22%2C%22j209%22%3A%22t%22%2C%22j210%22%3A%22t%22%2C%22j211%22%3A%22Google%20Inc.%22%2C%22j212%22%3A%22t%22%2C%22j213%22%3A%22t%22%2C%22j214%22%3A%22t%22%2C%22j215%22%3A%22t%22%2C%22j216%22%3A%22f0%22%2C%22j217%22%3A%224294705152%22%2C%22j218%22%3A%2237636648%22%2C%22j219%22%3A%2241796228%22%2C%22j222%22%3A%220%22%2C%22j223%22%3A%220%22%2C%22j224%22%3A%2224%22%2C%22j225%22%3A%229%22%2C%22j226%22%3A%22true%22%2C%22j227%22%3A%22falsehid%22%2C%22j228%22%3A%22t%22%2C%22j229%22%3A%22https%3A%22%2C%22j230%22%3A%22-480%22%2C%22j231%22%3A%22f0%22%2C%22j232%22%3A%22f0%22%2C%22j233%22%3A%22f0%22%2C%22j235%22%3A%223.186%22%2C%22j240%22%3A%22t%22%2C%22j241%22%3A%22f0%22%2C%22j242%22%3A%22f0%22%2C%22j243%22%3A%22f0%22%2C%22j244%22%3A%22t%22%2C%22j245%22%3A%22landscape-primary%22%2C%22j246%22%3A%2216px%22%2C%22j247%22%3A%22pppp%22%2C%22j248%22%3A%22pNpN%22%2C%22j249%22%3A%22xhr%22%2C%22j250%22%3A%22t%22%2C%22j251%22%3A%22t%22%2C%22j252%22%3A%22t%22%2C%22j253%22%3A%22t%22%2C%22j255%22%3A%22f0%22%2C%22j256%22%3A%22bjp5%22%2C%22j277%22%3A%225%22%2C%22j278%22%3A%22t%22%2C%22j279%22%3A%22t%22%2C%22j260%22%3A%22t%22%2C%22j261%22%3A%22t%22%2C%22j262%22%3A%22t%22%2C%22j263%22%3A%22t%22%2C%22j264%22%3A%22t%22%2C%22j265%22%3A%22t%22%2C%22j266%22%3A%22f%22%2C%22j267%22%3A%223%22%2C%22j268%22%3A%22t%22%2C%22j269%22%3A%22t%22%2C%22j270%22%3A%228%22%2C%22j271%22%3A%227%22%2C%22j272%22%3A%2210%22%2C%22j273%22%3A%22t%22%2C%22j274%22%3A%22t%22%2C%22j275%22%3A%22t%22%2C%22j276%22%3A%22t%22%2C%22j280%22%3A%22t%22%2C%22j281%22%3A%22f1%22%2C%22j282%22%3A%22f0%22%2C%22j283%22%3A%22f0%22%2C%22j285%22%3A%2211.0.25%22%2C%22j288%22%3A%22Mozilla%2F5.0%20(Windows%20NT%2010.0%3B%20Win64%3B%20x64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F123.0.0.0%20Safari%2F537.36%22%7D'
#
#     response = requests.post('https://cas.avalon.perfdrive.com/jsdata', params=params, headers=headers, data=data)
#     res = json.loads(response.text)
#     cookie = {**cookie_securepubads, **{k: str(v) for k, v in res.items()}}
#     # print(cookie)
#
#     search_terms = ['fathers day gifts']
#     for i in search_terms:
#         page = 1
#         params = {
#             '_from': 'R40',
#             '_fcid': '1',
#             '_nkw': f'{i}',
#             '_sacat': '1',
#             'rt': 'nc',
#             '_pgn': f'{page}',
#         }
#         response = requests.get('https://www.ebay.com/sch/i.html', params=params, headers=headers,cookies=cookie)
#         pattern = r'"text":"United States - USA"'
#         matches = re.findall(pattern, response.text)
#         len_cookie = len(matches)
#         print(cookie)
#         item = {}
#         if len_cookie == 2:
#             item['cookie_value'] = str(cookie)
#             item['created_time'] = datetime.now()
#             Con.save_ebay_cookies(item)
#         else:
#             print('不是us站点')
#
#         # print(re.findall('"countryGroup":(.*?)Select country"}', response.text))
#         # with open('get_pic.py', 'w',encoding='utf-8') as f:
#         #     f.write(response.text)
# # get_cookie()
#
#
# # pppoe_ip()
# # cookie_num = 0
# # start_time = datetime.now()
# # while cookie_num < 1000:
# #     try:
# #         print(f'第{cookie_num + 1}个cookie')
# #         get_cookie()
# #         cookie_num += 1
# #         current_time = datetime.now()
# #         elapsed_time = current_time - start_time
# #         if elapsed_time >= timedelta(minutes=10):
# #             print('时间超过5分钟，切换IP')
# #             pppoe_ip()
# #             start_time = current_time  # 重置开始时间
# #
# #     except Exception as e:
# #         print(e)
#
# cookie_list = Con.get_ebay_cookies()
# print(len(cookie_list))
# print(cookie_list)
# # print(cookie_list)
# with open('tt.py','w',encoding='utf-8') as f:
#     f.write(str(cookie_list))
import sys
import os
import pandas as pd
sys.path.append(os.path.dirname(sys.path[0]))  # 上级目录
import requests
import json
from lxml import etree
import re
import random
import pymysql
import uuid
import time
# from amazon_params import py_ja3
# from amazon_params.params import DB_CONN_DICT
import urllib3
import traceback
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

import requests
import json
import os
os.environ['NO_PROXY'] = 'stackoverflow.com'
sess = requests.Session()
from sqlalchemy import Column, Integer, String, ForeignKey, create_engine
from datetime import datetime, timedelta
from all_connect import ConnectSpider
Con = ConnectSpider()
from switch_ip import pppoe_ip
import threading


def dict_cookie(cookie_str,keys_of_interest):
    '''提取需要的cookie并转换成字典'''
    cookie_pattern = re.compile(r'(\w+)=(.*?);')
    cookie_part = {}
    for match in cookie_pattern.finditer(cookie_str):
        key, value = match.groups()
        if key in keys_of_interest:
            cookie_part[key] = value
    return cookie_part

def get_cookie():
    cookies = {
        '__uzma': '38b8c2c8-e0b1-4721-ae1b-130d45743b75',
        '__uzmb': '1718249396',
        '__uzme': '3867',
        'AMP_MKTG_f93443b04c': 'JTdCJTdE',
        '__ssds': '2',
        '__ssuzjsr2': 'a9be0cd8e',
        '__uzmaj2': '7f1344de-a158-4f1c-a065-f0a99eaf6cc8',
        '__uzmbj2': '1718249406',
        'ak_bmsc': '00D1B9A8275BABADAC8F456013074E31~000000000000000000000000000000~YAAQnetGaK+9AgaQAQAA6zFAEBi0aQGZtmZhHjJgG4BsoO2+z6/tPgDigr72j3a3rsbYmS22kuSqt4qjSwgUxVYUU+xh5jms5bTKtlam8wtiaY0AjtUMfEWla4BXlWUh0Zo8jTuPDZQaDwG/c/9LolLyDGlZIgCmTa/kOdectm63xx4KhnfawVobI9SvhuP86wh66t8YZq77GZBBBKE32/Jf23D3B6avL3lnWbVqqMLhPx9TAYy0M61OjnGG5Xr/RwE522JX9u1Hbc+LQVMBaRgMr/y74HkRihN+Bg44m0iVbJdy3dG138LhiPDn/CYEnfc0tF/eyFpD0wyjDmU4euwC2VePDs9FZyDBFILfPQXGPRQGus/Pa8UmFsFcJRrnY+eBelXDLA==',
        '__gads': 'ID=4be7ae7de57de345:T=1718249413:RT=1718260935:S=ALNI_MZAdj6jZPRzdT1jGzZHWHf7HKDDKQ',
        '__gpi': 'UID=00000e491b28f161:T=1718249413:RT=1718260935:S=ALNI_MY02_mr0eALTdxUUBrz-kFZb5X1OA',
        '__eoi': 'ID=54886735921bf6af:T=1718249413:RT=1718260935:S=AA-AfjZPSRDtde6gs8JvbpXsfMAW',
        '__deba': 'tX07czX9B8IZjOTrpzC69bX6NQLheDZaWUoVRZ92ET5HmhmRgxKrpsfNQBvaLMPZ3Py7dWJDjsWI46iesYyRC5KnYZh-QLQop7CfMgiydxHbBPQ_jjoPIJw1Mp2KAUXzfh_RC1uMH8PqP2XDcwllqw==',
        '__uzmc': '853444025942',
        '__uzmd': '1718260954',
        '__uzmf': '7f60008f4bd826-72b7-4226-b940-90175e73cca7171824939609811558087-85b5e86cc8b014b240',
        '__uzmcj2': '784073139079',
        '__uzmdj2': '1718260954',
        'AMP_f93443b04c': 'JTdCJTIyZGV2aWNlSWQlMjIlM0ElMjJjNDQ0YzllNy1iYjQxLTRkMjktYTg0ZS0xZGUwYzNiNTY2MTIlMjIlMkMlMjJzZXNzaW9uSWQlMjIlM0ExNzE4MjU4NTY2ODgwJTJDJTIyb3B0T3V0JTIyJTNBZmFsc2UlMkMlMjJsYXN0RXZlbnRUaW1lJTIyJTNBMTcxODI2MTA4NDI4OCUyQyUyMmxhc3RFdmVudElkJTIyJTNBOTMlMkMlMjJwYWdlQ291bnRlciUyMiUzQTM4JTdE',
        'ebay': '%5Epsi%3DAVXR8uxg*%5Ejs%3D1%5Esbf%3D%23%5E',
        'dp1': 'bpbf/%23e000e00000000000000000684bc87d^u1p/QEBfX0BAX19AQA**6a2cfbfd^bl/HK6a2cfbfd^',
        'ns1': 'BAQAAAY/q2Fg8AAaAANgAU2hLyH1jNjl8NjAxXjE3MTgyNDk0MDU2MTdeXjFeM3wyfDV8NHw3fDEwfDQyfDQzfDExXl5eNF4zXjEyXjEyXjJeMV4xXjBeMV4wXjFeNjQ0MjQ1OTA3NQ42fODucFMIIGyJo2OqU8FeZ4vG',
        's': 'CgAD4ACBma+Z9MGZhNTEyYTExOTAwYThkNjg2YmFkOTY1ZmZmZmFkZTagLiZz',
        'nonsession': 'BAQAAAY/q2Fg8AAaAAMoAIGos+/0wZmE1MTJhMTE5MDBhOGQ2ODZiYWQ5NjVmZmZmYWRlNgAzAARoS8h9LFVLUgDLAAJmapwFMzIyu/5IujWfmmDQCz/eHWDxIZ7m4Q**',
        'bm_sv': '502B7639F4A8B55DBB2A44EDCF86ADBB~YAAQG/AgF4BABfKPAQAABgBWEBgSEXJwAOfUOg5WS8dH4sEeLmSiWEIkWzeWM1EcIcstdVgxcBDfu7NtHK4JXx5yzx7o0/ClmM7x5obarqEh3WPP9+6oyiGu1a7Pvx4arUr9nV2Rdz5gljtXcUEuPnp3L+G0uR57JLmDMg+8AVQQsB7mzW81OBTs43hJMni4yKlepWU8t7t+mTuzWV4ZjKJykAGAZuSCxwwBoi/BaNatf55kXnxyaRs+3ekxy7Q=~1',
        'ds2': 'asotr/b8_5azzzzzzz^sotr/b8_5azzzzzzz^',
    }
    headers = {
        'accept': '*/*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'content-type': 'text/plain',
        'origin': 'https://www.ebay.com',
        'referer': 'https://www.ebay.com/sch/i.html?_from=R40&_trksid=p4432023.m570.l1313&_nkw=fathers+day+gifts&_sacat=0',
        'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
        'sec-ch-ua-full-version': '"123.0.6312.59"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-model': '""',
        'sec-ch-ua-platform': '"Windows"',
        'sec-ch-ua-platform-version': '"10.0.0"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
    }

    url = "https://www.ebay.com/gh/setuserpreference"
    params = {
        "_from": "R40",
        "_pgn": "1",
        "_nkw": "fathers+day+gifts",
        "_sacat": "1",
        "correlation": "si=0faf344d1900a0f2882bcc17fff86f88,c=1,operationId=2351460,trk-gflgs=QAE*",
        "v": "2"
    }
    data = {
        "userPreferedCountry": "USA"
    }
    data = json.dumps(data, separators=(',', ':'))
    response = sess.post(url, headers=headers, cookies=cookies,data=data,params=params, verify=False)

    ''' 获取's', 'ebay', 'ns1', 'nonsession', 'dp1', 'ds2', 'ak_bmsc','bm_sv'''
    cookie_str = response.headers.get('set-cookie')
    keys_of_interest = ['ak_bmsc']
    cookie_part1 = dict_cookie(cookie_str, keys_of_interest)
    cookie_part2 = {key: re.search(rf'{key}=([^;]+);', cookie_str).group(1) for key in ['bm_sv'] if re.search(rf'{key}=([^;]+);', cookie_str)}
    # 结合
    cookie_dict = {**cookie_part1, **cookie_part2}
    # print(cookie_dict)

    '''获取 __uzma|__uzmb|__uzmc|__uzmd|__uzme|__uzmf '''
    response = sess.get('https://www.ebay.com/sch/ajax/bos_items', cookies=cookie_dict, headers=headers)
    cookie_string = response.headers['set-cookie']
    pattern = re.compile(r'(__deba)=(.*?);')
    uz_cookies = {match.group(1): match.group(2) for match in pattern.finditer(cookie_string)}
    keys_of_interest = ['ak_bmsc']
    cookie_part3 = dict_cookie(cookie_string, keys_of_interest)
    result = {**cookie_part3, **uz_cookies}
    cookie_dict = {**cookie_dict, **result}

    search_terms = ['fathers day gifts']
    for i in search_terms:
        page = 1
        params = {
            '_from': 'R40',
            '_fcid': '1',
            '_nkw': f'{i}',
            '_sacat': '1',
            'rt': 'nc',
            '_pgn': f'{page}',
        }
        response = requests.get('https://www.ebay.com/sch/i.html', params=params, headers=headers,cookies=cookie_dict)
        # print(response.text)
        pattern = r'"text":"United States - USA"'
        matches = re.findall(pattern, response.text)
        len_cookie = len(matches)
        # print(111111111111111111111111111)
        print(cookie_dict)
        print(222222222222222222)
        print(len_cookie)
        item = {}
        if len_cookie == 2:
            item['cookie_value'] = str(cookie_dict)
            item['created_time'] = datetime.now()
            Con.save_ebay_cookies(item)
        else:
            print('不是us站点')

        # print(re.findall('"countryGroup":(.*?)Select country"}', response.text))
        # with open('get_pic.py', 'w',encoding='utf-8') as f:
        #     f.write(response.text)
# get_cookie()


# pppoe_ip()
# cookie_num = 0
# start_time = datetime.now()
# while cookie_num < 1000:
#     try:
#         print(f'第{cookie_num + 1}个cookie')
#         get_cookie()
#         cookie_num += 1
#         current_time = datetime.now()
#         elapsed_time = current_time - start_time
#         if elapsed_time >= timedelta(minutes=10):
#             print('时间超过5分钟，切换IP')
#             pppoe_ip()
#             start_time = current_time  # 重置开始时间
#
#     except Exception as e:
#         print(e)



cookie_list = Con.get_ebay_cookies()
print(len(cookie_list))

with open('tt1.py','w',encoding='utf-8') as f:
    f.write(str(cookie_list))
