第六课 Python新浪新闻爬虫最终整理总结

代码整理封装如图:


6不6?

Python简洁又强大!

至此,教程圆满结束,还有什么不懂的或有疑问的问题,欢迎大家加我的QQ:1099718640

顺便再送上完整代码,凑凑字数,哈哈~(其实推荐大家去下载Github上的内容,顺便给个小心心什么的

#第六课 封装整理
#作者:DYBOY
#时间:2017-09-06

import requests
import json
import re

from bs4 import BeautifulSoup
from datetime import datetime
#以上作为基本引用

#获取评论数量
def getCommentCounts(newsurl):
    newsid = re.search('doc-i(.*).shtml',newsurl).group(1)
    commentURL='http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-{}&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20'
    comments = requests.get(commentURL.format(newsid))
    com_num = json.loads(comments.text.strip('var data='))['result']['count']['total']
    return com_num

#获取新闻详情页
def getNewsDetail(newsurl):
    result = {}
    res = requests.get(newsurl)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'html.parser')
    result['title'] = soup.select('#artibodyTitle')[0].text
    result['time'] = datetime.strptime(soup.select('.time-source')[0].contents[0].strip(),'%Y年%m月%d日%H:%M')
    result['source'] = soup.select('.time-source span a')[0].text
    result['article'] = ' '.join([p.text.strip() for p in soup.select('#artibody p')[:-1]])
    result['editor'] = soup.select('.article-editor')[0].text.lstrip('责任编辑:')
    result['com_num'] = getCommentCounts(newsurl)
    return result

#获取新闻列表
def getNewsList(sortURL):
    res = requests.get(sortURL)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'html.parser')
    for news in soup.select('.news-item'):
        if( len(news.select('h2')) >0 ):
            h2 = news.select('h2')[0].text
            time = news.select('.time')[0].text
            newsurl = news.select('a')[0]['href']
            wenzhang = getNewsDetail(newsurl)
            print(time,h2,newsurl,wenzhang)

#MAIN-获取新闻
getNewsList('http://news.sina.com.cn/china/')

教程到这里就结束了,但是如果大家还有什么不懂的欢迎联系我,同时也希望各位能有所较大收获!

祝各位学业有成,事事顺心!

注:本文属于原创文章,转载请注明本文地址!

作者QQ:1099718640

CSDN博客主页:http://blog.csdn.net/dyboy2017

Github开源项目:https://github.com/dyboy2017/spider


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值