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


 

1.从新闻url获取新闻详情: 字典,anews

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')#转换成datetime类型
    return dt
#内容
def anews(url):
    newsDetail = {}
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'html.parser')
    newsDetail['newsTitle'] = soup.select('.show-title')[0].text#题目
    showinfo = soup.select('.show-info')[0].text
    newsDetail['newsDT'] = newsdt(showinfo)#时间
    newsDetail['newsClick'] = click(newsUrl)#点击次数
    return newsDetail

newsUrl = 'http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0404/11155.html'
print(anews(newsUrl))
获取一条新闻的详情

相关文章:

  • 2021-04-21
猜你喜欢
  • 2021-12-15
  • 2021-09-17
  • 2021-07-14
相关资源
相似解决方案