def setUp(self): self.q = GumboQuery('test.html')
import os import sys import json # add up directory to sys for import lib sys.path.append('../') from lib.GetElement import GumboQuery q = GumboQuery( "http://b2b.cnyes.com/news/ochart/nav_Chart_data.aspx?code=B08,080") dt = q.query("table > tr > td[class=dt]") rt = q.query("table > tr > td[class=rt]") dtData = [x.text for x in dt] rtData = [x.text for x in rt] allData = [] for x in xrange(len(dtData)): allData.extend([{"dt": dtData[x], "rt": rtData[x]}]) text = json.dumps(allData) with open("config.json", 'w') as fo: fo.write(text)
class GumboQueryTest(unittest.TestCase): def setUp(self): self.q = GumboQuery('test.html') def test_query_class(self): x = self.q.query('.myClass') self.assertEqual(x[0].text, unicode('345')) def test_query_id(self): x = self.q.query('#myID') self.assertEqual(x[0].text, unicode('Good Idea')) def test_query_element(self): x = self.q.query('a') self.assertEqual(x[0].text, unicode('gg')) self.assertEqual(x[1].text, unicode('a link')) x = self.q.query('h1') self.assertEqual(x[0].text, unicode('123')) self.assertEqual(x[1].text, unicode('345')) def test_query_element_children(self): x = self.q.query('div > a') self.assertEqual(x[0].text, unicode('a link')) x = self.q.query('.tt > #jj') self.assertEqual(x[0].text, unicode('JOB')) def test_query_attrs(self): x = self.q.query('div[title=good]') self.assertEqual(x[0].text, unicode('345')) def test_query_in_depth(self): x = self.q.query('table > td > .dt') self.assertEqual(x[0].text, unicode('2014/01/27')) x = self.q.query('table > td > .rt') self.assertEqual(x[0].text, unicode('12.5400')) x = self.q.query('table > tr > th') self.assertEqual(x[0].text, unicode('Title')) x = self.q.query('table > tr > th') self.assertEqual(x[1].text, unicode('Item')) def test_find_id(self): x = self.q.findID('div', 'myID') self.assertEqual(x[0].text, unicode('Good Idea')) def test_find_title(self): x = self.q.findTitle('h1', 'cc') self.assertEqual(x[0].text, unicode('345'))
import os import sys import json # add up directory to sys for import lib sys.path.append('../') from lib.GetElement import GumboQuery q = GumboQuery("http://b2b.cnyes.com/news/ochart/nav_Chart_data.aspx?code=B08,080") dt = q.query("table > tr > td[class=dt]") rt = q.query("table > tr > td[class=rt]") dtData = [x.text for x in dt] rtData = [x.text for x in rt] allData = [] for x in xrange(len(dtData)): allData.extend( [{"dt": dtData[x], "rt": rtData[x]}]) text = json.dumps(allData) with open("config.json",'w') as fo: fo.write(text)
import sys # add up directory to sys for import lib sys.path.append('../') from lib.GetElement import GumboQuery q = GumboQuery(open('../test.html').read()) data = q.query(".nba-stat-type-15")