获取一篇新闻的全部信息

作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2894

给定一篇新闻的链接newsUrl,获取该新闻的全部信息

标题、作者、发布单位、审核、来源

发布时间:转换成datetime类型

点击:

  • newsUrl
  • newsId(使用正则表达式re)
  • clickUrl(str.format(newsId))
  • requests.get(clickUrl)
  • newClick(用字符串处理,或正则表达式)
  • int()

整个过程包装成一个简单清晰的函数。

 

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

#点击次数
def click(url):
    id = re.findall('(\d{1,5})',url)[-1]
    clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)
    resClick = requests.get(clickUrl)
    newsClick = int(resClick.text.split('.html')[-1].lstrip("('").rstrip("');"))
    return newsClick
#时间
def newsdt(showinfo):
    newsDate = showinfo.split()[0].split(':')[1]
    newsTime = showinfo.split()[1]
    newsDT = newsDate+' '+newsTime
    dt = datetime.strptime(newsDT,'%Y-%m-%d %H:%M:%S')
    return dt
#内容
def anews(url):
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'html.parser')
    newsTitle = soup.select('.show-title')[0].text
    showinfo = soup.select('.show-info')[0].text
    newsDT = newsdt(showinfo)
    newsAuthor = soup.select('.show-info')[0].text.split()[2].lstrip('作者:')
    newsAuditing = soup.select('.show-info')[0].text.split()[3].lstrip('审核:')
    newsSource = soup.select('.show-info')[0].text.split()[4].lstrip('来源:')
    newsClick = click(newsUrl)
    newsDetail = soup.select('.show-content')[0].text
    pr =print('标题:'+newsTitle,'\n发布时间:',newsDT,'\n作者:'+newsAuthor,'\n审核:'+newsAuditing,'\n来源:'+newsSource,'\n点击量:',newsClick,'次\n新闻内容:'+newsDetail)
    return pr

newsUrl = 'http://news.gzcc.cn/html/2019/tongzhigonggao_0403/11141.html'
anews(newsUrl)
获取

 

posted @ 2019-04-03 14:43  yuui  阅读(90)  评论(0编辑  收藏  举报
http://cdn.acgscript.com/script/miobt/3.js?2