Jump to content
Sign in to follow this  
RickB75

Any tips on making this script run faster?

Recommended Posts

RickB75

I'm using the IE udf and i really don't need to open IE to get the data I need. I could get the data with InetGetSource but what really helps me right now with my knowledge level is the IE UDF. I was just wondering if you guys had any pointers to make this script run faster. The biggest time consumer is waiting on IE to Load so I can get the full source of the page.

#include <Excel.au3>
ConsoleWrite('!--------- STARTING ----------' & @CRLF)
$icounter = 2 ;skip to the 2nd row in excel
Local $oExcel = _ExcelBookNew(1)
Local $header[6] = ["id ", "Title", "link", "image link", "Price", "Condition"];headers on excel spreadsheet
_ExcelWriteArray($oExcel, 1, 1, $header, 0);wirtes headers into excel


Local $oIE = _IECreate("http://www.beamantoyota.com/used-inventory/index.htm",0,0)


While 1

    Local $sHTML = _IEDocReadHTML($oIE)
    Local $oListItems = _IETagNameGetCollection($oIE, "LI")
    For $oListItem In $oListItems
        Local $odivs = _IETagNameGetCollection($oListItem, 'div')
        For $odiv In $odivs
            Local $dataVin = $odiv.getAttribute("data-vin")
            If $dataVin <> '' Then
                ;ConsoleWrite('+ Found it, data-vin: ' & $dataVin & @CRLF)
                _ExcelWriteCell($oExcel, $dataVin, $icounter, 1)
                Local $oAnchors = _IETagNameGetCollection($odiv, 'a')
                For $oAnchor In $oAnchors
                    Local $sHref = $oAnchor.href
                    ;ConsoleWrite(">    Link Info: " & $sHref & @CRLF)
                    _ExcelWriteCell($oExcel, $sHref, $icounter, 3)
                    ExitLoop
                Next

                Local $imgs = _IETagNameGetCollection($odiv, 'img')
                For $img In $imgs
                    Local $pic = $img.src
                    ;ConsoleWrite(">    Image Info: " & $pic & @CRLF)
                    _ExcelWriteCell($oExcel, $pic, $icounter, 4)
                    ExitLoop
                Next

                Local $titles = _IETagNameGetCollection($oListItem, 'a')

                For $title In $titles
                    Local $datatitle = $title.getAttribute("data-title")
                    If $datatitle <> '' Then
                        ;ConsoleWrite('> Found it, data-title: ' & $datatitle & @CRLF)
                        _ExcelWriteCell($oExcel, $datatitle, $icounter, 2)
                        ExitLoop
                    EndIf
                Next
                If $datatitle = '' Then
                    Local $htitles = _IETagNameGetCollection($oListItem, 'h1')
                    For $htitle In $htitles
                        ;ConsoleWrite('> Found it, Web-title: ' & $htitle.innerText & @CRLF)
                        _ExcelWriteCell($oExcel, $htitle.innerText, $icounter, 2)
                        ExitLoop

                    Next
                EndIf

                Local $oSPANs = _IETagNameGetCollection($oListItem, "SPAN") ; Find all SPAN tags
                For $oSPAN In $oSPANs


                        If StringInStr($oSPAN.className, "msrp final-price") Then
                            If StringInStr($oSPAN.innertext,"$") Then
                                $price = StringSplit($oSPAN.innertext,"$")
                                _ExcelWriteCell($oExcel, $price[2], $icounter, 5)
                            Else
                            ;ConsoleWrite('> Found it, MSRP: ' & $oSPAN.innertext& @CRLF)
                            _ExcelWriteCell($oExcel, $oSPAN.innertext, $icounter, 5)
                            Endif
                        ElseIf StringInStr($oSPAN.className, "internetPrice final-price")Then
                            If StringInStr($oSPAN.innertext,"$") Then
                                $price = StringSplit($oSPAN.innertext,"$")
                                _ExcelWriteCell($oExcel, $price[2], $icounter, 5)
                            Else
                            ;MsgBox(0,"price",$price[2])
                            ;ConsoleWrite('> Found it, Internet Price: ' & $oSPAN.innertext& @CRLF)
                            _ExcelWriteCell($oExcel, $oSPAN.innertext, $icounter, 5)
                            EndIf
                        ElseIf StringInStr($oSPAN.className, "retailValue final-price")Then
                            If StringInStr($oSPAN.innertext,"$") Then
                                $price = StringSplit($oSPAN.innertext,"$")
                                _ExcelWriteCell($oExcel, $price[2], $icounter, 5)
                            Else
                            ;MsgBox(0,"price",$price[2])
                            ;ConsoleWrite('> Found it, Internet Price: ' & $oSPAN.innertext & @CRLF)
                            _ExcelWriteCell($oExcel, $oSPAN.innertext, $icounter, 5)
                            EndIf
                        EndIf

                Next
                _ExcelWriteCell($oExcel,"Used", $icounter, 6)
            $icounter = $icounter + 1
            EndIf



        Next


    Next
    $aPage = StringRegExp($sHTML, "Page (\d*) of (\d*)", 1)
    ;_ArrayDisplay($aPage)
    If $aPage[0] = $aPage[1] Then ExitLoop
    Local $oLinks = _IELinkGetCollection($oIE)
    $sMyString = "Next"
    For $oLink In $oLinks
        Local $sLinkText = _IEPropertyGet($oLink, "innerText")
        If StringInStr($sLinkText, $sMyString) Then
            _IENavigate($oIE, $oLink.href)
            ;_IEAction($oLink, "click")
            ExitLoop
        EndIf
    Next

WEnd
ConsoleWrite('!--------- ENDING ----------' & @CRLF)
_IEQuit($oIE)

Share this post


Link to post
Share on other sites
mLipok

first collect all the data

write to excel at once at the end of script

 

EDIT:
 

what if there is no "NEXT" link, script not Exits from 

For $oLink In $oLinks
        Local $sLinkText = _IEPropertyGet($oLink, "innerText")
        If StringInStr($sLinkText, $sMyString) Then
            _IENavigate($oIE, $oLink.href)
            ;_IEAction($oLink, "click")
            ExitLoop
        EndIf
    Next

and doing again the same ?

Edited by mlipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library
 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-10-15

Share this post


Link to post
Share on other sites
RickB75

I thought about sending the data to an array and writing everything to excel at the end but my biggest time consumer is waiting on ie to load the next page. Has anyone built a scraper lately using inetgetsource? How complicated was the script? I would really love to gather all the data first and save it to an array and write everything to excel last. I'm sure if I use inetgetsource the script would be more complex with regex's. inetgetsource works so much faster at gettin the source.

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  

×