python金融數(shù)據(jù)挖掘與分析(六)——解析上市公司PDF公告

@[toc]
這篇博文主要介紹通過數(shù)據(jù)挖掘技術(shù)批量下載菊草資訊網(wǎng)的上市公司理財公告PDF文件,并通過PDF文本解析技術(shù)分析獲取到的理財公告PDF文件,從中識別潛在的機(jī)構(gòu)投資者。

1. PDF文件批量下載

金融網(wǎng)站數(shù)據(jù)挖掘?qū)崙?zhàn)這篇博文中,我們介紹了在巨潮資訊網(wǎng)爬取公告信息的方法,該方法可以作文本文工作的基礎(chǔ),并在爬取多頁內(nèi)容、自動篩選所需內(nèi)容,爬取某段時間的數(shù)據(jù),理財公告PDF文件自動下載等功能方面進(jìn)行擴(kuò)展。

1.1 爬取多頁內(nèi)容

之前介紹過,我們可以通過修改網(wǎng)頁參數(shù)的方式爬取多頁內(nèi)容,但是這在巨潮資訊網(wǎng)不適用,因為進(jìn)行翻頁操作時,網(wǎng)址并沒有發(fā)生變化。此時可以利用Selenium庫模擬鼠標(biāo)單擊下圖所示的“下一頁”按鈕,并根據(jù)公告數(shù)量來確定模擬單擊的次數(shù),每單擊一次就獲取一下改業(yè)的源代碼,最后把獲取到的各頁的源代碼存儲到一個列表里。
模擬翻頁的代碼如下:

    browser = webdriver.Chrome()
    url = 'http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord=理財'
    browser.get(url)
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id="fulltext-search"]/div/div[1]/div[2]/div[4]/div[2]/div/button[2]').click()

上面的代碼可以實現(xiàn)自動翻頁功能,下面需要實現(xiàn)控制翻頁的次數(shù),也就是獲取總頁數(shù),每頁顯示10條公告,所以公告總數(shù)除以10可得到總
頁數(shù)。

    data = browser.page_source
    page_pattern = '<span class="total-box" style="">約\s*(.*?)\s*條 當(dāng)前顯示.*?條</span>'
    count = re.findall(page_pattern, data)[0]
    pages = int(int(count)/10)

findall()函數(shù)返回的是一個列表,因此要想獲取列表中的元素,需要使用索引。

將上面兩項功能結(jié)合起來,實現(xiàn)每次翻頁時都獲取當(dāng)前頁面的源碼,并把每一頁的源碼存儲到data_list列表中,代碼如下:

    # 每次翻頁時都獲取當(dāng)前頁面的源碼,并把每一頁的源碼存儲到data_list列表中
    data_list = []
    data_list.append(data)
    # for i in range(pages):
    for i in range(3):
        browser.find_element_by_xpath('//*[@id="fulltext-search"]/div/div[1]/div[2]/div[4]/div[2]/div/button[2]').click()
        time.sleep(2)
        data = browser.page_source
        data_list.append(data)
        time.sleep(1)

    # 將所有源碼轉(zhuǎn)換成一個字符串,方便進(jìn)行正則化處理
    alldata = ''.join(data_list)
    browser.quit()

    # 提取標(biāo)題、鏈接及日期數(shù)據(jù)
    '<td rowspan="1" colspan="1" class="el-table_1_column_2  ">'
    title_pattern = '<span title="" class="r-title">(.*?)</span>'
    href_pattern = '<td rowspan="1" colspan="1" class="el-table_1_column_2  ">.*?href="(.*?)" data-id='
    date_pattern = '<td rowspan="1" colspan="1" class="el-table_1_column_3.*?<span class="time">\s*(.*?)\s*</span>'
    title = re.findall(title_pattern, alldata)
    href = re.findall(href_pattern, alldata)
    date = re.findall(date_pattern, alldata)

    # 數(shù)據(jù)清洗
    for i in range(len(title)):
        title[i] = re.sub('<.*?>', '', title[i])
        title[i] = title[i].strip()
        date[i] = date[i].strip()
        href[i] = 'http://www.cninfo.com.cn' + href[i]
        href[i] = re.sub('amp;', '', href[i])
        print(str(i + 1) + '.' + title[i] + ' - ' + date[i])
        print(href[i])

