爬虫初步面向对象重构
This commit is contained in:
28
WebCrawler/crawler.py
Normal file
28
WebCrawler/crawler.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from lxml import etree
|
||||
|
||||
class Crawler:
|
||||
def __init__(self,htmlcode):
|
||||
self.html = etree.HTML(htmlcode)
|
||||
|
||||
def getString(self,_xpath):
|
||||
if _xpath == "":
|
||||
return ""
|
||||
result = self.html.xpath(_xpath)
|
||||
try:
|
||||
return result[0]
|
||||
except:
|
||||
return ""
|
||||
|
||||
def getStrings(self,_xpath):
|
||||
result = self.html.xpath(_xpath)
|
||||
try:
|
||||
return result
|
||||
except:
|
||||
return ""
|
||||
|
||||
def getOutline(self,_xpath):
|
||||
result = self.html.xpath(_xpath)
|
||||
try:
|
||||
return "\n".join(result)
|
||||
except:
|
||||
return ""
|
||||
Reference in New Issue
Block a user