zoukankan      html  css  js  c++  java
  • 【大数据应用技术】作业六|获取一篇新闻的全部信息

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

    目标:给定一篇新闻的链接newsUrl,获取该新闻的全部信息:标题、作者、发布单位、审核、来源、发布时间、点击次数等,并把整个过程包装成一个简单清晰的函数。

    源代码如下:

     1 import requests
     2 from bs4 import BeautifulSoup
     3 from datetime import datetime
     4 import re
     5 
     6 #获取新闻的全部信息
     7 def newsInfo(url):
     8     news = requests.get(url)
     9     news.encoding = 'utf-8'
    10     newSoup = BeautifulSoup(news.text,'html.parser')
    11     #标题
    12     title = newSoup.select('.show-title')[0].text
    13     print('标题:'+title)
    14     #发布信息
    15     newInfo = newSoup.select('.show-info')[0].text
    16     #发布时间
    17     newDate = newInfo.split()[0].lstrip('发布时间:')
    18     newTime = newInfo.split()[1]
    19     newDateTime = newDate+' '+newTime
    20     
    21     print('发布时间:'+newDateTime)
    22     #作者
    23     author = newInfo.split()[2]
    24     print(author)
    25     #审核
    26     examine = newInfo.split()[3]
    27     print(examine)
    28     #来源
    29     source = newInfo.split()[4]
    30     print(source)
    31     
    32     #获取点击次数的url
    33     id = re.findall('(d{1,7})',url)[-1]
    34     clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)
    35     click = requests.get(clickUrl)
    36     newClick = int(click.text.split('.html')[-1].lstrip("('").rstrip("');")) # 获取点击次数
    37     print('点击次数:')
    38     print(newClick)
    39     
    40     #内容
    41     newContent = newSoup.select('.show-content')[0].text
    42     print('内容:'+newContent)
    43     return;
    44 
    45 url = 'http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html'
    46 newsInfo(url)

    运行结果如图:

  • 相关阅读:
    DirectShow自带实例StillCap在回调函数里实现抓图并保存为文件
    x264 VS2008下编译成功
    yuy2_to_i420,yuyv_to_i420
    x264源码阅读
    oracle 归档日志开启、关闭及删除归档日志
    TOMCAT设置JVM
    linux root 操作oracle命令
    struts2 标签判断list是否为空
    linux下mysql 5.5配置
    RHEL 6 下VNC Server 的安装配置
  • 原文地址:https://www.cnblogs.com/bhuan/p/10636237.html
Copyright © 2011-2022 走看看