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

新浪新闻按keyword抓取实例的更多相关文章

  1. Python爬虫:新浪新闻详情页的数据抓取(函数版)

    上一篇文章<Python爬虫:抓取新浪新闻数据>详细解说了如何抓取新浪新闻详情页的相关数据,但代码的构建不利于后续扩展,每次抓取新的详情页时都需要重新写一遍,因此,我们需要将其整理成函数, ...

  2. Python_网络爬虫(新浪新闻抓取)

    爬取前的准备: BeautifulSoup的导入:pip install BeautifulSoup4 requests的导入:pip install requests 下载jupyter noteb ...

  3. selenium+BeautifulSoup+phantomjs爬取新浪新闻

    一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs. ...

  4. python3爬虫-爬取新浪新闻首页所有新闻标题

    准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...

  5. 门户级UGC系统的技术进化路线——新浪新闻评论系统的架构演进和经验总结(转)

    add by zhj:先收藏了 摘要:评论系统是所有门户网站的核心标准服务组件之一.本文作者曾负责新浪网评论系统多年,这套系统不仅服务于门户新闻业务,还包括调查.投票等产品,经历了从单机到多机再到集群 ...

  6. 小爬新浪新闻AFCCL

    1.任务目标: 爬取新浪新闻AFCCL的文章:文章标题.时间.来源.内容.评论数等信息. 2.目标网页: http://sports.sina.com.cn/z/AFCCL/ 3.网页分析 4.源代码 ...

  7. 网易新闻页面信息抓取(htmlagilitypack搭配scrapysharp)

    转自原文 网易新闻页面信息抓取(htmlagilitypack搭配scrapysharp) 最近在弄网页爬虫这方面的,上网看到关于htmlagilitypack搭配scrapysharp的文章,于是决 ...

  8. 今天写了一个简单的新浪新闻RSS操作类库

    今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些. 一年前我写过一个RSS阅读器,不过 ...

  9. Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现

    UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...

随机推荐

  1. angularjs ng-app

    <!DOCTYPE HTML> <html ng-app> //ng-app是初始化指令,整个页面都会被angularjs解析,写在div或者其他标签上表示只是局部的div和标 ...

  2. array_unique和array_flip 这两个函数的区别

    array_unique和array_flip 这两个函数的区别 标签(空格分隔): php array_unique 和 array_flip 验证 1 没有排序的数组 2 array_unique ...

  3. Lua,github,nginx

    个人觉得这几种工具都是大学里,至少是前几年未曾出现于课本中或者图书馆中的.而如今确实某些公司赫然出现在招聘需求中的东西. 什么是Lua,为什么要用Lua——做手机游戏而被推广的.Lua原来早在93年就 ...

  4. 200 from memory cache / from disk cache / 304 Not Modified 区别

    三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...

  5. 数据库Tsql语句创建--约束--插入数据

    1.创建数据库 use master go if exists(select * from sysdatabases where name='数据库名字') drop database 数据库名字 g ...

  6. tf.nn.top_k(input, k, name=None)和tf.nn.in_top_k(predictions, targets, k, name=None)

    tf.nn.top_k(input, k, name=None) 这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引. input: 一个张量,数据类型必须是以下之一 ...

  7. BZOJ 1176[Balkan2007]Mokia(CDQ分治)

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3381  Solved: 1520[Submit][S ...

  8. CSU 1506 Double Shortest Paths

    1506: Double Shortest Paths Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 5 Description ...

  9. C++11之decltype

    使用场景 在C++中常常要用到非常长的变量名.假设已经有变量和你将使用的变量是一个类型.就可以使用decltypekeyword 来申明一样的类型变量. decltype原理      返回现有变量类 ...

  10. TI C66x DSP 四种内存保护问题 -之- 外设訪问corePac内部资源时的内存保护问题

    外设訪问corePac内部资源(L1,L2)时的内存保护等问题请參考以下两个blog.已经叙述的非常具体. "TI C66x DSP 系统events及其应用 - 2"," ...