获取一篇新闻的全部信息
作业要求: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
url=' http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html'
res=requests.get(url)
res.encoding='utf-8'
res.text
soup=BeautifulSoup(res.text,'html.parser')
soup.head
soup=BeautifulSoup(res.text,'html.parser')
title=soup.select('.show-title')[0].text
time=soup.select('.show-info')[0].text[5:24]
time=soup.select('.show-info')[0].text.split()[0].lstrip('发布时间')
detail=soup.select('.show-content p')[0].text
soup.select('.show-title')[0].text
soup.select('.show-info')[0].text[5:24]
soup.select('.show-info')[0].text.split()[0].lstrip('发布时间')
from datetime import datetime
now = datetime.now()
now.day
dt=datetime(2019,4,1)
dt.second
showinfo=soup.select('.show-info')[0].text
newsDate=showinfo.split()[0].split(':')[1]
newsTime=showinfo.split()[1]
newsDT=newsDate+' '+newsTime
newsDT
type(newsDT)
from datetime import datetime
now=datetime.now()
now
now.year
newsdt=datetime(2019,4,1)
dt.second
newsdt=datetime.strptime(newsDT,'%Y-%m-%d %H:%M:%S')
newsdt
newsdt.strftime('%B%d,%y %A')
now.strftime('%Y{y}%m{m}%d{d} %U{a}%w %H{h}%M{f}%S{s}%p').format(y='年',m='月',d='日',h='时',f='分',s='秒',a='周 星期')
url=' http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html'
clickUrl='http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'
res=requests.get(clickUrl)
res.text
url=' http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html'
clickUrl='http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'
res=requests.get(clickUrl)
newsClick=int(res.text.split('.html')[-1].lstrip("('").rstrip("');"))
newsClick
import re
re.match(' http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/(.*).html',url).group(1)