|
本帖最后由 mingdongtianxia 于 2017-7-28 12:39 编辑
https://github.com/FullerHua/gooseeker我从这个网站找的fuller大神的GitHub的下载包,然后按http://www.gooseeker.com/doc/thread-2072-1-1.html这个网址的教程对他的文件做了必要的修改,然后就报错了,一共两个.py文件,它们的具体的代码如下:- 1.gooseeker.py
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # 模块名: gooseeker
- # 类名: GsExtractor
- # Version: 2.1
- # 说明: html内容提取器
- # 功能: 使用xslt作为模板,快速提取HTML DOM中的内容。
- # released by 集搜客(http://www.gooseeker.com) on May 18, 2016
- # github: https://github.com/FullerHua/jisou/core/gooseeker.py
- import time
- from urllib import request
- from urllib.parse import quote
- from lxml import etree
- class GsExtractor(object):
- def _init_(self):
- self.xslt = ""
- # 从文件读取xslt
- def setXsltFromFile(self , xsltFilePath):
- file = open(xsltFilePath , 'r' , encoding='UTF-8')
- try:
- self.xslt = file.read()
- finally:
- file.close()
- # 从字符串获得xslt
- def setXsltFromMem(self , xsltStr):
- self.xslt = xsltStr
- # 通过GooSeeker API接口获得xslt
- def setXsltFromAPI(self , APIKey , theme, middle=None, bname=None):
- apiurl = "http://www.gooseeker.com/api/getextractor?key="+ APIKey +"&theme="+quote(theme)
- if (middle):
- apiurl = apiurl + "&middle="+quote(middle)
- if (bname):
- apiurl = apiurl + "&bname="+quote(bname)
- apiconn = request.urlopen(apiurl)
- self.xslt = apiconn.read()
- # 返回当前xslt
- def getXslt(self):
- return self.xslt
- # 提取方法,入参是一个HTML DOM对象,返回是提取结果
- def extract(self , html):
- xslt_root = etree.XML(self.xslt)
- transform = etree.XSLT(xslt_root)
- result_tree = transform(html)
- return result_tree
- # 提取方法,入参是html源码,返回是提取结果
- def extractHTML(self , html):
- doc = etree.HTML(html)
- return self.extract(doc)
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|
共 8 个关于本帖的回复 最后回复于 2017-7-28 19:42