获取一篇新闻的全部信息

改作业要求来源于: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

url='http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html'

def new(url):
    res = requests.get(url)
    res.encoding = ('utf-8')
    soup = BeautifulSoup(res.text, 'html.parser')
    title = soup.select('.show-title')[0].text.split()
    author = soup.select('.show-info')[0].text.split()[2]
    auditor = soup.select('.show-info')[0].text.split()[3]
    come = soup.select('.show-info')[0].text.split()[4]

    # 获取点击次数
    clickurl = 'http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'
    res2 = requests.get(clickurl)
    click = res2.text.split('.html')[-1].lstrip("('").rstrip("');")

    # 获取时间
    newsdate = soup.select('.show-info')[0].text.split()[0].split(':')[1]
    newstime = soup.select('.show-info')[0].text.split()[1]
    time = newsdate + ' ' + newstime
    time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')

    p = print(title, '\n', author, '\n', auditor, '\n', come, '\n', time, '\n', "点击:", click)
    return p

new(url)

  效果如下图:

 

posted @ 2019-04-01 21:44  cmhbaba  阅读(109)  评论(0编辑  收藏  举报