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


a = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html'

# 1. 用正则表达式判定邮箱是否输入正确。
r = '^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$'
e = '3947653@qq.com'
if re.match(r,e):
    print('success')
else:
    print('please input :')

#2. 用正则表达式识别出全部电话号码。
str = '''版权所有:广州商学院   地址:广州市黄埔区九龙大道206号 学校办公室:020-82876130   招生电话:020-82872773 粤公网安备 44011602000060号    粤ICP备15103669号'''
print(re.findall('(\d{3,4}[) -]\d{6,8})',str))

# 3. 用正则表达式进行英文分词。re.split('',news)
news='''I like to read novel so much, especially the novels about how to find the murder. I want to be a detective when I grow up, because it means I am a smart person. I can catch the bad guys and help the good people. What’s more, people will respect me. It is so great to be a hero and save the world. '''
new = re.split("[\s+\n\.\,\']", news)
print(new)

# 4. 使用正则表达式取得新闻编号
a2 = re.search('\_(.*).html',a).group(1).split('/')[-1]
print(a2)

# 5. 生成点击次数的Request URL
newsId = re.search('\_(.*).html',a).group(1).split('/')[-1]
clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId)
print(clickUrl)

# 6. 获取点击次数
resc = requests.get(clickUrl).text.split('.html')[-1].lstrip("('").rstrip("');")
print(resc)

# 7. 将456步骤定义成一个函数 def getClickCount(newsUrl):
def getClickCount(newsUrl):
    newsId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1]
    clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId)
    resc = requests.get(clickUrl).text.split('.html')[-1].lstrip("('").rstrip("');")
    print('编号:'+newsId)
    print(clickUrl)
    print('点击数:'+resc)
    return

getClickCount('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html')

# 8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):
def getNewDetail(newsUrl):
    res = requests.get(newsUrl)
    res.encoding = 'utf-8'
    soupd = BeautifulSoup(res.text, "html.parser")
    info = soupd.select('.show-info')[0].text
    dt = info.lstrip('发布时间')[1:20]
    dt = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
    print(dt)
    i = info.find('来源:')
    z = info.find('作者:')
    c = info.find('审核:')
    sy = info.find('摄影:')
    if i > 0:
        s = info[info.find('来源:'):].split()[0].lstrip('来源:')
        print('来源:'+s)
    if z > 0 :
        z = info[info.find('作者:'):].split()[0].lstrip('作者:')
        print( '作者:' + z)
    if c > 0 :
        c = info[info.find('审核:'):].split()[0].lstrip('审核:')
        print( '审核:' + c)
    if sy > 0:
        sy = info[info.find('摄影:'):].split()[0].lstrip('摄影:')
        print('摄影:'+ sy)
    # print('正文:' + soupd.select('.show-content')[0].text)
    # print(getClickCount(newsUrl))
    return

getNewDetail('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0411/9203.html')