執(zhí)行結(jié)果如下:

1.焦點科技:國信證券股份有限公司關(guān)于公司使用部分超額募集資金購買理財產(chǎn)品的保薦意見 - 2020-02-08
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900009170&announcementId=1207297926&announcementTime=2020-02-08
2.焦點科技:關(guān)于授權(quán)使用部分超募資金購買理財產(chǎn)品的公告 - 2020-02-08
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900009170&announcementId=1207297929&announcementTime=2020-02-08
3.東尼電子:關(guān)于使用暫時閑置募集資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-08
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900032656&announcementId=1207297962&announcementTime=2020-02-08
4.創(chuàng)源文化:關(guān)于全資子公司使用閑置募集資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-07 17:59
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900031820&announcementId=1207298260&announcementTime=2020-02-07 17:59
5.仙樂健康:關(guān)于收回到期理財產(chǎn)品本金及收益的公告 - 2020-02-07 15:44
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900032360&announcementId=1207297837&announcementTime=2020-02-07 15:44
6.信誠理財28日盈:信誠理財28日盈債券型證券投資基金更新招募說明書摘要 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=jjjl0000099&announcementId=1207297739&announcementTime=2020-02-07
7.信誠理財28日盈:信誠理財28日盈債券型證券投資基金基金合同 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=jjjl0000099&announcementId=1207297776&announcementTime=2020-02-07
8.晨豐科技:關(guān)于使用部分閑置募集資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900034224&announcementId=1207296602&announcementTime=2020-02-07
9.索通發(fā)展:關(guān)于使用暫時閑置募集資金進(jìn)行委托理財?shù)倪M(jìn)展公告 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900023754&announcementId=1207296192&announcementTime=2020-02-07
10.通達(dá)電氣:關(guān)于部分理財產(chǎn)品到期贖回及繼續(xù)使用部分閑置募集資金進(jìn)行現(xiàn)金管理的進(jìn)展公告 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gfbj0870399&announcementId=1207296227&announcementTime=2020-02-07
11.海信家電:H股公告-認(rèn)購理財產(chǎn)品 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssz0000921&announcementId=1207296321&announcementTime=2020-02-07
12.信誠理財28日盈:信誠理財28日盈債券型證券投資基金托管協(xié)議 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=jjjl0000099&announcementId=1207297770&announcementTime=2020-02-07
13.信誠理財28日盈:信誠理財28日盈債券型證券投資基金更新招募說明書 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=jjjl0000099&announcementId=1207297780&announcementTime=2020-02-07
14.ST羅頓:委托理財進(jìn)展公告 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssh0600209&announcementId=1207296091&announcementTime=2020-02-07
15.麗江旅游:收回理財產(chǎn)品本金及收益的公告 - 2020-02-07
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssz0002033&announcementId=1207296416&announcementTime=2020-02-07
16.海信家電:須予披露交易-認(rèn)購理財產(chǎn)品 - 2020-02-06 23:59
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssz0000921&announcementId=1207297283&announcementTime=2020-02-06 23:59
17.唐源電氣:關(guān)于使用部分閑置募集資金購買理財產(chǎn)品的進(jìn)展公告 - 2020-02-06 16:28
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900036234&announcementId=1207296204&announcementTime=2020-02-06 16:28
18.博云新材:關(guān)于使用閑置募集資金購買理財產(chǎn)品到期收回的公告 - 2020-02-06 11:38
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900008207&announcementId=1207295925&announcementTime=2020-02-06 11:38
19.桃李面包:關(guān)于使用閑置自有資金購買理財產(chǎn)品的進(jìn)展公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900024533&announcementId=1207294566&announcementTime=2020-02-06
20.山東威達(dá):關(guān)于使用閑置自有資金進(jìn)行委托理財?shù)倪M(jìn)展公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssz0002026&announcementId=1207294638&announcementTime=2020-02-06
21.衛(wèi)信康:關(guān)于使用暫時閑置募集資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900034348&announcementId=1207294749&announcementTime=2020-02-06
22.光大添天盈:關(guān)于光大保德信添天盈月度理財債券型證券投資基金實施轉(zhuǎn)型的提示性公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=jjjl0000081&announcementId=1207295962&announcementTime=2020-02-06
23.光大添天盈:光大保德信添天盈月度理財債券型證券投資基金基金份額持有人大會表決結(jié)果暨決議生效公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=jjjl0000081&announcementId=1207296019&announcementTime=2020-02-06
24.烽火通信:關(guān)于使用部分可轉(zhuǎn)換公司債券閑置募集資金進(jìn)行委托理財?shù)膶嵤┕?- 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssh0600498&announcementId=1207294598&announcementTime=2020-02-06
25.美盈森:關(guān)于使用部分暫時閑置資金購買理財產(chǎn)品的進(jìn)展公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900008616&announcementId=1207295008&announcementTime=2020-02-06
26.振華科技:關(guān)于購買銀行保本理財產(chǎn)品(第十九次)到期的公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssz0000733&announcementId=1207294992&announcementTime=2020-02-06
27.平潭發(fā)展:關(guān)于使用閑置自有資金購買理財產(chǎn)品的進(jìn)展公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssz0000592&announcementId=1207294505&announcementTime=2020-02-06
28.北京科銳:關(guān)于使用閑置自有資金進(jìn)行投資理財?shù)倪M(jìn)展公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900010388&announcementId=1207294553&announcementTime=2020-02-06
29.大元泵業(yè):關(guān)于使用閑置募集資金購買理財產(chǎn)品到期回收的公告 - 2020-02-06
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900031903&announcementId=1207294569&announcementTime=2020-02-06
30.天喻信息:關(guān)于使用自有資金購買保本型理財產(chǎn)品的進(jìn)展公告 - 2020-02-05 16:40
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900017107&announcementId=1207294663&announcementTime=2020-02-05 16:40
31.海能實業(yè):關(guān)于使用閑置自有資金進(jìn)行委托理財?shù)倪M(jìn)展公告 - 2020-02-05 16:03
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900030659&announcementId=1207294560&announcementTime=2020-02-05 16:03
32.移為通信:關(guān)于理財產(chǎn)品到期贖回及繼續(xù)使用閑置募集資金和自有資金進(jìn)行現(xiàn)金管理的公告 - 2020-02-05 15:47
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900030831&announcementId=1207294540&announcementTime=2020-02-05 15:47
33.雅化集團(tuán):關(guān)于使用部分閑置募集資金購買銀行理財產(chǎn)品的進(jìn)展公告 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900015673&announcementId=1207292358&announcementTime=2020-02-05
34.祁連山:關(guān)于使用閑置自有資金購買保本型銀行理財產(chǎn)品到期贖回的公告 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssh0600720&announcementId=1207292396&announcementTime=2020-02-05
35.ST獅頭:關(guān)于使用部分閑置自有資金購買理財產(chǎn)品的實施進(jìn)展公告 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=gssh0600539&announcementId=1207292400&announcementTime=2020-02-05
36.桃李面包:關(guān)于使用閑置自有資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900024533&announcementId=1207292453&announcementTime=2020-02-05
37.綠康生化:關(guān)于使用部分閑置募集資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900030698&announcementId=1207292508&announcementTime=2020-02-05
38.晨豐科技:關(guān)于使用部分閑置募集資金購買理財產(chǎn)品到期贖回的公告 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900034224&announcementId=1207292568&announcementTime=2020-02-05
39.索通發(fā)展:關(guān)于使用暫時閑置募集資金進(jìn)行委托理財?shù)墓?- 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900023754&announcementId=1207292570&announcementTime=2020-02-05
40.索通發(fā)展:獨立董事關(guān)于使用暫時閑置募集資金購買保本型理財產(chǎn)品的獨立意見 - 2020-02-05
http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900023754&announcementId=1207292573&announcementTime=2020-02-05

