import time import platform import os import subprocess class Main: def __init__(self, url, outputFilePath): self.url = url self.outputFilePath = outputFilePath # temp file name self.fileName = str(time.time()) + ".html" def _is_windows(self): if "Windows" in str(platform.uname()): return True else: return False def run(self): if self._is_windows(): proc = subprocess.Popen(["cmd", "/c", "casperjs %s/site.js --url=%s --outputfile=%s" % (self.outputFilePath, self.url, self.fileName) ], stdout=subprocess.PIPE) else: proc = subprocess.Popen(["bash", "-c", "cd %s && casperjs site.js --url=%s --outputfile=%s" % (self.outputFilePath, self.url, self.fileName) ], stdout=subprocess.PIPE) out = proc.communicate()[0] htmlFileName = '' # 因为输出路径在windows不确定,所以这里加了所有可能的路径判断 if os.path.isfile(self.fileName): htmlFileName = self.fileName elif os.path.isfile(os.path.join(self.outputFilePath, self.fileName)): htmlFileName = os.path.join(self.outputFilePath, self.fileName) elif os.path.isfile(os.path.join(os.path.dirname(os.path.realpath(__file__)), self.fileName)): htmlFileName = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.fileName) if (not os.path.isfile(htmlFileName)): print('Failed to get html content from ', self.url) if __name__ == '__main__': Main(url="http://www.mouser.cn/Mobile/", outputFilePath="C:\\SourcePython\\uuspider\\test").run();