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

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

时间:2018-04-11 15:02:20      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:版权   code   find   inf   次数   etc   one   电话   show   

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

import re
str=r^[a-zA-Z0-9]+(\.[a-zA-Z0-9_-]+){0,4}@[a-zA-Z0-9]+(\.[a-zA-Z0-9]+){0,4}$
are=(840805339@qq.com)
if re.match(str,are):
    print(success)
else:
    print(please input ...)

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

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

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

import re
news=‘‘‘His friend started the engine and began to taxi onto the runway of the airport. Mr. Johnson had heard that the most dangerous part of a flight were the take-off and the landing, so he was extremely frightened and closed his eyes.‘‘‘
new = re.split("[\s+\n\.\,\‘]", news) print(new)

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

5. 生成点击次数的Request URL

6. 获取点击次数

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

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

 

import requests
import re
from bs4 import BeautifulSoup
from datetime import datetime

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


def getNewsDetail(url):
    res2 = requests.get(a)
    res2.encoding = utf-8
    soup2 = BeautifulSoup(res2.text, html.parser)
    te = soup2.select(#content)[0].text
    # 爬取校园新闻首页新闻的正文

    ifd = soup2.select(.show-info)[0].text
    dt2 = ifd.lstrip(发布时间:)[:19]
    # 获取每篇新闻的发布时间

    i = ifd.find(作者:)
    if i > 0:
        s = ifd[ifd.find(作者:):].split()[0].lstrip(作者:)
        # 获取每篇新闻的作者。

    q = ifd.find(来源:)
    if q > 0:
        b = ifd[ifd.find(来源:):].split()[0].lstrip(来源:)
        # 获取每篇新闻的来源。

    c = ifd.find(摄影:)
    if c > 0:
        n = ifd[ifd.find(摄影:):].split()[0].lstrip(摄影:)
        # 获取每篇新闻的摄影。
    dtn = datetime.strptime(dt2, %Y-%m-%d %H:%M:%S)  # 将其中的发布时间由str转换成datetime类型。


url = http://news.gzcc.cn/html/xiaoyuanxinwen/
res = requests.get(url)
res.encoding = utf-8
soup = BeautifulSoup(res.text,html.parser)
for news in soup.select(li):
    if len(news.select(.news-list-title))>0:
        a = news.select(a)[0].attrs[href]
        getNewsDetail(a)
        dtn=getClickCount(a)
        print(dtn)
        break

 

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

标签:版权   code   find   inf   次数   etc   one   电话   show   

原文地址:https://www.cnblogs.com/yxbdbolgs/p/8794922.html

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