import re

import requests
from bs4 import BeautifulSoup

from base import BaseCore
from retry import retry

baseCore = BaseCore.BaseCore()

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'
}


def getContentA(url):
    pass


def getContentB(url):
    req = requests.get(url,headers=headers)
    req.encoding = req.apparent_encoding
    soup = BeautifulSoup(req.text,'html.parser')
    contentWithTag = soup.find('div',attrs={'id':'desContent'})
    a_list = contentWithTag.find_all('a')
    for a in a_list:
        href = a.get('href')
        file_name = a.text.strip()

    content = contentWithTag.text.strip()


def doJob():
    urls = ['http://reits.szse.cn/lawrule/laws/index.html',
            'http://reits.szse.cn/lawrule/regulations/csrcorder/index.html',
            'http://reits.szse.cn/lawrule/regulations/csrcannoun/index.html']
    for url in urls:
        req = requests.get(url, headers=headers)
        req.encoding = req.apparent_encoding
        soup = BeautifulSoup(req.text, 'lxml')
        li_list = soup.find('ul', class_='newslist').find_all('li')
        for li in li_list:
            info = str(li.find('script'))
            href = re.findall('curHref = \'(.*?)\';', info)[0].replace('./', 'http://reits.szse.cn/lawrule/laws/')
            title = re.findall('curTitle =\'(.*?)\';', info)[0]
            publishDate = li.find('span', class_='time').text.strip()
            if '.html' in href:
                getContentA(href)
            else:
                getContentB(href)


if __name__ == '__main__':
    doJob()
