获取一篇新闻的全部信息

前端之家收集整理的这篇文章主要介绍了获取一篇新闻的全部信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

发布时间:转换成datetime类型

点击:

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

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

 

尝试去爬取一个你感兴趣的网页。

新闻链接http://news.gzcc.cn/html/2018/xiaoyuanxinwen_1109/10359.html

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

url = http://news.gzcc.cn/html/2018/xiaoyuanxinwen_1109/10359.html



# 获取新闻的全部信息
def newsinformation(url):
    news = requests.get(url)
    news.encoding = utf-8
    newSoup = BeautifulSoup(news.text,html.parser)

    #获取新闻标题
    title = newSoup.select(.show-title)[0].text
    print(标题 + title)

    # 获取新闻发布的相关信息
    newInfo = newSoup.select(.show-info)[0].text
    #print(newInfo)

    # 发布时间
    newDate = newInfo.split()[0].lstrip(发布时间:)
    newTime = newInfo.split()[1]
    newDateTime = newDate +   + newTime
    print(发布时间: + newDateTime)

    # 作者
    author = newInfo.split()[2]
    print(author)

    # 审核
    examine = newInfo.split()[3]
    print(examine)

    # 来源
    source = newInfo.split()[4]
    print(source)

    # 获取点击次数的url
    id = re.findall((\d{1,7}),url)[-1]
    clickUrl = http://oa.gzcc.cn/api.PHP?op=count&id={}&modelid=80.format(id)
    click = requests.get(clickUrl)
    newClick = int(click.text.split(.html)[-1].lstrip("(‘").rstrip("‘);"))  # 获取点击次数
    print(点击次数)
    print(newClick)

newsinformation(url)

爬取结果:

分享图片

猜你在找的正则表达式相关文章