博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用正则表达式,取得点击次数,函数抽离
阅读量:4687 次
发布时间:2019-06-09

本文共 2945 字,大约阅读时间需要 9 分钟。

1. 用正则表达式判定邮箱是否输入正确。e='454181644@qq.com'r='^(\w)+([\.\_\-]\w+)*@(\w)+((\.\w{2,3}){1,3})$'print(re.match(r,e))2. 用正则表达式识别出全部电话号码。tel='版权所有:广州商学院   地址:广州市黄埔区九龙大道206号  学校办公室:020-82876130  招生电话:020-82872773'a=re.search('(\d{3,4})-(\d{6,8})',tel).group(2)print(a)3. 用正则表达式进行英文分词。re.split('',news)import renews='''An empty street,An empty house,A hole inside my heart,I'm all alone,The rooms are getting smaller,I wonder how,I wonder why,I wonder where they are,The days we had,The songs we sang together,Oh yeah,And oh my love,I'm holding on forever,Reaching for a love that seems so far,So I say a little prayer,And hope my dreams will take me there,Where the skies are blue to see you once again, my love'''new = re.split("[\s+\n\.\,\']", news)print(new)4. 使用正则表达式取得新闻编号import reurl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'a=re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/(.*).html',url).group(1)print(a) 5. 生成点击次数的Request URLimport reurl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'a=re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/(.*).html',url).group(1)srac='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(a)print(srac)6. 获取点击次数import requestsfrom bs4 import BeautifulSoupurl='http://oa.gzcc.cn/api.php?op=count&id=9183&modelid=80'srac=requests.get(url)print(srac.text.split('.html')[-1].lstrip("('").rstrip("');"))7. 将456步骤定义成一个函数 def getClickCount(newsUrl)8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):import requestsimport refrom bs4 import BeautifulSoupfrom datetime import datetimeurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'res=requests.get(url)res.encoding="utf-8"soup=BeautifulSoup(res.text,"html.parser")#def getClickCount(newsUrl)def getClickCount(newUrl):    newId=re.search('\_(.*).html',newUrl).group(1).split('/')[1]    clickUrl='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newId)    return (int(requests.get(clickUrl).text.split('.html')[-1].lstrip("('").rstrip("');")))#def getNewDetail(newsUrl):def getNewDetail(newsUrl):    resd = requests.get(newsUrl)    resd.encoding = 'utf-8'    soupd = BeautifulSoup(resd.text, 'html.parser')    print(t)    print(newsUrl)    info = soupd.select('.show-info')[0].text    d = re.search('发布时间:(.*) \xa0\xa0 \xa0\xa0作者:', info).group(1)    dt = datetime.strptime(d, '%Y-%m-%d %H:%M:%S')    print('发布时间:{}'.format(dt))    print('作者:' + re.search('作者:(.*)审核:', info).group(1))    print('审核:' + re.search('审核:(.*)来源:', info).group(1))    print('来源:' + re.search('来源:(.*)摄影:', info).group(1))    print('摄影:' + re.search('摄影:(.*)点击', info).group(1))    print(getClickCount(a))    print('正文:'+soupd.select('.show-content')[0].text)for news in soup.select("li"):    if len(news.select(".news-list-title")) > 0:        t=news.select('.news-list-title')[0].text        a = news.select('a')[0].attrs['href']  # 新闻链接        getNewDetail(a)        break

 

转载于:https://www.cnblogs.com/liangyao111/p/8799384.html

你可能感兴趣的文章
php扩展目录
查看>>
PageRank算法
查看>>
git的介绍和配置
查看>>
C 语言 习题 1-14
查看>>
密码锁
查看>>
值类型和引用类型区别,一看就懂
查看>>
UVa 11375 Matches
查看>>
JdbcTemplate
查看>>
leetcode 2. 两数相加(Add Two Numbers)
查看>>
Crimm Imageshop 2.0 发布。
查看>>
Xcode中修改默认文件头部注释
查看>>
从一个针对ASP.NET MVC框架的Controller.Action的请求处理顺序来说整个请求过程。
查看>>
pandas read excel文件碰到的一个小问题
查看>>
教师发表职称论文须注意事项
查看>>
libGDX简介
查看>>
《深入理解计算机系统(第三版)》第二章学习总结
查看>>
JavaScript专题——专题三 JavaScript 面向对象
查看>>
快速排序
查看>>
crontab调用python脚本新思路
查看>>
df和du显示的磁盘空间使用情况不一致的原因及处理(文件删除后磁盘空间不释放)...
查看>>