码迷,mamicode.com
首页 > 其他好文 > 详细

获取一篇新闻的全部信息

时间:2019-04-03 18:09:59      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:cli   strip   字符   htm   单位   image   正则表达式   scl   int   

给定一篇新闻的链接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)

 技术图片

 

获取一篇新闻的全部信息

标签:cli   strip   字符   htm   单位   image   正则表达式   scl   int   

原文地址:https://www.cnblogs.com/lijiajie/p/10650440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!