python爬虫之获取新浪新闻信息

        一:前言

windows平台:

        1):谷歌的chrome浏览器;python3.6的软件安装包,需要导入的库有:
[python] view plain copy
  1. pip install requests  
  2. pip install BeautifulSoup4  
        2)直接用集成好的软件Anaconda

linux平台下:

       1)火狐浏览器;Ubuntu16.04已经自动安装了python2.7和python3.6;故只需导入库就可以
[python] view plain copy
  1. pip install requests  
  2. pip install BeautifulSoup4  

       二:模块化击破

        如今我们上网,网页上充满了各种各样的数据,我们怎样才能根据自己的需求,快速又准确的找到自己想要的数据呢?爬虫就产生了。网络爬虫的任务就是在浏览器中将网页上非结构化的网页数据转化为结构化的信息数据,从而快速找到满足我们的要求数据信息。
       非结构话的数据:没有固定的数据格式(ps我们上网浏览的网页就是最常见的非结构话数据);必须透过ETL(Extract,Transformation,Loading)工具将数据转换为结构化数据才能取用。
       网络爬虫的任务就是让这些原始数据经过爬虫工具整理后成为自己想要的结构化数据。即爬虫是从网页上对数据(这里的数据是原始数据 Raw Data)抽取,转换(TEL脚本),存储(结构化的数据Tidy Data)的过程。
      网络爬虫的架构描述为首先打开浏览器输入自己想处理的数据信息网页的URL,浏览器会发出Request请求,去服务器中查找相应的request请求,并返回给浏览器对应的request请求,把原始的数据显示在浏览器上,然后爬虫对这些原始的数据进行资料剖析(Data Parser),将我们想要的信息整理成结构化数据,存储在数据中心(Data Center)。
      在我们进行爬虫之前,首先要对网页上的数据显示有一个清晰的认识,所以我们要借助浏览器的开发者工具去查看这些浏览器给我们解析过的网页,我们利用开发者工具查看源码会找到对应网页资源的规律,我们就是利用这个规律去把这些非结构化的数据抽取出来转化为结构化的数据。
       在这里我们要利用BeautifulSoup这个库,它最主要最给力的功能就是从网页上抓取数据。官方解释如下:

Beautiful Soup提供一些简单的、Python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。

Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。

Beautiful Soup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度

       观察HTTP请求与返回内容(我们以爬取新浪网新闻的国内新闻为例):

      1):获取网页源码

python爬虫之获取新浪新闻信息
      1:我们鼠标点击右键,选择查看
      2:选择Network页标签
      3:选择doc
      4:选择china/
python爬虫之获取新浪新闻信息
       5:我们点击Headers就会看到这个网页的头信息。现在我们利用Request来获取这个网页:
[python] view plain copy
  1. #coding:utf-8  
  2. #Requests:网络资源获取的组件  
  3. #改善了Urllib2的缺点,让使用者以最简单的方式获取网络资源  
  4. #可以使用REST操作(POST, PUT, GET, DELETE)存取网络资源  
  5. import requests  
  6.   
  7. newsUrl = 'http://news.sina.com.cn/china/'  
  8.   
  9. #获取网页信息,返回在res中  
  10. res = requests.get(newsUrl)  
  11.   
  12. #设置获取网页内容的编码  
  13. res.encoding = 'utf-8'  
  14. #输出网页源码  
  15. print(res.text)  

       2):对网页源码进行信息搜索提取。

      首先我们先来一个简单的小例子来做实验:
       1:编写html网页,并放入BeautifulSoup中
[python] view plain copy
  1. #coding:utf-8  
  2. from bs4 import BeautifulSoup  
  3. html_sample = '\  
  4. <html>\  
  5.     <body>\  
  6.         <h1 id = "title">hello,Reptile</h1>\  
  7.         <a href="#" class="link">This is a link1</a>\  
  8.         <a href="#link2" class="link">This is a link2</a>\  
  9.     <body>\  
  10. </html>'  
  11.   
  12. #将网页存放在BeautifulSoup中  
  13. soup = BeautifulSoup(html_sample, 'html.parser')  
  14.   
  15. #获取本文信息  
  16. print(soup.text)  
       2:找出含有特定标签的HTML元素
           使用select找出含有h1标签的元素
