import urllib2

import requests

#import MySQLdb

import webbrowser

import string

import re

from BeautifulSoup import BeautifulSoup

def getHtml(page):#获取网址内容

    page=str(page)

    html=requests.get("http://search.sina.com.cn/?q=%BD%F0%D0%E3%CF%CD&range=all&c=news&sort=time&page="+page).text

    return html

def getPage():#获得网页总数

    html=requests.get("http://search.sina.com.cn/?

range=all&c=news&q=%BD%F0%D0%E3%CF%CD&from=home").text   #网址

    soup=BeautifulSoup(''.join(html))

    a=soup('div',{ 'class' : 'l_v2' })

    race=[]

    c=""

    race=str(a).split("新闻")[1].split("篇")[0].split(",")   #获取网址有多少页码

    b=len(race)

    for i in range(b):

        c+=race[i]

    b=string.atoi(c)/20

    return b

def getContents(html):#获取指定新闻内容

    soup=BeautifulSoup(''.join(html))

    rs=re.compile("fgray_time")

    html=soup.findAll('span',attrs={'class':rs})

    rs=re.compile("box-result clearfix")

    contents=soup.findAll('div',attrs={'class':rs})

    for c in html:

        length=len(c.text.split(' '))

        if length==3:

                   source=c.text.split(' ')[0]#新闻来源

                   time=c.text.split(' ')[1]+' '+c.text.split(' ')[2]#新闻发表时间

                   print source

                   print time

        else:

                   time=c.text#新闻发表时间

                   source=''#新闻来源

                   print time





    for i in contents:

        title= i.h2.a.text#新闻标题

        content= i.p.text#新闻简单介绍内容

##    print html

if __name__=="__main__":

    count=getPage()

    print 111

    for i in range(count):

        print getContents(getHtml(i))

    print 222

最新文章

  1. CAS 4.0.0RC编译环境
  2. IOS第11天(2:UIPickerView自定义国旗选择)
  3. ReentRantLock使用
  4. Sumblime Text2安装Package Control两种方法+安装插件+注册码
  5. MapReduce编程系列 — 4:排序
  6. 09_TomCat_基础知识
  7. uva 10026 Shoemaker's Problem
  8. 执行引擎子系统——JVM之五
  9. TCP/IP协议的编写《转载》
  10. vs2012 + web api + OData + EF + MYsql
  11. Hive详解
  12. Apache shiro集群实现 (一) shiro入门介绍
  13. Maven把项目依赖的所有jar包都打到同一个jar中
  14. python timeit模块用法
  15. ZOJ 2477 Magic Cube(魔方)
  16. servlet 遇到的奇怪问题
  17. 13.python错误和异常
  18. linux、centos下安装配置vim
  19. Slava and tanks 877C
  20. 2018.11.14 uoj#34. 多项式乘法(ntt)

热门文章

  1. List methods
  2. BZOJ 1251 Splay维护序列
  3. UI Framework-1: Aura Graphics Architecture
  4. 【Git 二】Windows|Mac 安装 Git
  5. 用VUE做网站后台
  6. 洛谷1414 又是毕业季II
  7. QQ音乐
  8. vue使用,问题
  9. OpenGL 获取当前屏幕坐标对应的三维坐标
  10. ArcGIS api for javascript——设置可见的比例