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)


运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值