Jump to content

Newbie with a question... or two (IE focused)


Recommended Posts

Hello hello!

As the title suggests, I am fairly new to AutoIt. In fact, I am new to scripting/coding in general! I've done a few Codecademy courses on CSS and HTML and perhaps Java though this was all a few years back. I've recently come across AutoIt and decided to give it a try since I do quite a few repetitive tasks on a daily basis. In the last couple of weeks I've managed to master (or at least get comfortable with) mouse clicks(left/right), window focus, sending key strokes, controls, and pixel search.

Now let's get to the topic.

At this point in time I've tried out a few simple IE scripts but I am having difficulty understanding some things and tying everything together into one tool.

Specifically, I am struggling with this little bit of code I got from DaleHohm in his IE examples thread. Post #3 (The last example.)

#include <IE.au3>

$sImgDir = "c:\foo\"; Please make certain this folder already exists (silent failure if not)
$sWebPage = "http://www.autoitscript.com/forum/index.php?"; webpage with images

$oIE = _IECreate()
_IENavigate($oIE, $sWebPage)
$oIMGs = _IETagNameGetCollection($oIE.document, "img")

; Loop through all IMG tags and save file to local directory using INetGet
For $oIMG in $oIMGs
    $sImgUrl = $oIMG.src
    $sImgFileName = $oIMG.nameProp
    INetGet($sImgUrl,  $sImgDir & $sImgFileName)
Next

I have a couple questions about the code above.

1) ".src" ".nameProp" What are these called? I figured out that I can change the .src to something like .href and it gets anything on the webpage with a .href tag but where can I learn more about these? I still haven't been able to figure out what ".nameProp" is for or what it does. Is there any documentation/list of all the different ".PurpleTextAfterAVariable" (Edit: Not sure why it's red in the above example, just checked SciTE and it's purple there) that I can use?

2) I understand that the code above gets every "For $oIMG in $oIMGs" on the page but how can I make it only get the first 5? I've tried doing a "count" and a "for" but I am unsure what to replace the "For...in" statement with to keep the script functional. Is there a way to limit the _IETagNameGetCollection function to only get a specific amount of tags?

 

Finally, the reason I can't just use the code as is.

The site I am trying to get images from works in this way:

A href= "Link-To-Picture.jpg"

Img src= "Link-To-Picture-thumbnail.jpg"

The script above downloads every single thumbnail from the image gallery which is great, it does what it's supposed to but I need the full resolution image.

After changing the script to get anything with an "A href" tag it does what I need it to do, it gets every single image in full resolution... along with every single one of the 80-100 extra files/links to other sites that are listed under an "A href" tag.

 

Now I've come up with two solutions but unfortunately, as I mentioned above. I don't know how to put my solution into the code above to make it work.

Solution 1) Only get the first 5 instances of "A href" on the page.

As mentioned above. I don't know how to do this.

Solution 2) Read the entire page, find "-Thumbnail.jpg" replace with ".jpg" and use the script as is.

I understand how to do a replace. All I am missing is how to do a replace within a field in the code of an IE page. I assume that I have to use the HTMLRead functions but how do I use/alter the data read?

I really hope all of this make sense and that someone here will be able to help me figure out a solution to my issue or at least answer one of my questions! I do have plenty more questions and I am sure that I'll have even more by the time I figure this out.

Thank you very much for your time!

Edited by AnAdventurer
Double checked a color difference between SciTE and the forum code.
Link to comment
Share on other sites

 

On 31/10/2016 at 6:59 AM, AnAdventurer said:

I figured out that I can change the .src to something like .href and it gets anything on the webpage with a .href tag

No. your code currently gets all the "img" tags and extracts the information from the ".src" attribute. Changing ".src" to ".href" would just give you the href attribute of the img tags. Based on your code you want the "a" tag, wrapped around your "img" tags? if so try my code below.

On 31/10/2016 at 6:59 AM, AnAdventurer said:

I still haven't been able to figure out what ".nameProp" is for or what it does. Is there any documentation/list of all the different ".PurpleTextAfterAVariable"

The "nameProp" property is acesseble only via the IE object you created in your code. IE documentation could point you in the right direction. Here is documentation for the "nameProp" property: nameProp property

 

The code below should be modified to work with your page.

#include <IE.au3>

$sImgDir = "c:\foo\"; Please make certain this folder already exists (silent failure if not)
$sWebPage = "http://www.autoitscript.com/forum/index.php?"; webpage with images

$oIE = _IECreate()
_IENavigate($oIE, $sWebPage)
$oIMGs = _IETagNameGetCollection($oIE.document, "img")

; Loop through all IMG tags and save file to local directory using INetGet
For $oIMG in $oIMGs
    $oA = $oIMG.parentNode
    If Not ($oA.localName == "a") Then ContinueLoop
    $sImgUrl = $oA.src
    $sImgFileName = $oA.nameProp
    INetGet($sImgUrl,  $sImgDir & $sImgFileName)
Next

 

Link to comment
Share on other sites

14 hours ago, genius257 said:

The code below should be modified to work with your page.

#include <IE.au3>

$sImgDir = "c:\foo\"; Please make certain this folder already exists (silent failure if not)
$sWebPage = "http://www.autoitscript.com/forum/index.php?"; webpage with images

$oIE = _IECreate()
_IENavigate($oIE, $sWebPage)
$oIMGs = _IETagNameGetCollection($oIE.document, "img")

; Loop through all IMG tags and save file to local directory using INetGet
For $oIMG in $oIMGs
    $oA = $oIMG.parentNode
    If Not ($oA.localName == "a") Then ContinueLoop
    $sImgUrl = $oA.src
    $sImgFileName = $oA.nameProp
    INetGet($sImgUrl,  $sImgDir & $sImgFileName)
Next

 

Thank you so much! I had to change some things around but it works now! I didn't even know you could use a parentnode! Is the msdn site you linked the best source for this type of thing?

Link to comment
Share on other sites

7 hours ago, AnAdventurer said:

Thank you so much! I had to change some things around but it works now!

Np :) Glad to hear it.

7 hours ago, AnAdventurer said:

Is the msdn site you linked the best source for this type of thing?

Yeah i would say so :)

Link to comment
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
 Share

×
×
  • Create New...