码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-11 18:14:20      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:ret   ref   show   正则表达式   nes   表达   时间   商学院   click   

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

r=‘^(\w)+([\.\_\-]w+)*@(\w)+((\.\w{2,3}){1,3})‘
e=‘157049540@qq.com‘
if re.match(r,e):
    print(‘suc‘)
else:
    print(‘false‘)

 

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

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

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

new=‘‘‘在庄严的国歌声中,决赛正式拉开序幕。本次决赛分为必答和抢答两个环节,
所有选手全身心投入比赛。必答环节,选手们准备充分,胸有成竹,各代表队分数不相上下。
抢答环节,选手们全神贯注,争分夺秒,斗志满满,现场气氛既紧张又活跃,观众不时为选手的出色表现欢呼鼓掌。
经过激烈的角逐,根据两个环节的最后得分,第一队的林铄姿、徐映珠、陈诗媛与第八队的程媚、雷小云、陈海燕获得一等奖;
第二队的吴绮婷、曾楷芬、梁晓棋,第三队的林靖、黄琪琳、许悦,第六队的林锦涛、马丽群、赵志红,第五队的杨少璟、黄金龙、
郑文婷获得二等奖;第七队的黄楚婷、李国祥、符琼文,第四队的陈玉萍、黄芷萱、张小梅获得三等奖。王相东、余九林、陈流芳为获奖团队颁奖。‘‘‘

e=re.split("[\s..?\‘\,\。\、]+",new)
print(e)

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

import re
newsUrl = ‘http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0401/9167.html‘
a1 = re.search(‘\_(.*).html‘,newsUrl).group(1)
print(a1)

 5. 生成点击次数的Request URL

import requests
res=requests.get(‘http://oa.gzcc.cn/api.php?op=count&id=9167&modelid=80‘)
res.encoding = ‘utf-8‘

 6. 获取点击次数

a=res.text.split(".html")[-1].lstrip("(‘)").rstrip("‘);")
print(a)

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

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

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

def getNewDetail(newsUrl):
    ress=requests.get(newsUrl)
    ress.encoding = ‘utf-8‘
    soups = BeautifulSoup(ress.text, ‘html.parser‘)
    title = soups.select(‘.show-title‘)[0].text  # 标题
    info = soups.select(‘.show-info‘)[0].text             #连接
    dt = datetime.strptime(info.lstrip(‘发布时间:‘)[:19], ‘%Y-%m-%d %H:%M:%S‘) #发布时间
    if info.find(‘来源:‘)>0:
        source=info[info.find(‘来源:‘):].split()[0].lstrip(‘来源:‘)
    else:
        source=‘none‘
    # content=soup.select(".show-content")[0].text.strip()
    click=getClickCount(newsUrl)
    print(dt, title, newsUrl, source, click)
 
res=requests.get(‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘)
res.encoding = ‘utf-8‘
soup = BeautifulSoup(res.text, ‘html.parser‘)
for news in soup.select(‘li‘):
    if len(news.select(‘.news-list-title‘)) > 0:
        ness=news.select(‘a‘)[0].attrs[‘href‘]#继续
        getNewDetail(ness)

 

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

标签:ret   ref   show   正则表达式   nes   表达   时间   商学院   click   

原文地址:https://www.cnblogs.com/qq157049540/p/8797063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!