[python] view plain copy
  1. #数据存放在header列表中  
  2. header = soup.select('h1')  
  3. #打印列表  
  4. print(header)  
  5. #打印列表第一个元素  
  6. print(header[0])  
  7. #.text提取文本信息  
  8. print(header[0].text)  
       使用select找出含有a标签的元素
[python] view plain copy
  1. #将数据存放在alink列表中  
  2. alink = soup.select('a')  
  3. print(alink)  
  4. #打印列表  
  5. for link in alink:  
  6.     #输出列表元素  
  7.     print(link)  
  8.     #提取列表元素的文本信息  
  9.     print(link.text)  
        3:取得含有特定CSS属性的元素
            使用select找出所有id为title的元素(id前面需要加#)
[python] view plain copy
  1. alink = soup.select('#title')  
  2. print(alink)  
  3. print(alink[0].text)  
           使用select找出所有class为link的元素(class前面需加点.)
[python] view plain copy
  1. alink = soup.select('.link')  
  2. print(alink)  
  3. for link in alink:  
  4.     print(link)  
  5.     print(link.text)  
           使用select找出所有a tag的href连结
[python] view plain copy
  1. alinks = soup.select('a')  
  2. for link in alinks:  
  3.     print(link['href'])  

       三:对新闻网页数据的正式爬取

1:提取页面信息标题,时间,Url
python爬虫之获取新浪新闻信息
[python] view plain copy
  1. #coding:utf-8  
  2.   
  3. #页面信息标题,时间,Url  
  4. import requests  
  5. from bs4 import BeautifulSoup  
  6. res = requests.get('http://news.sina.com.cn/china/')  
  7. res.encoding = 'utf-8'  
  8. #输出获得的页面代码  
  9. #print(res)  
  10. #print (res.text)  
  11. soup = BeautifulSoup(res.text, 'html.parser')  
  12. for newsTitle in soup.select('.news-item'):  
  13.     if len(newsTitle.select('h2')) > 0:  
  14.         h2 = newsTitle.select('h2')[0].text  
  15.         print("标题%s"%h2)  
  16.         time = newsTitle.select('.time')[0].text  
  17.         print("时间:%s"%stime)  
  18.         href = newsTitle.select('a')[0]['href']  
  19.         print("网址:%s"%href)  
  20.         #print(h2, time, href)  
[python] view plain copy
  1. print('-'*70)  
2:对某一特定内文页面的提取
       获取内文网页源码存于soup中
[python] view plain copy
  1. #coding:utf-8  
  2. import requests  
  3. from bs4 import BeautifulSoup  
  4. res = requests.get('http://news.sina.com.cn/c/nd/2017-05-02/doc-ifyetwsm1726806.shtml')  
  5. res.encoding = 'utf-8'  
  6. soup = BeautifulSoup(res.text, 'html.parser')  
  7. #获取内文网页源码存在soup中  
  8. #print(soup)  
      获取内文网页的标题:
python爬虫之获取新浪新闻信息
[python] view plain copy
  1. #获取内文网页标题  
  2. title = soup.select('#artibodyTitle')  
  3. print('标题是:%s' %title[0].text)  
      获取内文网页时间以及新闻来源:
python爬虫之获取新浪新闻信息
[python] view plain copy
  1. #获取内文时间  
  2. timeStr = soup.select('.time-source')[0].contents[0].strip()  
  3. #print(timesource)  
  4. #print(timesource[0])  
  5. #print('时间是:%s'%timesource[0].contents[0].strip())  
  6. print('时间是:%s'%timeStr)  
  7. #print(type(timeStr))  
  8. #把字符串转换为时间格式  
  9. dt = datetime.strptime(timeStr, '%Y年%m月%d日%H:%M')  
  10. print('时间是:%s'%dt)  
  11. #print(type(dt))  
  12.   
  13. #获取内文来源  
  14. source = soup.select('.time-source span a')[0].text  
  15. print('新闻来源:%s'%source)  
        获取新闻内文内容
python爬虫之获取新浪新闻信息
[python] view plain copy
  1. #获取内文内容  
  2. article = soup.select('#artibody p')[:-1]  
  3. #print(article)  
  4. article_list = []  
  5. for p in article:  
  6.     article_list.append(p.text.strip())  
  7.   
  8. #print('新闻内容:%s\n\t%s'%(article_list[0], article_list[1]))  
  9. print('新闻内容:%s'%(' '.join(article_list)))  
  10. #把上面的整合成一个语句  
  11. #print('新闻内容:%s'%(' '.join(p.text.strip() for p in soup.select('#artibody p')[:-1])))  
        获取新闻编辑:
[python] view plain copy
  1. #获取新闻编辑  
  2. author = soup.select('.article-editor')[0].text.strip('责任编辑:')  
  3. print('编辑人:%s'%author)  
       获取评论数
python爬虫之获取新浪新闻信息
[python] view plain copy
  1. #获取评论数  
  2. #输出为空说明评论数不在这里  
  3. #comment = soup.select('#commentCount1M')  
  4. #print(comment[0].text)  
       获取评论数:
python爬虫之获取新浪新闻信息
 
python爬虫之获取新浪新闻信息
[python] view plain copy
  1. #评论数  
  2. comments= requests.get('http://comment5.news.sina.com.cn/page/info?version=1&\  
  3. format=js&channel=gn&newsid=comos-fyetwsm1726806&group=&compress=0&\  
  4. ie=utf-8&oe=utf-8&page=1&page_size=20')  
  5. jd = json.loads(comments.text.strip('var data='))  
  6. print('评论数:%d 条'%jd['result']['count']['total'])  
[python] view plain copy
  1. #获取新闻编号  
  2. newsurl = 'http://news.sina.com.cn/c/nd/2017-05-02/doc-ifyetwsm1726806.shtml';  
  3. newsid = newsurl.split('/')[-1].rstrip('.shtml').lstrip('doc-i')  
  4. print('新闻编号:%s'%newsid)  
[python] view plain copy
  1. #利用正则表达式获取新闻编号  
  2. import re  
  3. m = re.search('doc-i(.*).shtml',newsurl)  
  4. print('新闻编号:%s'%m.group(1))  

        四:代码整合

[python] view plain copy
  1. #coding:utf-8  
  2. import re  
  3. import requests  
  4. import json  
  5. from bs4 import BeautifulSoup  
  6. from datetime import datetime  
  7.   
  8. commentURL = 'http://comment5.news.sina.com.cn/page/info?version=1&format=js&\  
  9. channel=gn&newsid=comos-{}&group=&compress=0&ie=utf-8&oe=utf-8&\  
  10. page=1&page_size=20'  
  11. newsurl = 'http://news.sina.com.cn/c/nd/2017-05-02/doc-ifyetwsm1726806.shtml'  
  12.   
  13.   
  14. def getCommentsCount(newsurl):  
  15.     m = re.search('doc-i(.*).shtml', newsurl)  
  16.     newsid = m.group(1)  #获取新闻id  
  17.     comments = requests.get(commentURL.format(newsid))  
  18.     jd = json.loads(comments.text.strip('var data='))  
  19.     return jd['result']['count']['total'#获取评论数  
  20.       
  21. def getNewsDatail(newsurl):  
  22.     result = {}  
  23.     res = requests.get(newsurl)  
  24.     res.encoding = 'utf-8' #设置编码  
  25.     soup = BeautifulSoup(res.text, 'html.parser')  
  26.     m = re.search('doc-i(.*).shtml', newsurl)  
  27.     newsid = m.group(1)  
  28.     result['newsid'] = newsid    
  29.     result['title'] = soup.select('#artibodyTitle')[0].text  #获取新闻标题  
  30.     result['newssource'] = soup.select('.time-source span a')[0].text  #获取新闻来源  
  31.     timesource = soup.select('.time-source')[0].contents[0].strip()   #获取新闻时间  
  32.     result['dt'] = datetime.strptime(timesource, '%Y年%m月%d日%H:%M')  #转换时间格式  
  33.     result['article'] = ' '.join([ p.text.strip for p in soup.select('#artibody p')[:-1]])  #获取新闻内容  
  34.     result['editor'] = soup.select('.article-editor')[0].text.strip('责任编辑:')  #获取新闻作者  
  35.     result['comments'] = getCommentsCount(newsurl)  #获取评论数  
  36.     return result