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


学会使用正则表达式

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))

 

posted on 2018-04-10 17:47  223陈子翔  阅读(159)  评论(0编辑  收藏  举报