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

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

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

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

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

5. 生成点击次数的Request URL

6. 获取点击次数

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

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



# import requests # res = requests.get('http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80') # res.encoding = 'utf-8' # a = int(res.text.split('.html')[-1].lstrip("(')").rstrip("');")) # print(a) # import re # newsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_0925/8249.html' # b = re.match('http://news.gzcc.cn/html/2017/xiaoyuanxinwen_(.*).html',newsUrl).group(1).split('/')[-1] # b=re.search('\_(.*).html',newsUrl).group(1) # c=re.findall('\_(.*).html',newsUrl)[0] # print(b) 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 = '''She takes care of me all the time and sometimes I think she is a little overprotective.'''
word = re.split('[\s,.?\-]+',news)
print(word)

  

posted @ 2018-04-10 16:03  HISAM  阅读(193)  评论(0编辑  收藏  举报