Python之路__爬虫篇:新浪新闻爬取回顾(二)
2018-01-15 22:34
525 查看
接着上面章节 根据新闻的URL 拿到新闻内详情
#2- 获取新闻内详情 入参: http://news.sina.com.cn/c/nd/2018-01-12/doc-ifyqqciz6031881.shtml def getNewsDetail(newsURL): newsModel = {} reContent = requests.get(newsURL) reContent.encoding = 'utf-8' soupContent = BeautifulSoup(reContent.text, 'html.parser') # 获取newsURL print(newsURL) # 新闻ID match = re.search('doc-i(.*?).shtml', newsURL) newsID = match.group(1) print(newsID) # 新闻标题 title = soupContent.select('.main-title')[0].text print(title) # 获取时间 time = soupContent.select('.date-source span')[0].text print(time) # 获取来源 source = '' if len(soupContent.select('.date-source a')) > 0: source = soupContent.select('.date-source a')[0].text print(source) elif len(soupContent.select('.source')) > 0: source = soupContent.select('.source')[0].text print(source) else: print('当前未检测到来源', newsURL) #获取内容 article = ''.join([article.text.strip() for article in soupContent.select('.article p')]) # for article in soupContent.select('.article p'): # print(article.text) print(article) #获取编辑/作者 show_author = soupContent.select('.show_author')[0].text print(show_author) newsModel['newsID'] = newsID newsModel['newsHref'] = newsURL newsModel['title'] = title newsModel['time'] = time newsModel['source'] = source newsModel['article'] = article newsModel['show_author'] = show_author return newsModel
相关文章推荐
- Python之路__爬虫篇:新浪新闻爬取回顾(一)
- Python之路__爬虫篇:新浪新闻爬取回顾(四)
- Python之路__爬虫篇:新浪新闻爬取回顾(三)
- python3爬虫-爬取新浪新闻首页所有新闻标题
- python爬取新浪新闻存储到excel
- Python爬虫:抓取新浪新闻数据
- Python学习之路(仅供学习py的学者复习回顾)
- python3爬取新浪新闻文章内容代码
- python2.7 爬虫初体验爬取新浪国内新闻_20161130
- python - 采集 新浪新闻-国内-最新消息 转为xlsx
- Python 2013 精彩回顾:新闻、好文和资源
- python3+urllib撸新浪滚动新闻爬虫
- python 爬取新浪国内新闻含获取js里的数据
- Python简单抓取新浪某网页新闻链接及标题
- python爬虫之获取新浪新闻信息
- python 自动化之路 day 18 前端内容回顾、补充/Django安装、创建
- Python之路【番外篇】回顾&类的静态字段
- Python 编写新浪新闻网络爬虫(学习整理)
- python爬虫 根据关键字在新浪网站查询跟关键字有关的新闻条数(按照时间查询)
- Python爬虫爬取新浪新闻内容