Python学习笔记(24)——Greatchao资讯网理财公告信息的selenium挖掘

2022/2/10 20:19:43

本文主要是介绍Python学习笔记(24)——Greatchao资讯网理财公告信息的selenium挖掘,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

代码练习:

from selenium import webdriver#引进selinium库
import re#引入re库
#定义函数
def greatchao(keyword):
    #selenium库模拟浏览器
    url='http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord='+keyword
    chrome_options=webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    browser=webdriver.Chrome(options=chrome_options)
    browser.get(url)
    data=browser.page_source
    browser.quit()  # 关闭模拟浏览器
    #正则提取内容
    p_title = '<span title="" class="r-title">(.*?)</span>'
    p_href = '<a target="_blank" href="(.*?)" data-id=".*?" data-orgid=.*?" data-seccode=".*?" class="ahover">'
    p_date = '<td rowspan="1" colspan="1" class="el-table_1_column_3 is-left "><div class="cell"><span class="time">(.*?)</span>'
    title = re.findall(p_title,data,re.S)
    href = re.findall(p_href,data,re.S)
    date = re.findall(p_date,data,re.S)
    # print(title)#通过观察需要清除<>内容
    # print(href)#通过观察,网址需要加前缀http: // www.cninfo.com.cn /,去掉文中amp;
    # print(date)#通过观察,日期需要清除换行符,空格以及部分带时间的格式
    #遍历标题清洗数据
    for i in range(len(title)):
        title[i]=re.sub('<.*?>', '', title[i])
        href[i]='http://www.cninfo.com.cn' + href[i]
        href[i]=re.sub('amp;', '', href[i])
        date[i]=date[i].strip()
        date[i]=date[i].split(' ')[0]
        print(str(i+1)+'.'+title[i]+'-'+date[i])
        print(href[i])
#调用自定义函数
keywords=['理财','现金管理','纾困']
for i in keywords:
    greatchao(i)


运行结果:

 



这篇关于Python学习笔记(24)——Greatchao资讯网理财公告信息的selenium挖掘的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程