import requests,re,time,random,pymysql,json
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver

cnx = pymysql.connect(host='114.115.159.144',user='caiji', password='zzsn9988', db='dbScore', charset='utf8mb4')

df_all = pd.read_excel('500强股票代码-信用代码.xlsx')


for num in range(len(df_all)):
    com_code = df_all['股票代码'][num]
    social_code = df_all['信用代码'][num]
    print(num,social_code)
    if com_code != '--' and social_code != '--':
        pass
    else:
        continue
    print(num)

    page = 1

    t = int(time.time()*1000)
    url = f'https://np-anotice-stock.eastmoney.com/api/security/ann?sr=-1&page_size=50&page_index={page}&ann_type=U%2CU_Pink&client_source=web&stock_list={com_code}'
    res_json = requests.get(url).json()
    #res_json = json.loads(re.findall('\((.*?)\)',res_text)[0])
    list_all = res_json['data']['list']

    for one_info in list_all:

        news_title = one_info['title']

        news_date = one_info['notice_date'][:10]
        art_code = one_info['art_code']
        news_url = f'https://data.eastmoney.com/notices/detail/{com_code}/{art_code}.html'

        t = int(time.time()*1000)
        url_news = f'https://np-cnotice-stock.eastmoney.com/api/content/ann?art_code={art_code}&client_source=web&page_index=1&_={t}'  
        json_news = requests.get(url_news).json()
        #json_news = json.loads(re.findall('\((.*?)\)',res_news)[0])

        news_html = json_news['data']['attach_url']

        list_info = [
            social_code,
            news_title,
            news_date,
            news_url,
            news_html,
            '东方财富网',
            '1'
        ]

        # list_all_info.append(tuple(list_info))
        with cnx.cursor() as cursor:
            sel_sql = '''select social_credit_code from brpa_source_article where source_address = %s ''' 

            cursor.execute(sel_sql,news_url)
            selects = cursor.fetchall()
            if selects:
                break

            insert_sql = '''insert into brpa_source_article(social_credit_code,title,publish_date,source_address,pdf_address,origin,type) values(%s,%s,%s,%s,%s,%s,%s)'''      
            cursor.execute(insert_sql,tuple(list_info))
            cnx.commit()
            print(news_title)

