Python Forum
Search for a text in a html file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search for a text in a html file
#1
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()
Reply
#2
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.
Reply
#3
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}
Reply
#4
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' 
Reply
#5
Beautifull. Thank you very much!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,256 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  search file by regex SamLiu 1 860 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 877 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,062 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  If function is false search next file mattbatt84 2 1,111 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,841 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Search text in PDF and output its page number. atomxkai 21 8,671 Jan-21-2022, 06:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020