Jump to content

Read\Write from txt Files(s) issue


Recommended Posts

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 post
Share on other sites
Posted (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 by Nine
Link to post
Share on other sites
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 post
Share on other sites
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 post
Share on other sites
Posted (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 by Nine
Link to post
Share on other sites
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...