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

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

import re;
def isEmailAccount(str):
    if(re.match("\w*@\w*(\.\w{2,3}){1,3}$",str)):
        return True
    else:
        return False
print(isEmailAccount('123sdf_sd@qq.com.cm'))
print(isEmailAccount('123sdf_sd@qq'))

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

import re;
import requests
newurl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'
res = requests.get(newurl)
res.encoding='utf-8'
tel=re.findall("(\d{3,4}) *- *(\d{7,8})",res.text)

3. 用正则表达式进行英文分词。

import re;
str="""In this year's government work report, Premier Li Keqiang frequently referred to internet-related concepts, like Internet Plus, artificial intelligence, and Digital China, and noted that the Chinese government will give priority to "providing more and better online content"."""
a=re.split('[\s,.?\" ]+',str)
print(a)

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

5. 生成点击次数的Request URL

6. 获取点击次数

import re
import requests
url="http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html"
newId=re.search("/(\d*).html$",url).group(1)
print(newId)
clickUrl ="http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newId)
print(clickUrl)
resc = requests.get(clickUrl)
num = re.search(".html\('(\d*)'\)",resc.text).group(1)
print(num)

 

posted @ 2018-04-09 19:12  110林博  阅读(79)  评论(0编辑  收藏  举报