PDA

View Full Version : Extract data from HTML string in Python with PyQt WebView



PythonHelp
1st March 2017, 21:51
So i'm using the Google Maps API to work out the straight-line distance between 2 points on the map.


gmaps="""
***generic google maps HTML api stuff first, then:***

overalldist=calcDistance(firstcoord, secondcoord);
alert(overalldist)

//calculates distance between two points in km's
function calcDistance(firstcoord, secondcoord) {
return (google.maps.geometry.spherical.computeDistanceBet ween(firstcoord, secondcoord) / 1000).toFixed(2);
"""


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:


self.webView.setHtml(gmaps)

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.

anda_skoa
3rd March 2017, 12:33
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,
_

d_stranz
3rd March 2017, 15:15
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.