Python Forum
Search for a text in a html file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Search for a text in a html file (/thread-15996.html)



Search for a text in a html file - ebolisa - Feb-09-2019

Hi,

I need to search for the ip number from the file below. The problem is that the ip is dynamic so it changes often.

I appreciate any help.

This' the code I use to generate the text:

#!/usr/bin/env python3

import requests as req
import re

url = 'http://myurl'

def getmyip():
    r = req.get(url)
    if(r.status_code == req.codes.ok):
      t = r.elapsed # returns datetime.timedelta(0, 1, 666890)
      print('elapsed time: {}'.format(t))
      txt = r.text
      print(txt)

getmyip()



RE: Search for a text in a html file - snippsat - Feb-09-2019

What's the url do you try to get IP from?
What you get back now is JSON,should use r.json().
Then it will be Python dictionary,and get values as you would with a dictionary.


RE: Search for a text in a html file - ebolisa - Feb-09-2019

Unfortunatelly, I cannot share my url as it's using a personal key to show the results.

this' what I get with r.json()

Output:
{'isShared': False, 'name': 'WashupPI', 'isPreview': False, 'widgets': [{'y': 8, 'width': 5, 'value': ' 3 ds 12 hrs 27 m', 'tabId': 0, 'x': 0, 'fontSize': 'MEDIUM', 'max': 1.0, 'pin': 17, 'isDefaultColor': False, 'height': 1, 'color': -1, 'pwmMode': False, 'type': 'DIGIT4_DISPLAY', 'min': 0.0, 'rangeMappingOn': False, 'label': 'Uptime', 'pinType': 'VIRTUAL', 'deviceId': 0, 'frequency': 60000, 'id': 113201735}, {'y': 8, 'width': 3, 'value': '81.110.111.172', 'tabId': 0, 'x': 5, 'fontSize': 'MEDIUM', 'max': 1.0, 'pin': 18, 'isDefaultColor': False, 'height': 1, 'color': -1, 'pwmMode': False, 'type': 'DIGIT4_DISPLAY', 'min': 0.0, 'rangeMappingOn': False, 'label': 'External IP', 'pinType': 'VIRTUAL', 'deviceId': 0, 'frequency': 600000, 'id': 782408619}, {'y': 0, 'offButtonState': {'text': 'Activar S1 (3 veces)', 'textColor': 255, 'backgroundColor': -308477697}, 'width': 7, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1327861279, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 0, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 1, 'offButtonState': {'text': 'Activar S2 (3 veces)', 'textColor': 255, 'backgroundColor': -308477697}, 'width': 7, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 597002699, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 1, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 2, 'offButtonState': {'text': 'Activar S', 'textColor': 255, 'backgroundColor': 79755519}, 'width': 7, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1330870261, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 2, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 3, 'offButtonState': {'text': 'Activar XL', 'textColor': 255, 'backgroundColor': 79755519}, 'width': 7, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1975890842, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 3, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 4, 'offButtonState': {'text': 'Activar XXL', 'textColor': 255, 'backgroundColor': 79755519}, 'width': 7, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 2061439691, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 4, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 5, 'offButtonState': {'text': 'Apagar WiFi', 'textColor': 255, 'backgroundColor': -1}, 'width': 4, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -750560001}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1734656511, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 10, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 6, 'offButtonState': {'text': '?Luz', 'textColor': 255, 'backgroundColor': 600084223}, 'width': 4, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 736875659, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 6, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 7, 'offButtonState': {'text': '? Sirena', 'textColor': 255, 'backgroundColor': 600084223}, 'width': 4, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1101465001, 'buttonStyle': 'SOLID', 'x': 0, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 5, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 5, 'offButtonState': {'text': '? Puerta', 'textColor': 255, 'backgroundColor': 600084223}, 'width': 4, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1246182759, 'buttonStyle': 'SOLID', 'x': 4, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 8, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 6, 'offButtonState': {'text': '? Caja', 'textColor': 255, 'backgroundColor': 600084223}, 'width': 4, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'isDefaultColor': True, 'pwmMode': False, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 769998085, 'buttonStyle': 'SOLID', 'x': 4, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 7, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 7, 'offButtonState': {'text': 'Persiana', 'textColor': 255, 'backgroundColor': -750560001}, 'width': 3, 'tabId': 0, 'lockSize': False, 'onButtonState': {'textColor': -1, 'backgroundColor': -1}, 'value': '0', 'pwmMode': False, 'isDefaultColor': True, 'rangeMappingOn': False, 'edge': 'PILL', 'color': 600084223, 'id': 1203295534, 'buttonStyle': 'SOLID', 'x': 4, 'pushMode': True, 'height': 1, 'max': 1.0, 'pin': 9, 'fontSize': 'MEDIUM', 'min': 0.0, 'pinType': 'VIRTUAL', 'deviceId': 0, 'type': 'STYLED_BUTTON'}, {'y': 0, 'width': 1, 'value': '0', 'tabId': 0, 'x': 7, 'max': 255.0, 'pin': 14, 'isDefaultColor': False, 'height': 1, 'color': -308477697, 'pwmMode': False, 'type': 'LED', 'min': 0.0, 'rangeMappingOn': False, 'label': 'S1', 'pinType': 'VIRTUAL', 'deviceId': 0, 'id': 928748809}, {'y': 1, 'width': 1, 'value': '0', 'tabId': 0, 'x': 7, 'max': 255.0, 'pin': 15, 'isDefaultColor': False, 'height': 1, 'color': -308477697, 'pwmMode': False, 'type': 'LED', 'min': 0.0, 'rangeMappingOn': False, 'label': 'S2', 'pinType': 'VIRTUAL', 'deviceId': 0, 'id': 1561384447}, {'y': 2, 'width': 1, 'value': '0', 'tabId': 0, 'x': 7, 'max': 255.0, 'pin': 11, 'isDefaultColor': False, 'height': 1, 'color': -308477697, 'pwmMode': False, 'type': 'LED', 'min': 0.0, 'rangeMappingOn': False, 'label': 'S', 'pinType': 'VIRTUAL', 'deviceId': 0, 'id': 17403846}, {'y': 3, 'width': 1, 'value': '0', 'tabId': 0, 'x': 7, 'max': 255.0, 'pin': 12, 'isDefaultColor': False, 'height': 1, 'color': -308477697, 'pwmMode': False, 'type': 'LED', 'min': 0.0, 'rangeMappingOn': False, 'label': 'XL', 'pinType': 'VIRTUAL', 'deviceId': 0, 'id': 1968886652}, {'y': 4, 'width': 1, 'value': '0', 'tabId': 0, 'x': 7, 'max': 255.0, 'pin': 13, 'isDefaultColor': False, 'height': 1, 'color': -308477697, 'pwmMode': False, 'type': 'LED', 'min': 0.0, 'rangeMappingOn': False, 'label': 'XXL', 'pinType': 'VIRTUAL', 'deviceId': 0, 'id': 2145715107}, {'y': 7, 'width': 1, 'value': '0', 'tabId': 0, 'x': 7, 'max': 255.0, 'pin': 16, 'isDefaultColor': False, 'height': 1, 'color': -750560001, 'pwmMode': False, 'type': 'LED', 'min': 0.0, 'rangeMappingOn': False, 'id': 1884574540, 'deviceId': 0, 'pinType': 'VIRTUAL'}], 'keepScreenOn': False, 'isAppConnectedOn': True, 'widgetBackgroundOn': False, 'isDefaultColor': False, 'isActive': False, 'theme': 'Blynk', 'updatedAt': 1549750193461, 'devices': [{'boardType': 'Raspberry Pi 3 B', 'vendor': 'washupPi', 'isUserIcon': False, 'name': 'washupPi', 'connectionType': 'ETHERNET', 'id': 0}], 'createdAt': 1505399768812, 'parentId': -1, 'isNotificationsOff': False, 'color': 0, 'id': 1644267628}

