使用正则表达式,取得点击次数,函数抽离
学会使用正则表达式
1. 用正则表达式判定邮箱是否输入正确。
import re
def validateEmail(email):
if len(email) > 7:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
return 1
return 0
2. 用正则表达式识别出全部电话号码。
import re
text="123456 1847854 13257872936 135938975 1a2b3c a123456"
m=re.findall(r"1\d{10}",text)
if m:
print (m)
else:
print('not match')
3. 用正则表达式进行英文分词。re.split('',news)
4. 使用正则表达式取得新闻编号
5. 生成点击次数的Request URL
6. 获取点击次数
def getClickTime(newsUrl):
newsId = re.findall('\_(.*).html', newsUrl)[0].split('/')[1]
clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId)
clickStr = requests.get(clickUrl).text
count = re.search("hits'\).html\('(.*)'\);",clickStr).group(1)
return count
7. 将456步骤定义成一个函数 def getClickCount(newsUrl):
8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):

浙公网安备 33010602011771号