代码改变世界

获取一篇新闻的全部信息

2019-04-01 19:18  科ke  阅读(208)  评论(0编辑  收藏  举报
import requests
from datetime import datetime
from bs4 import BeautifulSoup


def newsdt(shareinfo):
    newsDate = shareinfo.split()[0].split(':')[1]
    newsTime = shareinfo.split()[1]
    showtime = datetime.strptime(newsDate + " " + newsTime, "%Y-%m-%d %H:%M:%S")
    print("新闻发布时间:", end="")
    print(showtime)


click_url = 'http://oa.gzcc.cn/api.php?op=count&id=11086&modelid=80'
def click(click_url):
    return_click_num = requests.get(click_url)
    click_info = BeautifulSoup(return_click_num.text, 'html.parser')
    click_num = int(click_info.text.split('.html')[3].split("'")[1])
    print("点击次数:", end="")
    print(click_num)


html_url = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_0922/8232.html'
def anews(html_url):
    resourses = requests.get(html_url)
    resourses.encoding = 'UTF-8'
    soup = BeautifulSoup(resourses.text, 'html.parser')


    print("\n新闻标题:" + soup.select('.show-title')[0].text)
    publishing_unit = soup.select('.show-info')[0].text.split()[4].split('')[1]
    print("新闻发布单位:", end="")
    print(publishing_unit)
    print("作者:", end="")
    writer = soup.select('.show-info')[0].text.split()[2].split('')[1]
    print(writer)
    print("新闻内容:" + soup.select('.show-content')[0].text.replace('\u3000', ''))
    shareinfo = soup.select('.show-info')[0].text

    newsdt(shareinfo)
    click(click_url)

anews(html_url)