【大数据作业六】获取一篇新闻的全部信息

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

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

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

发布时间:转换成datetime类型

点击:

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

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

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

 主要代码:

 1 import requests
 2 import re
 3 from bs4 import BeautifulSoup
 4 def click(url):
 5     id=re.findall('(\d{1,5})',url)[-1]
 6     clickUrl='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)
 7     res=requests.get(clickUrl)
 8     newsClick=res.text.split('.html')[-1].lstrip("('").rstrip("');")
 9     newsDJ='点击次数:'+newsClick+''
10     return newsDJ​-
11 def newsdt(showinfo):
12     newsDate=showinfo.split()[0].split(':')[1]
13     newsTime=showinfo.split()[1]
14     newsDT=newsDate+' '+newsTime
15     dt=datetime.strptime(newsDT,'%Y-%m-%d %H:%M:%S')
16     return dt
17 def anews(url):
18     res=requests.get('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0328/11080.html')
19     res.encoding='utf-8'
20     soup=BeautifulSoup(res.text,'html.parser')
21     title=soup.select('.show-title')[0].text
22     newsTitle='新闻标题:'+title
23     time=soup.select('.show-info')[0].text[5:24]
24     time=soup.select('.show-info')[0].text.split()[0].lstrip('发布时间:')
25     newsDate='发布日期:'+time
26     shenyuan=soup.select('.show-info')[0].text[38:43]
27     laiyuan=soup.select('.show-info')[0].text[46:56]
28     bianhao=re.findall('(\d{1,5})',url)[-1]
29     newsBH='新闻编号:'+bianhao
30     detail=soup.select('.show-content p')[0].text[2:1000]
31     newsDetail='新闻内容:'+detail
32     pr=(newsTitle,newsDate,newsBH,shenyuan,laiyuan,newsDetail)
33     return pr
34 url='http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0328/11080.html'
35 print(click(url))
36 print(anews(url))
View Code

显示结果:

猜你喜欢

转载自www.cnblogs.com/makky1116/p/10635621.html
今日推荐