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

本文介绍了如何利用正则表达式实现多种实用功能,包括验证电子邮件格式、提取电话号码、进行英文文本分词等,并提供了具体的Python代码示例。

学会使用正则表达式

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

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

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

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

5. 生成点击次数的Request URL

6. 获取点击次数

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

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

 

import requests
import re

newsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_0925/8249.html'

def getClickCount(newsUrl):
    newsId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1]
    res = requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId))
    return (int(res.text.split('.html')[-1].lstrip("(')").rstrip("');")))


click = getClickCount(newsUrl)
print(click)

r = '^(\w)+(\.\w+)*@(\w)+(\w)+((\.\w{2,3}){1,3})$'
e = '3947653@qq.com'
if re.match(r, e):
    print(re.match(r, e).group(0))
else:
    print('error')

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

news = '''Mr. Johnson had never been up in an aerophane before and he had read a lot about air accidents, so one day when a friend offered to take him for a ride in his own small phane, Mr. Johnson was very worried about accepting. Finally, however, his friend persuaded him that it was very safe, and Mr. Johnson boarded the plane.'''
word = re.split('[\s,.?\-]+', news)
print(word)

re.search('\_(.*).html',newsUrl).group(1)
re.findall('',newsUrl)
print(re.search('\_(.*).html',newsUrl).group(1))

 

转载于:https://www.cnblogs.com/BOXczx/p/8780959.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值