Sign in to follow this  
Followers 0
SuperCrazyJoel

Output the contents of a HTML tag in a specific <div>

9 posts in this topic

#1 ·  Posted (edited)

I am currently working on a script that will output the contents of a specific HTML tag in a specific <div>.

Thus far, I have managed to output the contents of the entire div (in plain text).

#include <IE.au3>
#include <MsgBoxConstants.au3>


Local $oIE = _IECreate("www.autoitscript.com")
Local $oDiv = _IEGetObjById($oIE, "header")


MsgBox($MB_SYSTEMMODAL, "Obtain Title", $oDiv.innerText)

However,  I only need the contents of the <h1> tag. 

Secondary question, in my div "header" there is also some text contained in <p> tags. I want to also output the line that begins with a certain phrase e.g. SEARCH("Select this line").

All help appreciated. 

Edited by SuperCrazyJoel

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You'll need to use StringRegExp

$Data = StringRegExp($oDiv.innerText, "(?i)(?s)<h1>(.*?)</h1>", 1)

That should get you what you want.

What that is doing is searches for <h1> and then copies everything until it finds </h1>

Didn't test but I think that will work out.

StringRegExp is a complicated and vast amount of information so to understand it fully you'll want to look through the help document extensively. But that should get you started and a clear idea of what it does.

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites

Thanks for the reply. However, the problem is .innerText removes the HTML tags (thus the regular expression cannot find anything). 

I understand that _IEGetObjById returns an object variable. The regular expression needs to search this raw object variable (not the innerText). How can I do this?

Share this post


Link to post
Share on other sites

Use $oDiv.innerHtml instead  of $oDiv.innerText

Share this post


Link to post
Share on other sites

Another quick question - this time about the regex. It didn't like the / therefore I edited the regex: 

StringRegExp($oDiv.innerHtml, "(?i)(?s)<h1>(.*?)<\/h1>",1)

On a regex tester (PCRE) - I get results as expected. On autoit - I don't get results. 

Share this post


Link to post
Share on other sites

A forward slash doesn't need to be escaped and should cause no issue. What does "it didn't like the /" mean?

Please post reproducer to help us help you.

#include <Array.au3>

Local $sIn = "this is a made up sample containing <h1>something interesting</h1> and more blah"
Local $aRes = StringRegExp($sIn, "(?is)<h1>(.*?)</h1>", 1)
_ArrayDisplay($aRes)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
No problem.
 
#include <IE.au3>
#include <MsgBoxConstants.au3>


Local $oIE = _IECreate("http://www.w3schools.com/html/default.asp")
Local $oDiv = _IEGetObjById($oIE, "main")
Local $oInnerHTML = $oDiv.innerHTML
Local $oTitle = StringRegExp($oInnerHTML, "(?is)<h1>(.*?)</h1>", 1)


;MsgBox($MB_SYSTEMMODAL, "Obtain Title", $oDiv.innerHTML)
;MsgBox($MB_SYSTEMMODAL, "Obtain Title", $oTitle)


;ConsoleWrite($oInnerHTML)
ConsoleWrite($oTitle)

Share this post


Link to post
Share on other sites

Please refer to my sample code and/or to the help file: StringRegExp returns an ...


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

As StringRegExp returns an array, you must check an array element

#include <IE.au3>
#include <MsgBoxConstants.au3>

Local $oIE = _IECreate("http://www.w3schools.com/html/default.asp")
Local $oDiv = _IEGetObjById($oIE, "main")
Local $oInnerHTML = $oDiv.innerHTML
Local $oTitle = StringRegExp($oInnerHTML, "(?is)<h1>([^<]+)", 3)[0]

MsgBox($MB_SYSTEMMODAL, "Obtain Title", $oTitle)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0