def test_hogwarts_json(self): r = requests.get('https://home.testing-studio.com/categories.json') print(r.text) assert r.status_code == 200 assert r.json( )['category_list']['categories'][0]['name'] == '霍格沃兹测试学院公众号' print(jsonpath(r.json(), '$..name')) assert jsonpath(r.json(), '$..name')[0] == '霍格沃兹测试学院公众号'
def sync_data(self): self.heatpumpstate = jsonpath( heat_pumps, '$.[?(@.deviceLabel == \'' + self.id + '\')]')[0] self._name = jsonpath(self.heatpumpstate, '$.area')[0] d = dateutil.parser.parse( jsonpath(self.heatpumpstate, '$.heatPumpConfig.changedTime')[0]) self._current_temperature = jsonpath( self.heatpumpstate, '$.latestClimateSample.temperature')[0] if self._config_date is None or self._config_date < d: self._target_temperature = jsonpath( self.heatpumpstate, '$.heatPumpConfig.targetTemperature')[0] hvac_mode = jsonpath(self.heatpumpstate, '$.heatPumpConfig.mode')[0] self._on = True if jsonpath( self.heatpumpstate, '$.heatPumpConfig.power')[0] == 'ON' else False if self._on: self._hvac_mode = VERISURE_TO_HA_STATE[hvac_mode] else: self._hvac_mode = HVAC_MODE_OFF self._fan_mode = jsonpath(self.heatpumpstate, '$.heatPumpConfig.fanSpeed')[0].title() self._swing_mode = jsonpath( self.heatpumpstate, '$.heatPumpConfig.airSwingDirection.vertical')[0].title() self._config_date = d
def test_tag_list(): corpsecret = "4Y8E2yFo3qNuWKNHmq16eiF28nqhrEkmFNxn9oTyQmg" corpid = 'ww3ba71edc4fc65297' r = requests.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken", params={ "corpid": corpid, "corpsecret": corpsecret }) assert r.status_code == 200 assert r.json()['errcode'] == 0 print(json.dumps(r.json(), indent=2)) token = r.json()['access_token'] r = requests.post( 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list', params={'access_token': token}, json={'tag_id': []}) print("======") print(json.dumps(r.json(), indent=2)) print("======") assert r.status_code == 200 assert r.json()['errcode'] == 0 tag_name = "hogwarts01_1_update" + str( datetime.datetime.now().strftime("%s")) print(tag_name) r = requests.post( "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag", params={'access_token': token}, json={ "id": "etL_AEDgAAmLkcZSXNBosVXWhnExVQaA", "name": tag_name }) print("======") print(json.dumps(r.json(), indent=2)) print("======") assert r.status_code == 200 assert r.json()['errcode'] == 0 # tags = [] print(r.raw) r = requests.post( 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list', params={'access_token': token}, json={'tag_id': ["etL_AEDgAAmLkcZSXNBosVXWhnExVQaA"]}) tags = [] for group in r.json()['tag_group']: if group['group_name'] == 'hogwarts01': for tag in group['tag']: if tag['name'] == tag_name: tags.append(tag) print(tags) jsonpath(f"$..[?(@.name='{tag_name}')]") assert tags != ""
def setUpClass(cls) -> None: url = conf.get('env', 'base_url') + '/member/login' params = { 'mobile_phone': conf.get('test_data', 'mobile'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) response = requests.post(url=url, json=params, headers=headers) res = response.json() token = jsonpath(res, '$..token')[0] headers['Authorization'] = 'Bearer ' + token cls.headers = headers cls.member_id = jsonpath(res, '$..id')
def test_xueqiu_quote(self): params = { '_t': '1UNKNOWNc60715cb4a61425b311034a49f4aa024.3446260779.1563002521424.1563005246620', '_s': '8c6b2d', 'category': '1', 'pid': -1, 'size': 10000, 'x': 1.3, 'page': 1 } headers = { 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4', 'User-Agent': 'Xueqiu Android 11.19', 'Host': 'stock.xueqiu.com' } cookies = { 'xq_a_token': '5806a70c6bc5d5fb2b00978aeb1895532fffe502', 'u': '3446260779' } response = requests.get(self.url, params=params, headers=headers, cookies=cookies) logging.info(json.dumps(response.json(), indent=2)) assert jsonpath()
def test_demo(self): #pass r = requests.get("http://www.baidu.com") print(r.status_code) i = r.__dict__ print("type>>>{}".format(type(i))) header = jsonpath(i, "$.headers") print("h[0]: {}".format(type(header[0]))) print("header:{}".format(header)) print("server???{}".format(jsonpath(header[0], "$..Server"))) # for k,v in r.__dict__.items(): # print("{}:::{}".format(k,v)) print("json:{}".format(r.json())) #print(r.__dict__.items()) print(type(requests.session()))
def test_get_label_member(self): """ *获取标签成员 """ tagid = 1 r = self.label_manager.get_label_member(tagid) print(r.json()) # assert r.json()['errcode'] == 0 assert jsonpath(r.json(), '$.errcode') == 0
def test_01(self): #logging.basicConfig(level="INFO") params = {"category": "1"} url = "https://stock.xueqiu.com/v5/stock/portfolio/stock/list.json" cookies = { "xq_a_token": "f3646a14c8cc784b6ca2ddfaabfc34cbd6d36e86", "u": "5261478148" } header = { "Host": "stock.xueqiu.com", "User-Agent": "Xueqiu Android 12.8.1" } r = requests.get(url, headers=header, params=params, cookies=cookies) print(r.status_code) logging.info(str(r.status_code)) logging.info(json.dumps(r.json(), indent=2)) assert_that(jsonpath(r.json(), "$..name"), any_of(has_item("招商银行"), has_item("中国平安"))) assert "招商银行" or "中国平安" in jsonpath(r.json(), "$..name")
def update_overview(event_time): """Update the overview.""" global heat_pumps try: heat_pumps = jsonpath(session.get_overview(), '$.heatPumps')[0] except ResponseError as ex: _LOGGER.error('Could not read overview, %s', ex) if ex.status_code == 503: # Service unavailable _LOGGER.info('Trying to log in again') session.login() else: raise
def setup_platform(hass, config, add_entities, discovery_info=None): global session global heat_pumps configs = [] if (discovery_info): configs = [PLATFORM_SCHEMA(x) for x in discovery_info] else: configs = [config] for c in configs: username = c.get(CONF_USERNAME) password = c.get(CONF_PASSWORD) session = Session(username, password) session.login() heat_pumps = jsonpath(session.get_overview(), '$.heatPumps')[0] track_time_interval(hass, update_overview, SCAN_INTERVAL) for heat_pump in heat_pumps: device_label = jsonpath(heat_pump, '$.deviceLabel')[0] add_entities([HeatPump(device_label)])
def jsonpath_base_use(): url = "http://www.lagou.com/lbs/getAllCitySearchLabels.json" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" } # 发送请求 # 注意:只有返回是json文件才可以使用json()方法 data = requests.get(url, headers=headers).json() # 解析-返回结果为列表 parse_name = jsonpath(data, '$..name') for name in parse_name: print name
def get_x_y(item, wd): headers = { 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36', 'Accept': '*/*', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Dest': 'empty', # 'Referer': 'https://map.baidu.com/search/%E5%B9%BF%E4%B8%AD%E8%B7%AF598%E5%8F%B7/@13522382.115,3647548.76,19z?querytype=s&da_src=shareurl&wd=%E5%B9%BF%E4%B8%AD%E8%B7%AF598%E5%8F%B7&c=289&src=0&pn=0&sug=0&l=13&b=(13505897.46,3621783.94;13552713.46,3646167.94)&from=webmap&biz_forward=%7B%22scaler%22:2,%22styles%22:%22pl%22%7D&device_ratio=2', 'Accept-Language': 'zh-CN,zh;q=0.9', } params = ( ('newmap', '1'), ('reqflag', 'pcmap'), ('biz', '1'), ('from', ['webmap', 'webmap']), ('da_par', 'after_baidu'), ('pcevaname', 'pc4.1'), ('qt', 's'), ('da_src', 'searchBox.button'), ('wd', wd), ('c', '289'), ('src', '0'), ('wd2', ''), ('pn', '0'), ('sug', '0'), ('l', '19'), ('b', '(13522223.115,3647358.26;13522541.115,3647739.26)'), ('biz_forward', '/{"scaler":2,"styles":"pl"/}'), ('sug_forward', ''), ('auth', 'VD2wW05WBgvRBE6cFQJNgQLdO@b6CNL9uxHTxRHERVVtBnlQADZZz1GgvPUDZYOYIZuVt1cv3uVtGccZcuVtPWv3GuztQZ3wWvUvhgMZSguxzBEHLNRTVtcEWe1GD8zv7u@ZPuVteuxtf0wd0vyIUySIFOUOuuyWWJ0IcvY1SGpuxEtEjjg2J' ), ('device_ratio', '2'), ('tn', 'B_NORMAL_MAP'), ('nn', '0'), ('u_loc', '13520351,3635774'), ('ie', 'utf-8'), ('t', '1591854801846'), ) response = requests.get('https://map.baidu.com/', headers=headers, params=params) res_json = response.json() res_ls = jsonpath(res_json, '$.addrs[*]') if res_ls: res = res_ls[0] Longitude = jsonpath(res, '$.x')[0] if Longitude: Longitude = list(str(Longitude)) Longitude.insert(-2, '.') Longitude = ''.join(Longitude) Latitude = jsonpath(res, '$.y')[0] if Latitude: Latitude = list(str(Latitude)) Latitude.insert(-2, '.') Latitude = ''.join(Latitude) item["Longitude"] = Longitude item["Latitude"] = Latitude InsertTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") item['InsertTime'] = InsertTime sql_ditu.handle_lo_la(item)
def baidu(ls): headers = { # 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36', 'Accept': '*/*', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Dest': 'empty', 'Referer': 'https://map.baidu.com/search/%E9%93%BE%E5%AE%B6/@13519255.82,3633244.67,12z?querytype=con&from=webmap&c=289&wd=%E9%93%BE%E5%AE%B6&pn=22&nn=210&db=0&sug=0&addr=0&da_src=shareurl&on_gel=1&src=7&gr=3&l=12&device_ratio=2', 'Accept-Language': 'zh-CN,zh;q=0.9', } s = requests.session() s.keep_alive = False for i in ls: wd = i.get("wd") Company = i.get("Company") for i in range(0, 100): print(i) if i > 1: pn = i + 1 nn = i * 10 else: pn = i nn = pn * 10 params = ( ('pn', '{pn}'.format(pn=str(pn))), ('nn', '{nn}'.format(nn=str(nn))), ('newmap', '1'), ('reqflag', 'pcmap'), ('biz', '1'), ('from', ['webmap', 'webmap']), ('da_par', 'direct'), ('pcevaname', 'pc4.1'), ('qt', 's'), ('da_src', 'searchBox.button'), ('wd', wd), ('c', '289'), ('src', '0'), ('wd2', ''), ('sug', '0'), ('l', '14'), ('b', '(13502322,3618789;13520242,3643173)'), ('biz_forward', '/{"scaler":2,"styles":"pl"/}'), ('sug_forward', ''), ('auth', 'Ib5ULR6eP0WAbzMT1FKWHRb93D1USd @ euxHTxHTNLxTt1qo6DF == C1GgvPUDZYOYIZuVt1cv3uVtGccZcuVtPWv3Guxt58Jv7uUvhgMZSguxzBEHLNRTVtcEWe1GD8zv7u @ ZPuEthyHxhjzgjyBKWBKWQOYWxk1dK84yDFICquTTGdFrZZWuV' ), ('device_ratio', '2'), ('tn', 'B_NORMAL_MAP'), ('u_loc', '13520357,3635775'), ('ie', 'utf-8'), ('t', '1591597646869'), ) response = s.get('https://map.baidu.com/', headers=headers, params=params) res_json = response.json() # test1 = jsonpath(res_json, '$.addrs') # print(test1) res_ls = jsonpath(res_json, '$.content[*]') if res_ls: for res in res_ls: item = {} flag = jsonpath(res, '$.di_tag')[0] if '房产中介' in flag: StoreName = jsonpath(res, '$.name')[0] StoreAddr = jsonpath(res, '$.addr')[0] Longitude = jsonpath(res, '$.x')[0] if Longitude: Longitude = list(str(Longitude)) Longitude.insert(-2, '.') Longitude = ''.join(Longitude) Latitude = jsonpath(res, '$.y')[0] if Latitude: Latitude = list(str(Latitude)) Latitude.insert(-2, '.') Latitude = ''.join(Latitude) Status = jsonpath(res, '$.status')[0] TelPhone = jsonpath(res, '$.ext.detail_info.phone')[0] InsertTime = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") item["Company"] = Company item["StoreName"] = StoreName item["StoreAddr"] = StoreAddr item["Longitude"] = Longitude item["Latitude"] = Latitude item["Status"] = Status item["TelPhone"] = TelPhone item['InsertTime'] = InsertTime sql_ditu.handle_lo_la(item) else: print("最大页数") break
def get_val_by_xpath(json_obj, xpath_exp): json_grp = jsonpath(json_obj, xpath_exp) l = [] for i in json_grp: l.append(str(i)) return l
def jsonpath(self,json_object,expr): return jsonpath(json_object,expr)