def exec_action(self): parser = JsonParser.JsonParser() actions = parser.parse_stream(sys.stdin) for action in actions: output = self.__parse_action__(action) encoded_output = json.dumps(output) print(encoded_output)
def read_comands(self): parser = JsonParser.JsonParser() commands = parser.parse_stream(sys.stdin) for command in commands: op_name = command['operation-name'] func = self.command_list[op_name] output = func() print json.dumps(output)
def main(): files = [] for i in range(1, len(sys.argv)): files.append(sys.argv[i]) parser = jp.JsonParser(files) # Run program normally if output args not specified. if not check_for_output(parser): display = dis.CDisplay(parser)
def LagouSpider(keyword): lagou_url = "http://www.lagou.com/jobs/positionAjax.json?px=default&first={tp}&kw={kw}&pn={pn}" # Preprocess keyword -- transfer it into utf-8 format and add it to lagou url kw_bytes = keyword.encode('utf-8') kw_bytes = str(kw_bytes).replace(r'\x', '%').replace(r' ', '') kw_in_url = re.sub(r'^b', '', kw_bytes) # count how many results by key "totalcount" << "positionResult" << "content" in json file page_num = 0 first_url = lagou_url.format(tp='true', kw=kw_in_url, pn=str(page_num + 1)) with request.urlopen(first_url) as uf: json_data = json.loads( str(uf.read(), encoding='utf-8', errors='ignore')) resultsCount = int( json_data.get('content', {}).get('positionResult', {}).get('totalCount', 0)) print('本次搜索页面共计%d' % resultsCount) if resultsCount == 0: print("Query Keyword error or index key word error") return -1 # Game is on, let's scrap for i in range(3): if i == 0: tp = 'true' else: tp = 'false' url = lagou_url.format(tp=tp, kw=kw_in_url, pn=str(i + 1)) with request.urlopen(url) as uf: data = uf.read() try: totaldata = JsonParser.JsonParser(data) except Exception as e: continue #开始写入数据库 print(totaldata) totaldata.to_excel('lagou.xls', sheet_name='sheet1')
def main(): address = ('localhost', 8844) directory = os.getcwd() parser = JsonParser.JsonParser() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(address) s.listen(5) while True: print 'waiting for connection' (client, ip) = s.accept() try: while True: req = client.recv(1024) try: parsedreq = json.loads(req) print("sending " + parsedreq) client.send(parsedreq + ' gotem\n') except: client.send('not json bruh') pass finally: client.close()
# "{'a':1}" # '{1:1}' # '{true:1}' # '{"a":{}' # '{"a":-}' # '{"a":[,]}' # '{"a":.1}' # '{"a":+123}' # '{"a":"""}' # '{"a":"\\"}' ] file_path = 'E:/jsonStringFile.txt' for test_json_str in json_test: a1 = JsonParser.JsonParser() a2 = JsonParser.JsonParser() a3 = JsonParser.JsonParser() a1.load(test_json_str) # print a1.data d1 = a1.dumpDict() # print d1 a2.loadDict(d1) a2.dumpJson(file_path) a3.loadJson(file_path) # print a3 d3 = a3.dumpDict() # print d3 assert d1 == d3, '解析错误%s:' % test_json_str
def setUp(self): super(TestJsonParser, self).setUp(JsonParser.JsonParser(gp=mock.Mock())) self.tcp_server = TcpServer.TcpServer(gp=mock.Mock()) self.tcp_server.addReceiver("JsonParser", self.test_object)
def __init__(self): self.urls = UrlManger.UrlManger() self.downloader = JsonDownloader.JsonDownloader() self.parser = JsonParser.JsonParser() self.outputer = JsonOutputer.JsonOutputer()
def __init__(self): self.parser = JsonParser.JsonParser() self.card = 10