Results 1 to 3 of 3

Thread: Extract data from HTML string in Python with PyQt WebView

  1. #1
    Join Date
    Mar 2017
    Posts
    1
    Qt products
    Platforms
    Windows

    Default Extract data from HTML string in Python with PyQt WebView

    So i'm using the Google Maps API to work out the straight-line distance between 2 points on the map.
    Qt Code:
    1. gmaps="""
    2. ***generic google maps HTML api stuff first, then:***
    3.  
    4. overalldist=calcDistance(firstcoord, secondcoord);
    5. alert(overalldist)
    6.  
    7. //calculates distance between two points in km's
    8. function calcDistance(firstcoord, secondcoord) {
    9. return (google.maps.geometry.spherical.computeDistanceBetween(firstcoord, secondcoord) / 1000).toFixed(2);
    10. """
    To copy to clipboard, switch view to plain text mode 

    This is within the script section of my HTML code which is all placed within a simple string in Python. The string is called by:
    Qt Code:
    1. self.webView.setHtml(gmaps)
    To copy to clipboard, switch view to plain text mode 
    Where gmaps is the string name that contains the HTML and webView is the name of the HTML viewer in my PyQt GUI.

    The outcome is a popup box outputting the correct distance.

    My issue is that I cannot extract the variable 'overalldist' from the HTML. The alert pops up with the correct distance but I then cannot move this into my Python code.

    Please advise.

    Any further ideas?

    It is the function calcDistance I am attempting to retrieve from the HTML code to use in my Python code.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Extract data from HTML string in Python with PyQt WebView

    You didn't mention which version of Qt you are using so it is difficult to give any help as the availale API of web views depends on which you are using.

    If we assume WebKit1 based QWebView, you can for example create a subclass of QWebPage and override the javaScriptAlert() method to get the message instead of a dialog being shown.

    Or you modify the HTML document inside the script and extract the element you put the result into, see the page's web frames.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Extract data from HTML string in Python with PyQt WebView

    So i'm using the Google Maps API to work out the straight-line distance between 2 points on the map.
    Unless you are developing an app around Google Maps, wouldn't it be easier just to do the calculation yourself instead of going through all of the overhead of using the Google API and then having to parse the result just to get a single number? I am sure that Wikipedia could give you the equation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 13th September 2013, 14:12
  2. Replies: 1
    Last Post: 18th October 2011, 08:32
  3. Webkit: extract information from HTML
    By bunjee in forum Qt Programming
    Replies: 7
    Last Post: 1st January 2011, 20:50
  4. Replies: 0
    Last Post: 9th November 2009, 17:03
  5. Python + PyQt IRC Client
    By crisp in forum Newbie
    Replies: 1
    Last Post: 25th January 2009, 06:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.