1.2 篩選所需內(nèi)容

這里只進(jìn)行簡單的數(shù)據(jù)清洗,選出一定日期范圍內(nèi)的數(shù)據(jù)。

    for i in range(len(title)):
        if '2020' not in date[i]:
            title[i] = ''
            href[i] = ''
            date[i] = ''
            
    while '' in title:
        title.remove('')
    while '' in href:
        href.remove('')
    while '' in date:
        date.remove('')

1.3 理財公告PDF文件的自動批量下載

打開剛剛獲取到的公告網(wǎng)址,可以看到需要下載的PDF文件,如果需要實現(xiàn)自動現(xiàn)在,需要使用Selenium庫模擬點擊頁面中的“下載”按鈕。

    browser = webdriver.Chrome()
    url = 'http://www.cninfo.com.cn/new/disclosure/detail?orgId=9900009170&announcementId=1207297926&announcementTime=2020-02-08'
    browser.get(url)

    browser.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[1]/a[3]').click()
    time.sleep(3)

通過以上代碼可以實現(xiàn)公告的自動下載,這里要實現(xiàn)批量下載,只需要循環(huán)訪問我們存入href中的網(wǎng)址即可。

    for i in range(len(href)):
        browser = webdriver.Chrome()
        browser.get(href[i])
        try:
            browser.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[1]/a[3]').click()
            time.sleep(3)   # 下載需要時間,這里的sleep必不可少
            print(str(i+1) + '.' + title[i] + '下載完畢')
            browser.quit()
        except:
            print(title[i] + '不是PDF')

