使用python脚本下载www.wuxia.net.cn上的书籍,并且将它们合并成一个文件
来源:互联网 发布:抽腹水的人活不久知乎 编辑:程序博客网 时间:2024/06/07 14:20
如题,太累了,直接贴上source,应该能看明白,不明白的话,请给我回复,我会及时回复。
# -*- coding: cp936 -*-import sgmllibimport urllib2import HTMLParserimport codecsclass BookParser(sgmllib.SGMLParser): def __init__(self): # inherit from the SGMLParser class sgmllib.SGMLParser.__init__(self) # create a list this will store all the links found self.links = [] self.bookNames = [] self.inside_a_element = 0 self.count = 0; def unknown_starttag(self, tag, attrs): #print "unknown tag start " + tag for key, value in attrs: if key.lower() == "href": if "/book/" in value and ".html" in value: self.links.append(value) self.inside_a_element = 1 def unknown_endtag(self, tag): #print "in end tag" if self.inside_a_element: self.inside_a_element = 0 self.count += 1 def handle_data(self, text): #print text if self.inside_a_element: text = unicode(text, 'utf-8') text = text + str(self.count) + ".txt" self.bookNames.append(text) codecs.open(text, 'a', "utf-8")class PageParser(sgmllib.SGMLParser): def __init__(self): # inherit from the SGMLParser class sgmllib.SGMLParser.__init__(self) # create a list this will store all the links found self.links = [] def unknown_starttag(self, tag, attrs): #print "unknown tag start " + tag for key, value in attrs: if key.lower() == "href": if "/book/" in value and ".html" in value: self.links.append(value) class ContentParser(sgmllib.SGMLParser): def __init__(self, bookNames): # inherit from the SGMLParser class sgmllib.SGMLParser.__init__(self) # create a list this will store all the divs(page content) found self.divs = [] # create a list this will store all the h1(title) found self.headone = [] print " in content parser " + bookNames self.logfile = codecs.open(bookNames, 'a', "utf-8") self.inside_a_element = 0 self.h = HTMLParser.HTMLParser() # this function is called once an anchor tag is found def unknown_starttag(self, tag, attrs): #print "unknown tag start " + tag if tag.lower() == "h1": self.inside_a_element = 1 if tag.lower() == "p": self.inside_a_element = 2 def unknown_endtag(self, tag): if self.inside_a_element: self.inside_a_element = 0 self.logfile.write("\n") def handle_data(self, text): #print "handle data " + text if self.inside_a_element == 1: text = unicode(text, 'utf-8') self.logfile.write(text + "\n\n") if self.inside_a_element == 2: text = unicode(text, 'utf-8') self.logfile.write(text) def handle_charref(self, ref): print "#############" print "chart is " + ref print self.h("&#%(ref)") def handle_entityref(self, ref): self.logfile.write(self.h.unescape("&"+ref+";")) #print "#############" #print "enttity is + " + ref #print self.h.unescape("&"+ref+";")def getBookList(url): bookDict = {} sock = urllib2.urlopen(url) # make sure the string that is going to be parsed is 8-bit ascii if sock.info().dict['content-type'] == 'text/html': parser = BookParser() parser.feed(sock.read()) bookDic = dict(zip(parser.links, parser.bookNames)) return bookDicdef getPageList(bookUrl): pageList = [] sock = urllib2.urlopen(bookUrl) # make sure the string that is going to be parsed is 8-bit ascii if sock.info().dict['content-type'] == 'text/html': parser = PageParser() parser.feed(sock.read()) pageList = parser.links return pageListdef getPageContent(pageUrl, bookNames): sock = urllib2.urlopen(pageUrl) # make sure the string that is going to be parsed is 8-bit ascii if True: parser = ContentParser(bookNames) parser.feed(sock.read()) # print out links for link in parser.divs: print linkdef main(wuxiaUrl): bookDic = getBookList(wuxiaUrl) print type(bookDic) print bookDic for link, bookNames in bookDic.iteritems(): print "link is " + link print "bookNames is " + bookNames pageList = getPageList("http://www.wuxia.net.cn" + link) for page in pageList: getPageContent("http://www.wuxia.net.cn" + page, bookNames)if __name__ == '__main__': # this str is author's page main("http://www.wuxia.net.cn/author/shiweihan.html") #if len(sys.argv) < 2: # print("Usage: %s xuxiaurl"%sys.argv[0]) #else: # main(sys.argv[1])
- 使用python脚本下载www.wuxia.net.cn上的书籍,并且将它们合并成一个文件
- http://www.ebookee.com.cn/ 不错的书籍下载站点
- 一个免费的电影网站 www.dy5.net.cn
- python读写excel(附上将一个文件夹下所有excel文件合并成一个文件的代码)
- 从多个url下载pdf,并且合并成一个
- 如何将两个有重复字符在内的字符串合并成一个,并且排序
- debian 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件
- 要求能够将多个文件合并为一个文件,并且能够将被打包文件再次释放
- 在python中调用dll,并将dll以及python合并打包成一个exe文件的方法
- java生成一个XML文件,并且将该文件压缩成ZIP格式后再写到硬盘上
- 以后的日志将全部写在我自己买的空间上,http://www.aspneter.cn
- python 多线程将大文件分开下载后在合并
- 用脚本提取一个文件夹内的某些文件,并且将文件复制到制定目录下
- python将某一目录下的多个文件合并为一个文件
- 写一个函数,将字符串首尾空格去除,并且字符串之间的多个空格合并成一个。
- 使用Pyinstaller将python脚本打包成exe文件
- Python爬取www.alexa.cn网站上的部分url和相应的等级
- python实现将文件夹内所有txt文件合并成一个文件
- poj 1067
- Linux 内核中的 GCC 特性(转)
- SQLServer2008 快捷键集合
- 如何成为一名合格的硬件工程师
- 败者树 K-路归并排序
- 使用python脚本下载www.wuxia.net.cn上的书籍,并且将它们合并成一个文件
- 约瑟夫问题的解法
- 推荐一家不错的IT资讯网站
- oracle非系统用户
- 设置Centos系统时间
- 对于macbook,软件不重要!
- C语言中的运算符
- 求1234567891011121314151617181920*2019181716151413121110987654321的值
- 使用进度条指示进度