PS
What I'm interested in is reading this line:

Output:
{'y': 8, 'width': 3, 'value': '81.110.111.172', 'tabId': 0, 'x': 5, 'fontSize': 'MEDIUM', 'max': 1.0, 'pin': 18, 'isDefaultColor': False, 'height': 1, 'color': -1, 'pwmMode': False, 'type': 'DIGIT4_DISPLAY', 'min': 0.0, 'rangeMappingOn': False, 'label': 'External IP', 'pinType': 'VIRTUAL', 'deviceId': 0, 'frequency': 600000, 'id': 782408619}



RE: Search for a text in a html file - snippsat - Feb-09-2019

It's now a Python dictionary that also has mixed in list,which is common when data come it from JSON.
>>> json_data['widgets'][1]
{'y': 8, 'width': 3, 'value': '81.110.111.172', 'tabId': 0, 'x': 5, 'fontSize': 'MEDIUM', 'max': 1.0, 'pin': 18, 'isDefaultColor': False, 'height': 1, 'color': -1, 'pwmMode': False, 'type': 'DIGIT4_DISPLAY', 'min': 0.0, 'rangeMappingOn': False, 'label': 'External IP', 'pinType': 'VIRTUAL', 'deviceId': 0, 'frequency': 600000, 'id': 782408619}

>>> json_data['widgets'][1]['value']
'81.110.111.172' 



RE: Search for a text in a html file - ebolisa - Feb-09-2019

Beautifull. Thank you very much!!