# -*- coding: utf-8 -*-
import pymysql
import pandas as pd
from tqdm import tqdm
import xlsxwriter
import openpyxl
from urllib.parse import urlparse




def pipeiName(qiyedatas):
    sql1 = """select id, info_source_code, web_site_name, site_name , site_uri  from  info_source  WHERE web_site_name like '%[name]%' """
    cont=1;
    qynot=[]
    qyin=[]
    for qy in qiyedatas:
        name=qy['name']
        if name is  None:
            uqynot,uqyin=pipeiURL(qy)
            if uqynot:
                qynot.append(uqynot[0])
            if uqyin:
                qyin.append(uqyin[0])
            continue
        if ''==name:
            uqynot,uqyin=pipeiURL(qy)
            if uqynot:
                qynot.append(uqynot[0])
            if uqyin:
                qyin.append(uqyin[0])
            continue
        try:
            sql2=sql1.replace("[name]",name)
            cursor.execute(sql2)
        except Exception as e:
            uqynot,uqyin=pipeiURL(qy)
            if uqynot:
                qynot.append(uqynot[0])
            if uqyin:
                qyin.append(uqyin[0])
            continue
        result_data = cursor.fetchall()
        if(len(result_data)<1):
            uqynot,uqyin=pipeiURL(qy)
            if uqynot:
                qynot.append(uqynot[0])
            if uqyin:
                qyin.append(uqyin[0])
        else:
            cont+=1
            print(cont)
            qyin.append(qy)
            # for row2 in tqdm(result_data):
            #     try:
            #         rd = {'id': row2[0],
            #               '编码': row2[1],
            #               '网站名称': row2[2],
            #               '栏目名称': row2[3],
            #               '栏目地址': row2[4],
            #               '企业名称': qy['name']
            #               }
            #         qyin.append(rd)
            #     except Exception as e:
            #         print(e)
            #         print("查询失败！！"+sql2)
    print(qyin)
    df_in = pd.DataFrame(data=qyin)
    df_in.to_excel('n企业情况在平台中有数据2.xlsx', engine='xlsxwriter', index=False)
    print(qynot)
    df_out = pd.DataFrame(data=qynot)
    df_out.to_excel('n企业情况在平台中没有数据2.xlsx', engine='xlsxwriter', index=False)

def pipeiURL(qy):
    uqynot=[]
    uqyin=[]
    url=qy['url']
    sql1 = """select id, info_source_code, web_site_name, site_name , site_uri  from  info_source  WHERE site_uri like '%[url]%' """
    if url is  None:
        uqynot.append(qy)
        return uqynot,uqyin
    try:
        parsed_url = urlparse(url)
        domain = parsed_url.netloc
        if ''==domain:
            uqynot.append(qy)
            return uqynot,uqyin
    except Exception as e:
        uqynot.append(qy)
        return uqynot,uqyin
    sql2=sql1.replace("[url]",domain)
    cursor.execute(sql2)
    result_data = cursor.fetchall()
    # if(len(result_data)>1):
    if(len(result_data)<1):
        uqynot.append(qy)
    else:
        uqyin.append(qy)
# for row2 in tqdm(result_data):
        #     try:
        #         rd = {'id': row2[0],
        #               '编码': row2[1],
        #               '网站名称': row2[2],
        #               '栏目名称': row2[3],
        #               '栏目地址': row2[4],
        #               '企业名称': qy['name']
        #               }
        #         uqyin.append(rd)
        #     except Exception as e:
        #         print(e)
        #         print("查询失败！！"+sql2)
    return uqynot,uqyin


if __name__ == '__main__':
    # 打开Excel文件
    workbook = openpyxl.load_workbook('name.xlsx')

    # 获取工作表对象
    worksheet = workbook.active

    qiyedatas=[]
    # 遍历工作表的行
    for row in worksheet.iter_rows(values_only=True):
        qiyemsg={
            'yname':row[0],
            'name':row[1],
            'url':row[2]
        }
        qiyedatas.append(qiyemsg)
        # 打印每行的数据
        # print(row)
    conn = pymysql.Connect(host='114.116.44.11', port=3306, user='root', passwd='f7s0&7qqtK', db='clb_project',
                           charset='utf8')
    cursor = conn.cursor()
    pipeiName(qiyedatas)







