Python抓取小说

Python抓取小说

前言


此脚本为了在MAC上抓取小说而写,用Python几句代码就可以了。




www.dwjajf.com

代码


[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # coding=utf-8  
  2.   
  3. import re  
  4. import urllib2  
  5. import chardet  
  6. import sys  
  7. from bs4 import BeautifulSoup  
  8. import codecs  
  9.   
  10. class Spider():  
  11.   
  12.     def __init__(self):  
  13.         self.aTag=re.compile("<a href=\"(http://www.44pq.com/read/[0-9]+?_[0-9]+?.html)\"[^>]*?>(.+?)</a>")  
  14.         self.contentTag=re.compile("<div class=\"readerContent\" id=\"content\">(.+?)</div>",re.I|re.S)  
  15.   
  16.     def getHtml(self, url):  
  17.         headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}  
  18.         req=urllib2.Request(url,headers=headers)  
  19.         response = urllib2.urlopen(req)  
  20.           
  21.         html = response.read()  
  22.         return html  
  23.         #soup=BeautifulSoup(html.decode("GB18030","ignore"))  
  24.         #return soup.findAll("a")  
  25.         #return soup.prettify()  
  26.         #typeEncode = sys.getfilesystemencoding()  
  27.         #infoencode = chardet.detect(html).get('encoding','utf-8')  
  28.         #return html.decode('GB18030','ignore').encode("utf-8")  
  29.         return html.decode('GB18030','ignore').encode(sys.getfilesystemencoding())  
  30.       
  31.     def Run(self):  
  32.         bookurl="http://www.44pq.com/read/13567.html"  
  33.         bookname="地球上唯一的魔法师"  
  34.         text=[]  
  35.         matchs=self.aTag.finditer(self.getHtml(bookurl))  
  36.         alist=list(matchs)  
  37.         total = len(alist)  
  38.         print "total {0}".format(total)  
  39.         i=0  
  40.         for m in alist:  
  41.             i+=1  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值