2. PDF文本解析

我們從網(wǎng)上得到的公告信息多為PDF文件,如果要更深層次的分析公告內(nèi)容,需要對PDF文件進(jìn)行解析,這里采用pdfplumber庫(pip安裝),其不僅能解析文字,還能方便地解析表格。

2.1 用pdfplumber庫提取文本內(nèi)容

通過pdfplumber庫的exact_text()函數(shù)解析PDF文件的文本內(nèi)容。

    path = os.path.join('bulletin', '東尼電子:關(guān)于使用暫時閑置募集資金購買理財產(chǎn)品到期贖回的公告.PDF')   # pdf文件路徑
    pdf = pdfplumber.open(path)
    pages = pdf.pages   # 通過pages屬性獲取所有頁的信息,此時pages是一個列表
    text_all = []
    for page in pages:
        text = page.extract_text()
        text_all.append(text)

    text_all = ''.join(text_all)    # 把列表轉(zhuǎn)換成字符串
    print(text_all)
    pdf.close()

我們可以通過extract_tables()函數(shù)獲取表格信息。

    path = os.path.join('bulletin', '東尼電子:關(guān)于使用暫時閑置募集資金購買理財產(chǎn)品到期贖回的公告.PDF')   # pdf文件路徑
    pdf = pdfplumber.open(path)
    pages = pdf.pages   # 通過pages屬性獲取所有頁的信息,此時pages是一個列表
    page = pages[3]     # 表格在第4頁
    tables = page.extract_tables()      # 提取表格
    table = tables[0]

獲取得到的table是一個嵌套列表結(jié)構(gòu),大列表里包含多個小列表,每個小列表的內(nèi)容即表格中每一行的內(nèi)容,我們可以通過pandas庫使顯示效果更佳美觀。

import pandas as pd
df = pd.DataFrame(table[1:], columns=table[0])

第一行內(nèi)容為表頭信息。table[1:]為表格第2行及其以下的內(nèi)容。

3. PDF文本解析實戰(zhàn)——尋找合適的理財公告

3.1 遍歷文件夾里所有PDF文件

    # 遍歷文件夾中的素有PDF文件
    file_dir = r'bulletin'
    for files in os.walk(file_dir):
        print(files[2])     # files[0]表示母文件夾信息,files[1]表示各個子文件夾信息,files[2]表示母文件夾和子文件夾里的各個文件信息。

