'''
import requests
from bs4 import BeautifulSoup
res=requests.get('http://news.sina.com.cn/china/')
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
for news in soup.select('.news-item'):
if len(news.select('h2'))>0:
title=news.select('h2')[0].text
time=news.select('.time')[0].text
net=news.select('a')[0]['href']
print(title,time,net,sep='\n')
'''
#以上获取新浪新闻首页标题,时间,网址
import requests
from bs4 import BeautifulSoup
res=requests.get('http://news.sina.com.cn/o/2017-12-09/doc-ifypnqvn1930279.shtml')
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
print(soup.select('#artibodyTitle')[0].text)
#以上获取单个新闻标题
time1=soup.select('.time-source')[0].contents[0].strip()
from datetime import datetime
time=datetime.strptime(time1,'%Y年%m月%d日%H:%M')
print(time)
#以上获取正文右上角时间
#时间和来源可以同时获得,以上将其分开
print(soup.select('.time-source span a')[0].text)
#以上获取来源
'''
body=[]
for body1 in soup.select('#artibody p')[:-1]:
#print(body1)
#print(body.append(body1))
#print(body1.text)
body.append(body1.text.strip())
print('//'.join(body))
'''
#以上获取正文
print('//'.join([body1.text.strip() for body1 in soup.select('#artibody p')[:-1]]))
#一行代码,获取正文(另一种方法)
print(soup.select('.show_author')[0].text)
#获取正文下方责任编辑
print(soup.select('.article-keywords span')[0].text)
#获取正文下方关键词
keyw=[]
for keyw1 in soup.select('.article-keywords a'):
keyw.append(keyw1.text)
#print(keyw)
print('\t'.join(keyw))
#获取3个关键词
com_res=requests.get('http://comment5.news.sina.com.cn/page/info?version=1&\
format=js&channel=sh&newsid=comos-fypnqvn1930279&group=&\
compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20')
#print(com_res.text)
import json
jd=json.loads(com_res.text.strip('var data='))
print(jd['result']['count']['total'])
#以上取得新闻评论数
newsurl='http://news.sina.com.cn/o/2017-12-09/doc-ifypnqvn1930279.shtml'
#print(newsurl.split('/')[-1].strip('doc-i').rstrip('.shtml'))
import re
m=re.search('doc-i(.+).shtml',newsurl)
print(m.group(1))
#正则表达式获取新闻编号
#以上获取新闻编号
commentsurl='http://comment5.news.sina.com.cn/page/info?version=1&\
format=js&channel=sh&newsid=comos-{}&group=&\
compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20'
def getcomments(newsurl1):
m=re.search('doc-i(.+).shtml',newsurl1)
commentsurl2=commentsurl.format(m.group(1))
com_res1=requests.get(commentsurl2)
jd=json.loads(com_res1.text.strip('var data='))
return jd['result']['count']['total']
news='http://news.sina.com.cn/o/2017-12-09/doc-ifypnqvn1930279.shtml'
print(getcomments(news))
#以上获取新闻评论函式,输入新闻网站
#以下是代码运行结果
国家卫生计生委:远程医疗已覆盖所有国家级贫困县
2017-12-09 07:28:00
央视新闻
原标题:国家卫生计生委:远程医疗已覆盖所有国家级贫困县//国家卫生计生委昨天(8日)举行例行发布会,会上介绍说,我国已将远程医疗覆盖1.3万家医疗机构和所有国家级贫困县,今年开展远程会诊,远程病理、影像、心电诊断等服务超过6000万例次,并在医联体内对符合条件的慢性病患者实施处方延伸。目前,2777家医疗机构提供移动支付结算方式,同比增加1399家;并有一半以上的三级医院开展了日间手术。
责任编辑:初晓慧
文章关键词:
远程医疗 国家级贫困县 国家卫生计生委
1
fypnqvn1930279
1
获取新浪新闻信息
最新推荐文章于 2021-02-04 06:16:08 发布
本文介绍了一种使用Python的requests库和BeautifulSoup库爬取新浪新闻网站信息的方法。通过发送HTTP请求获取网页内容,再利用BeautifulSoup解析,实现了新闻标题、发布时间、正文内容、责任编辑及关键词等信息的抓取。
1万+

被折叠的 条评论
为什么被折叠?



