ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-02-10 20:00:14  阅读:156  来源: 互联网

标签:24 title Python print selenium re href date options


代码练习:

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)


运行结果:

 

标签:24,title,Python,print,selenium,re,href,date,options
来源: https://blog.csdn.net/Ama_tor/article/details/122868053

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有