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

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

学会使用正则表达式

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

import rew='^(/w)+(\.\w+)*@(\w)+((\/\w{2,3}){1,3})$'s='915908192@qq.com'if re.match(w,s):    print(re.match(w,s).group(0))else:    print('error')

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

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

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

import renews = ''''It was summer. A duck  was sitting in her nest. Her little ducklings were about to hatched.     One egg after another began to crack,but the biggest one was still there. At last, it cracked.    The baby was big and ugly. '''word = re.split('[\s,.?]',news)print(word)

 

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

import renewsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html'www=re.search('\_(.*).html',newsUrl).group(1)print(www)

 

5. 生成点击次数的Request URL

import renewsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html'newsId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1]qw = 'http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80'.format(newsId)print(qw)

 

6. 获取点击次数

import requestsimport renewsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html'newsId=re.search('\_(.*).html', newsUrl).group(1).split('/')[-1]pp = requests.get('http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80'.format(newsId))ClickCount=(int(pp.text.split('.html')[-1].lstrip("(')").rstrip("');")))print(ClickCount)

 

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

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

 

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

def getNewsDetail(newsUrl):    resd = requests.get(newsUrl)    resd.encoding = 'utf-8'    soupd = BeautifulSoup(resd.text, 'html.parser')  # 打开新闻详情页    c = soupd.select('.show-title')[0].text  # 正文    info = soupd.select('.show-info')[0].text    ws = datetime.strptime(info.lstrip('发布时间:')[0:19], '%Y-%m-%d %H:%M:%S')  # 发布时间    if info.find("来源:") > 0:  # 作者: 审核:来源:摄影:        source = info[info.find('来源:'):].split()[0].lstrip('来源:')    else:        source = 'none'    content = soupd.select('.show-content')[0].text.strip()    click = getClickCount(newsUrl)    print(ws, c, newsUrl, source, click)

 

9. 取出一个新闻列表页的全部新闻 包装成函数def getListPage(pageUrl):

def getListPage(pageUrl):    res = requests.get(pageUrl)    res.encoding = 'utf-8'    soup = BeautifulSoup(res.text,'html.parser')    for news in soup.select('li'):        if len(news.select('.news-list-title')) > 0:            newsUrl = news.select('a')[0].attrs['href']  # 链接            getNewsDetail(newsUrl)

 

10. 获取总的新闻篇数,算出新闻总页数包装成函数def getPageN():

def getPageN():    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')    res.encoding = 'utf-8'    soup = BeautifulSoup(res.text, 'html.parser')    pagenumber=int(soup.select('.a1')[0].text.rstrip('条'))    page = int(soup.select('.a1')[0].text.rstrip('条'))//10+1    return page

 

11. 获取全部新闻列表页的全部新闻详情。

m=getPageN()for i in range(1,m+1):    pageUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'    getListPage(pageUrl)

 

转载于:https://www.cnblogs.com/candyxue/p/8798838.html

你可能感兴趣的文章
C# 将类的内容写成JSON格式的字符串
查看>>
Android SqliteManager 源码
查看>>
iSCSI, FC和FCoE的比较和适用场景
查看>>
MySQL - 学习入门
查看>>
IT从业人员关注哪些问题
查看>>
Windows 2012 Hyper –V 3.0 New Functions
查看>>
maven部分插件配置demo
查看>>
BZOJ 2818GCD
查看>>
提交包到iTunes Connect时构建版本“正在处理”后直接消失的问题
查看>>
我的友情链接
查看>>
QQ空间技术架构之深刻揭密
查看>>
nfs常见问题解决方法
查看>>
centOS 6 安装mongoDB
查看>>
Java基础学习总结(10)——static关键字
查看>>
大型网站技术架构(六)网站的伸缩性架构
查看>>
Linux实用工具
查看>>
JDBC Statement 实例- 查询结果集
查看>>
Java消息服务JMS详解
查看>>
Grin交易原理详解
查看>>
磁盘分区以及挂接挂载
查看>>