Tim93 Posted May 10, 2021 Share Posted May 10, 2021 Hi Guys! I trying to write script, which helps me with web-scrapping. What should it do: 1) Get URL from line 1...n from file source.txt 2) Get elelement URL and write it to file imglinks.txt 3) Loop scrapping (while lines in source.txt proceed) Issue: I can't add FileReadLine to my script and loop it When i tried add it - script return 0. #Include <String.au3> #include <INet.au3> #include <File.au3> #include <Array.au3> $Img_file = @ScriptDir & "\imglinks.txt" $s_URL = "https://mywebsite.com/abc123" $source = _INetGetSource ($s_URL) $url = _StringBetween($source, '<img class="screenshot-image" src="', '" crossorigin="') FileWriteLine($Img_file, $url[0]) FileClose($Img_file) ;MsgBox(0, "out", $url[0]) I'd really appreciate, if someone can help me. Thank you! Link to comment Share on other sites More sharing options...
Nine Posted May 10, 2021 Share Posted May 10, 2021 Look in help file for FileReadToArray function. Then loop thru the array as shown in the example. Tim93 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Nine Posted May 10, 2021 Share Posted May 10, 2021 (edited) Just noticed your FileClose is wrong. As described in the help file, you need to provide the file handle (obtain thru FileOpen) to close properly a file. In your case, you do not need to use file handle since you are writing the file with its file name. That means you do not need the FileClose (just remove that line). FileOpen / FileClose are useful when reading/writing a large amount of data and performance becomes an issue. Otherwise, using file name is quite acceptable. Edited May 10, 2021 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Tim93 Posted May 10, 2021 Author Share Posted May 10, 2021 12 minutes ago, Nine said: Look in help file for FileReadToArray function. Then loop thru the array as shown in the example. Loop now working, but script return 0 instead URL links #Include <String.au3> #include <INet.au3> #include <File.au3> #include <Array.au3> $Img_file = @ScriptDir & "\imglinks.txt" ;файл куда выгружать готовые ссылки $s_URL = @ScriptDir & "\source.txt" Get() Func Get() Local $aArray = FileReadToArray(@ScriptDir & "\source.txt") Local $iLineCount = @extended If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) Else For $i = 0 To $iLineCount - 1 $source = _INetGetSource ($s_URL) $url = _StringBetween($aArray, '<img class="no-click screenshot-image" src="', '" crossorigin="anonymous"') FileWriteLine($Img_file, $url) ;записать новую строку FileClose($Img_file) ;MsgBox(0, "out", $url[0]) Next EndIf EndFunc Link to comment Share on other sites More sharing options...
Tim93 Posted May 10, 2021 Author Share Posted May 10, 2021 7 minutes ago, Nine said: Just noticed your FileClose is wrong. As described in the help file, you need to provide the file handle (obtain thru FileOpen) to close properly a file. In your case, you do not need to use file handle since you are writing the file with its file name. That means you do not need the FileClose (just remove that line). FileOpen / FileClose are useful when reading/writing a large amount of data and performance becomes an issue. Otherwise, using file name is quite acceptable. Ok, thanks. Copy that Link to comment Share on other sites More sharing options...
Nine Posted May 10, 2021 Share Posted May 10, 2021 (edited) _StringBetween returns an array. Read carefully in help file how that function is working. You will need to provide the index of the array cell you want to save (e.g. $url[1]). In fact all your usages of array are wrong. You will need to understand how to use array properly.... Edited May 10, 2021 by Nine Tim93 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Tim93 Posted May 10, 2021 Author Share Posted May 10, 2021 23 minutes ago, Nine said: _StringBetween returns an array. Read carefully in help file how that function is working. You will need to provide the index of the array cell you want to save (e.g. $url[1]). In fact all your usages of array are wrong. You will need to understand how to use array properly.... Thanks mate. This part of code works fine Spoiler #Include <String.au3> #include <INet.au3> #include <File.au3> #include <Array.au3> $Img_file = @ScriptDir & "\imglinks.txt" $s_URL = "https://abc.com/124abc" ; <---- list of links should load from txt file $source = _INetGetSource ($s_URL) $url = _StringBetween($source, '<img class="no-click screenshot-image" src="', '" crossorigin="anonymous"') FileWriteLine($Img_file, $url[0]) When I Added FileReadToArray - error in MsgBox(0, "out", $url[0]) Code with mistakes : #Include <String.au3> #include <INet.au3> #include <File.au3> #include <Array.au3> $Img_file = @ScriptDir & "\imglinks.txt" get() Func get() Local $aArray = FileReadToArray(@ScriptDir & "\source.txt") Local $iLineCount = @extended If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) Else For $i = 0 To $iLineCount - 1 local $source = _INetGetSource ($aArray) local $url = _StringBetween($source, '<img class="no-click screenshot-image" src="', '" crossorigin="anonymous"') ;FileWriteLine($Img_file, $url[0]) MsgBox(0, "out", $url[0]) Next EndIf EndFunc Link to comment Share on other sites More sharing options...
Tim93 Posted May 10, 2021 Author Share Posted May 10, 2021 I found mistake. Thank you for help! Now it works 👍 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now