当前位置: 首页 > news >正文

使用正则表达式,取得点击次数,函数抽离

1. 用正则表达式判定邮箱是否输入正确。

import re
r = '^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$'
e = '1234567@qq.com'
if re.match(r,e):
    print('success')
else:
    print('please input ...')

2. 用正则表达式识别出全部电话号码。

import re
text='版权所有:广州商学院   地址:广州市黄埔区九龙大道206号学校办公室:020-82876130   招生电话:020-82872773 粤公网安备 44011602000060号    粤ICP备15103669号'
tele=re.findall('(\d{3,4})-(\d{6,8})',text)
print(tele)

3. 用正则表达式进行英文分词。re.split('',news)

import re
news=''' Then a steep, narrow staircase took us up inside the pagoda and the light changed dramatically. 
  All those windows let in a flood of sunshine and we could see out for miles across the flat land.
'''
new = re.split("[\s,.;'!?]+", news)
print(new)

4. 使用正则表达式取得新闻编号。

import re
a='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'
num=re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_(.*).html',a).group(1).split("/")[-1]
print(num)

5. 生成点击次数的Request URL。

import re
a='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'
num=re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_(.*).html',a).group(1).split("/")[-1]
url='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(num)
print(url)

6. 获取点击次数。

import re
import requests
a='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'
newId = re.search('\_(.*).html',a).group(1).split('/')[1]
clickUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newId)
print(requests.get(clickUrl).text.split('.html')[-1].lstrip("('").rstrip("');"))

7. 将456步骤定义成一个函数 def getClickCount(newsUrl):。

8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl)。

import re
import requests
from bs4 import BeautifulSoup
from datetime import datetime

def getClickCount(newsUrl):
    newId = re.search('\_(.*).html',newsUrl).group(1).split('/')[1]
    clickUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newId)
    print("点击次数:"+requests.get(clickUrl).text.split('.html')[-1].lstrip("('").rstrip("');"))

def getNewDetail(newsUrl):
    resd = requests.get(newsUrl)
    resd.encoding = 'utf-8'
    soupd = BeautifulSoup(resd.text, 'html.parser')
    print(t)
    print(newsUrl)
    info = soupd.select('.show-info')[0].text
    d = re.search('发布时间:(.*) \xa0\xa0 \xa0\xa0作者:', info).group(1)
    dt = datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
    print('发布时间:{}'.format(dt))
    print('作者:' + re.search('作者:(.*)审核:', info).group(1))
    print('审核:' + re.search('审核:(.*)来源:', info).group(1))
    print('来源:' + re.search('来源:(.*)摄影:', info).group(1))
    print('摄影:' + re.search('摄影:(.*)点击', info).group(1))
    print('正文:'+soupd.select('.show-content')[0].text)

url='http://news.gzcc.cn/html/xiaoyuanxinwen/'
res=requests.get(url)
res.encoding="utf-8"
soup=BeautifulSoup(res.text,"html.parser")
for news in soup.select("li"):
    if len(news.select(".news-list-title")) > 0:
        t=news.select('.news-list-title')[0].text
        a = news.select('a')[0].attrs['href']  # 新闻链接
        getClickCount(a)
        getNewDetail(a)
        break

 

转载于:https://www.cnblogs.com/w092/p/8764478.html

相关文章:

  • 记一次和乔布斯合作最难忘的经历
  • 第9章WEB09-Servlet篇
  • servlet匹配路径时/和/*的区别(转)
  • POJ 2251 Dungeon Master
  • 面试总结JavaScript篇
  • Generic detail view PostDetailView must be called with either an object pk or a slug.解决
  • 高端家电“金选奖”名单揭晓,激起新消费主义浪潮
  • Python2与Python3的区别
  • 集群中用Memcached来实现session共享
  • AngularJs的表单验证
  • 如何查看linux中的ssh端口开启状态
  • Go 语言之 struct 结构体
  • 安卓设置背景图平铺,同时设置背景色
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • 为用户管理连接 Confluence 6 到 Jira 应用程序
  • 分享的文章《人生如棋》
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • Android Studio:GIT提交项目到远程仓库
  • Android组件 - 收藏集 - 掘金
  • Java新版本的开发已正式进入轨道,版本号18.3
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • Octave 入门
  • php的插入排序,通过双层for循环
  • Python语法速览与机器学习开发环境搭建
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • Vultr 教程目录
  • 回顾2016
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 区块链共识机制优缺点对比都是什么
  • 深入浏览器事件循环的本质
  • 手机端车牌号码键盘的vue组件
  • 微服务框架lagom
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • - 转 Ext2.0 form使用实例
  • HanLP分词命名实体提取详解
  • 国内开源镜像站点
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #宝哥教你#查看jquery绑定的事件函数
  • (20050108)又读《平凡的世界》
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (十)c52学习之旅-定时器实验
  • (四)鸿鹄云架构一服务注册中心
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .NET Core 和 .NET Framework 中的 MEF2
  • .Net多线程总结
  • .NET构架之我见
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • // an array of int
  • ?php echo ?,?php echo Hello world!;?
  • [ vulhub漏洞复现篇 ] GhostScript 沙箱绕过(任意命令执行)漏洞CVE-2019-6116
  • [2016.7 day.5] T2
  • [AIGC] 开源流程引擎哪个好,如何选型?