python统计次数正则_python-使用正则表达式,取得点击次数,函数抽离

1.源代码

importrequestsfrom bs4 importBeautifulSoupfrom datetime importdatetimeimportre#====================================================================#2-7. 将456步骤定义成一个函数 def getClickCount(newsUrl):#====================================================================

defgetClickCount(newsUrl):#====================================================================

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

#====================================================================

newsRes=newsUrlprint("正则表达式·获取新闻编号:",re.match("http://news.gzcc.cn/html/\d{4}/xiaoyuanxinwen_(.*).html",newsRes).group(1).split('/')[-1])#====================================================================

#2-5. 生成点击次数的Request URL

#====================================================================

Source="http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html"clickUrl="http://oa.gzcc.cn/api.php?op=count&id=9183&modelid=80"

#newsId=re.search("\_(.*).html",Source).group(1).split("/")[-1]

#====================================================================

#2-6. 获取点击次数

#====================================================================

resc=requests.get(clickUrl)

w=resc.text.split(".html")[-1].lstrip("('").rstrip("');")print("正则表达式·获取点击次数:",w)#========================================================================#2-8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):#========================================================================

defgetNewDetail(newsUrl):

res=requests.get(newsUrl)

res.encoding='utf-8'soup=BeautifulSoup(res.text,'html.parser')globalsoupDetailfor news in soup.select('li'):if len(news.select('.news-list-title'))>0:

pert=news.select('.news-list-title')[0].text #pertitle 每则新闻题目

perdt=news.select('.news-list-info')[0].contents[0].text #perDetail 每则新闻详细内容

perhref=news.select('a')[0].attrs['href'] #perHref 每则新闻源链接

#————————————爬取子页面内容——————————————————————————

globalsoupDetail

perdetail=requests.get(perhref)

perdetail.encoding='utf-8'soupDetail=BeautifulSoup(perdetail.text,'html.parser')

textContent=soupDetail.select('#content')[0].text#————————————输出内容——————————————

print('题目:',pert)print('发布时间:',perdt)print('源页面:',perhref)print('正文内容:',textContent)break

#========================================================================#1-1.用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。#========================================================================

url='http://news.gzcc.cn/html/xiaoyuanxinwen/'newsUrl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'getNewDetail(url)

getClickCount(newsUrl)#=============================================================#1-2.分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。#=============================================================

info=soupDetail.select('.show-info')[0].text

catagory=['发布时间:','审核:','作者:','摄影:','来源:','点击:']

i=0while(i

valid=info.find(catagory[i])if(valid>=0):

s=info[info.find(catagory[i]):].split()[0].lstrip(catagory[i])#=============================================================#1-3.将其中的发布时间由str转换成datetime类型。#=============================================================

if(valid>0 and i==0):

timeC=datetime.strptime(s,'%Y-%m-%d %H:%M:%S')print(catagory[i]+s)

i=i+1

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

mailCheck='^(\w)+([\.\_\-]\w+)*@((\w)+(\.\w{2,3}){1,3})$'input='znJane@333.com'

print("正则表达式·判定邮箱输入:")if(re.match(mailCheck,input)):print('match success.')else:print('please review ur input.')#====================================================================#2-2. 用正则表达式识别出全部电话号码。#====================================================================

telRes='学校办公室:020-82876130 招生电话:020-82872773'telCheck='\d{3,4}-\d{6,8}'result=re.findall(telCheck,telRes)print("正则表达式·识别全部电话号码:",result)#====================================================================#2-3. 用正则表达式进行英文分词。re.split('',news)#====================================================================

EngSplRes='It could soon cost less to operate a factory of robots in the United States than to employ a human worker in Africa. That is the finding of a report from a London-based research organization. The report warns that automation could have a bad effect on developing economies. It says to avoid that governments must invest in digitalization and skills training.Businesses traditionally have sent production work to the developing world where labor is less costly. But, technology may soon bring an end to that.The policy research organization Overseas Development Institute recently examined furniture manufacturing in Africa.'

print("正则表达式·进行英文分词:",re.split("[\s,.?!:\'\"]",EngSplRes))

2.截图

1875c58f546a50d44bbf56c13dab17de.png


版权声明:本文为weixin_30818373原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。