#coding:utf-8
import urllib2
import sys
import re def getPage(url,offset = ''):
realurl = "%s%s%s" %(url,offset,'.shtml')
print realurl
resp = urllib2.urlopen(realurl)
content = resp.read()
#print content
p = re.compile('<[^>]+>')
p1=re.compile('<[^>p]+>')
print p
rematch = re.compile(r'(<h1.*</h1>)')
h1 = rematch.findall(content)
print h1[0],'ok'
try:
h1content = p.sub("",h1[0])
print h1content
except Exception,e:
print str(e),'error'
return
fp = open(r'juyudao.txt','a')
fp.write(h1content+ '\n')
fp.flush() #print content content = content.replace('\r','')
content = content.replace('\n','') content = content.replace(' ','') content = content.replace(' ','')
cont = re.search('articleBody(.*)class="pages">', content, re.S)#先获取一部分html
#print 'cont1',cont.group()
cont1=cont.group()
articleBody=re.findall('</script>(.*)<divclass="pages">',cont1)
#print articleBody
articleBody=articleBody[0].replace('</p>','')
articleBody=p1.sub('',articleBody)
txt=articleBody.split('<p>')
for i in txt:
fp.write(i+ '\n')
fp.flush() fp.close() def getBook(url, startoffset, endOffset):
while startoffset < endOffset:
getPage(url, offset = str(startoffset))
startoffset += 1 if __name__ == '__main__':
getPage(url = 'http://book.sohu.com/20131107/n389762800',offset='')
getBook(url = 'http://book.sohu.com/20131107/n389762800_',startoffset=1,endOffset=20)

python 爬虫-sohu抓小说的更多相关文章

  1. Python爬虫实战---抓取图书馆借阅信息

    Python爬虫实战---抓取图书馆借阅信息 原创作品,引用请表明出处:Python爬虫实战---抓取图书馆借阅信息 前段时间在图书馆借了很多书,借得多了就容易忘记每本书的应还日期,老是担心自己会违约 ...

  2. 【转】Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  3. Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  4. Python爬虫实现抓取腾讯视频所有电影【实战必学】

    2019-06-27 23:51:51 阅读数 407  收藏 更多 分类专栏: python爬虫   前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问 ...

  5. Python爬虫,抓取淘宝商品评论内容!

    作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...

  6. python爬虫之抓取小说(逆天邪神)

    2022-03-06 23:05:11 申明:自我娱乐,对自我学习过程的总结. 正文: 环境: 系统:win10, python版本:python3.10.2, 工具:pycharm. 项目目标: 实 ...

  7. python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)

    转载出处:药少敏   ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...

  8. python爬虫数据抓取方法汇总

    概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...

  9. python爬虫——论抓包的正确姿势和学好Javascript的重要性(1)

    没事想爬下数据,就入了scrapy坑,跟着https://zhuanlan.zhihu.com/data-factory这篇教程走,中间被小数量的网站坑过,不过还是写出了爬虫~~ 切糕王子:毫无防御, ...

随机推荐

  1. 将assembly包添加到自己的maven仓库

    mvn install:install-file -DgroupId=com.asiainfo -DartifactId=spark-assembly -Dversion=1.2.0 -Dpackag ...

  2. 《FPGA全程进阶---实战演练》第二十一章 电源常用类型:LDO和 DCDC

    高速电路中的电源设计 高速电路中的电源设计大概分为两种,一种是集总式架构,一种是分布式架构.集总式架构就是由一个电源输入,然后生成多种所需要的电压.如图1所示.这种架构会增加多个DC/DC模块,这样成 ...

  3. win8或win8.1修改注册表失败的原因

    win8 and win8.1 modify the registry need compiled to be different versions according to the os bits.

  4. com学习前提必看

    1) COM组件实际上是一个C++类,而接口都是纯虚类.组件从接口派生而来.我们可以简单的用纯粹的C++的语法形式来描述COM是个什么东西: class IObject { public: virtu ...

  5. 再看.net本质(二)

    3.[HTTP协议]  当浏览器寻找到Web服务器的地址之后,浏览器将帮助我们把对服务器的请求转换为一系列参数发送给Web服务器.服务器收到浏览器的请求对数之后,将会分析这些数据并进行处理,然后向浏览 ...

  6. ng-class ionic

    我发现 ng-class="{yourclass:true,outerclass:false}" 竟然不起作用...囧.... 幸好有Google  .... <p ng-c ...

  7. CLRS:median and order statistics

    //maximum and minimum     暴力遍历 O(n) //i-th element dicide and conquer random_selected_partition     ...

  8. 【C++面试】常考题复习

    // CPP_Test@2014.10.22.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <assert.h&g ...

  9. git gc

    git gc 有时候当你运行一些git命令(比如git pull)的时候,会有如下提示: 1 2 Auto packing the repository for optimum performance ...

  10. Android手机指令操作释疑

    有人问我一个关于Android手机root与否的问题,她说明明iTools显示已取得root权限,但她就是没法在该手机上运行需要root权限的App如钛备份等等.我告诉她最好的确认方式便是以adb指令 ...