獲取文件名后,判斷文件后綴名是否為“.PDF”,這里使用os.path.splitext()函數(shù)將文件名與擴(kuò)展名分離。

    file_list = []
    for files in os.walk(file_dir):
        for file in files[2]:     # files[0]表示母文件夾信息,files[1]表示各個子文件夾信息,files[2]表示母文件夾和子文件夾里的各個文件信息。
            if os.path.splitext(file)[1] == '.pdf' or os.path.splitext(file)[1] == '.PDF':
                file_list.append(file_dir + '\\' + file)

3.2 批量解析每一個PDF文件

    # 遍歷文件夾中的素有PDF文件
    file_dir = r'bulletin'
    file_list = []
    for files in os.walk(file_dir):
        for file in files[2]:     # files[0]表示母文件夾信息,files[1]表示各個子文件夾信息,files[2]表示母文件夾和子文件夾里的各個文件信息。
            if os.path.splitext(file)[1] == '.pdf' or os.path.splitext(file)[1] == '.PDF':
                file_list.append(file_dir + '\\' + file)

    print(file_list)


    for i in range(len(file_list)):
        pdf = pdfplumber.open(file_list[i])
        pages = pdf.pages   # 通過pages屬性獲取所有頁的信息,此時pages是一個列表
        text_all = []
        for page in pages:
            text = page.extract_text()
            text_all.append(text)

        text_all = ''.join(text_all)    # 把列表轉(zhuǎn)換成字符串
        print(text_all)
        pdf.close()

3.3 將合格的PDF文件自動歸檔

遍歷并解析完P(guān)DF文件后,便能盡心高一些深度分析了。這里以關(guān)鍵詞篩選為例進(jìn)行演示:只有當(dāng)PDF文件正文里含有“自有”,“議案”,“理財”,“現(xiàn)金管理”這些關(guān)鍵詞,才把這個PDF文件篩選出來并存儲到一個列表里。

    # 遍歷文件夾中的素有PDF文件
    file_dir = r'bulletin'
    file_list = []
    for files in os.walk(file_dir):
        for file in files[2]:     # files[0]表示母文件夾信息,files[1]表示各個子文件夾信息,files[2]表示母文件夾和子文件夾里的各個文件信息。
            if os.path.splitext(file)[1] == '.pdf' or os.path.splitext(file)[1] == '.PDF':
                file_list.append(file_dir + '\\' + file)

    print(file_list)

    pdfs = []
    for i in range(len(file_list)):
        pdf = pdfplumber.open(file_list[i])
        pages = pdf.pages   # 通過pages屬性獲取所有頁的信息,此時pages是一個列表
        text_all = []
        for page in pages:
            text = page.extract_text()
            text_all.append(text)

        text_all = ''.join(text_all)    # 把列表轉(zhuǎn)換成字符串

        if ('自有' in text_all) or ('議案' in text_all) or ('理財' in text_all) or ('現(xiàn)金管理' in text_all):
            pdfs.append(file_list[i])
    print(pdfs)

    # 將篩選后的pdf文件存放到新的文件夾中
    for pdf_i in pdfs:
        newpath = 'new_bulletin\\' + pdf_i.split('\\')[-1]
        os.rename(pdf_i, newpath)   # 執(zhí)行文件移動操作

這里只是提供了解析pdf文本的方法,具體如何解析,以及解析的策略還需要大家自己來提供。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 常聽人說:“人活著真可怕,不知道幸運和災(zāi)難哪一個先到?!闭f實話誰也料想不到明天會怎么樣。樂觀向上的高萍,絕對沒有料...
    hua楓葉閱讀 797評論 4 18
  • 與星邀明月,曹衣凈落塵, 川江共天色,孤影浪排云, 心縈不知處,昔人今何復(fù)? 誰憐夜色里,舊曲戀新琴。
    雲(yún)灬閱讀 277評論 0 1
  • CentOS 7 默認(rèn)安裝了 Python 2,當(dāng)需要使用 Python 3 的時候,可以手動下載 Python ...
    素年錦時2021閱讀 586評論 0 0
  • 人定勝天,事在人為。 想要得的成就,就必須用實際行動來落實,永遠(yuǎn)也別祈求外界有什么意外,或者說想要天上掉餡餅,那只...
    景稚閱讀 302評論 1 2

友情鏈接更多精彩內(nèi)容