import requests,re,time,os,datetime,random
import pandas as pd
from selenium import webdriver
from bs4 import BeautifulSoup
import redis

# headers = {
#     "Cookie":"currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2Fdata%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%25202021%25202020%25202019%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26rnd%3D1663641959596; PGS=10; _pk_id.93.72ee=ee83303e45a089a1.1663061058.; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=cn_EP; _pk_ses.93.72ee=1; LevelXLastSelectedDataSource=EPODOC; menuCurrentSearch=%2F%2Fworldwide.espacenet.com%2FsearchResults%3FAB%3D%26AP%3D%26CPC%3D%26DB%3DEPODOC%26IC%3D%26IN%3D%26PA%3Dapple%26PD%3D2022%26PN%3D%26PR%3D%26ST%3Dadvanced%26Submit%3D%E6%A3%80%E7%B4%A2%26TI%3D%26locale%3Dcn_EP; currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26Submit%3D%25E6%25A3%2580%25E7%25B4%25A2; JSESSIONID=qnLt7d5QgBtpUGjMmHuD-kwJ.espacenet_levelx_prod_2",
#     "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
# }
df_all = pd.read_excel('D:\\kkwork\\jupyter\\专利数量\\t1.xlsx')
# for i in range(2022,1890,-1):
#     df_all[f'{i}'] = ''

# df_all['Espacenet专利检索'] = ''
headers = {
    "Cookie": "currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2Fdata%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%25202021%25202020%25202019%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26rnd%3D1663641959596; PGS=10; _pk_id.93.72ee=ee83303e45a089a1.1663061058.; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=cn_EP; _pk_ses.93.72ee=1; LevelXLastSelectedDataSource=EPODOC; menuCurrentSearch=%2F%2Fworldwide.espacenet.com%2FsearchResults%3FAB%3D%26AP%3D%26CPC%3D%26DB%3DEPODOC%26IC%3D%26IN%3D%26PA%3Dapple%26PD%3D2022%26PN%3D%26PR%3D%26ST%3Dadvanced%26Submit%3D%E6%A3%80%E7%B4%A2%26TI%3D%26locale%3Dcn_EP; currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26Submit%3D%25E6%25A3%2580%25E7%25B4%25A2; JSESSIONID=qnLt7d5QgBtpUGjMmHuD-kwJ.espacenet_levelx_prod_2",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
}

for i in range(len(df_all['英文名称'])):

    for num in range(0, 2):
        try:
            if '中国' not in df_all['企业所属国家'][i]:
                com_name = df_all['英文名称'][i]
                num_zhuanli = 0

                url1 = f'https://worldwide.espacenet.com/data/searchResults?ST=singleline&locale=cn_EP&submitted=true&DB=&query={com_name}&rnd=' + str(
                    int(float(time.time()) * 1000))

                res1 = requests.get(url1, headers=headers)
                soup1 = BeautifulSoup(res1.content, 'html.parser')

                num_text = soup1.find('p', {'class': 'numResultsFoundMsg'}).text

                # try:
                #     zhuanli = re.findall("约(.*?)个", num_text)[0].replace(',', '')
                # except:
                #     zhuanli = re.findall("多于(.*?)个", num_text)[0].replace(',', '')
                zhuanli = '10000'
                if zhuanli == '10000':
                    for year in range(2023, 1900, -1):
                        # url = f'https://worldwide.espacenet.com/data/searchResults?submitted=true&locale=cn_EP&DB=EPODOC&ST=advanced&TI=&AB=&PN=&AP=&PR=&PD={year}&PA={com_name}&IN=&CPC=&IC=&rnd=' + str(
                        #     int(float(time.time()) * 1000))
                        url = 'https://worldwide.espacenet.com/data/searchResults?submitted=true&locale=cn_EP&DB=EPODOC&ST=advanced&TI=&AB=&PN=&AP=&PR=&PD=2022&PA=APPLE&IN=&CPC=&IC=&rnd=1703643229331'
                        res = requests.get(url, headers=headers)
                        soup = BeautifulSoup(res.content, 'html.parser')

                        num_text = soup.find('p', {'class': 'numResultsFoundMsg'}).text
                        try:
                            try:
                                zhuanli2 = int(re.findall("约(.*?)个", num_text)[0].replace(',', ''))
                            except:
                                zhuanli2 = int(re.findall("多于(.*?)个", num_text)[0].replace(',', ''))
                        except:
                            zhuanli2 = int(re.findall("找到(.*?)个", num_text)[0].replace(',', ''))
                        if zhuanli2 == 0:
                            break
                        df_all[f'{year}'][i] = zhuanli2
                        # num_zhuanli = num_zhuanli + zhuanli2
                        num_zhuanli = num_zhuanli + zhuanli2
                        print(year)
                        time.sleep(random.uniform(1.5, 2))
                else:

                    num_zhuanli = int(zhuanli)
                    time.sleep(random.uniform(1.5, 2))

                df_all['Espacenet专利检索'][i] = num_zhuanli
                print(f"{com_name} : {num_zhuanli}")

                break
        except:
            if num == 0:
                print("重试")
                time.sleep(60)
                continue
            else:
                print("error!{}".format(df_all['英文名称'][i]))