PDA

View Full Version : How to get the "URL" used in style sheets of a HTML response?



bsalunke
6th October 2015, 13:28
Hello All,

I'm using qt-webkit to make http/https request and process the html response of it. I want to parse the style sheets present in the HTML response to get the specific elements of style sheets.
e.g. I have following web page received after HTTP request:


<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("http://www.w3schools.com/css/border.png"), url("img_flwr.gif");
background-color: #cccccc;

}
div {
cursor: url(http://i.imgur.com/jto5jnA.gif), auto;
}
</style>
</head>
<body>
<div>
this is text
</div>
</body>
</html>



Now from above HTML response, I want to parse the style sheet and the get the "URL" used. e.g. URL used for "background-image" or "cursor".
Any pointer on how to get these URLs used in style-sheet?

ChrisW67
6th October 2015, 21:20
You could use the QWebFrame functions to find all the style elements and then use a regular expression to find strings matching: url\s*\(\s*"[^"]*"\s*\)
Or something like that in the text of the style element. You may need to allow for both single and double quotes.

bsalunke
7th October 2015, 05:51
Thanks ChrisW67, but do we get the "style" elements which are present in the HEAD of document as mentioned in the example in above question?

ChrisW67
7th October 2015, 10:44
I don't see why you wouldn't with a selector of "style" or "head style". Have you actually tried it to see?