SRL Archive - OSR Help

It's not writing to file


stata

2013-10-04 04:31:09

Hey guys, just wondering why my code won't write to a text file. The text file is just empty, despite that the strings I'm writing to it are definitely not empty. Help would really be appreciated

P.S. code won't run since I'm not including the site url I'm scraping and filepath


program SiteScraper;
{$i SRL/SRL.simba}
{$loadlib Appa_Simba}
var
myUrl,currentStr,startStr,endStr: String;
user, i, bI, startIndex, endIndex, numEle,fileHandle: Integer;

e: TAppaElementArray;
const
URLNAME = 'url';
FILEPREF = 'C:\Users\myname\Desktop\filename';
WAITTIME = 500;

GOLDOFF = 12;

ERRORINDEX = 66;
DEL = ';';


begin

InputQuery('scraper', 'Start ID:' ,startStr);
startIndex := StrToInt(startStr);
InputQuery('scraper', 'End ID:' ,endStr);
endIndex := StrToInt(endStr);
fileHandle := CreateFile(FILEPREF+startStr+'to'+endStr+'.txt');
if fileHandle = -1 then exit;

appa_ShowForm;
wait(1000);

for user := startIndex to endIndex do
begin
myUrl := URLNAME + IntToStr(user);
appa_Navigate(myUrl);
wait(WAITTIME);

try
appa_GetElements(e);
numEle := length(e);
if (numEle > ERRORINDEX-1) and (e[ERRORINDEX].innerText = 'Error') then
begin
writeln('no user');
continue;
end;

for i := length(e) - 1 downto 0 do
begin
{writeln(e[i].innerText + '----' + IntToStr(i));}
if (e[i].innerText <> 'Group:') then continue;
bI := i;
break;
end;

// writeln(IntToStr(i));
currentStr := (IntToStr(user)+DEL+
e[bI+GOLDOFF].innerText+DEL+#13#10);

WriteFileString(fileHandle,currentStr);
writeln(currentStr);


except
WriteFileString(fileHandle,IntToStr(user)+DEL+'fail'+#13#10);
writeln('Failed navigating to: ' + myURL);
{
finally
writeln('Success');
}
end;
end;

writeln('Done');

end.



tealc

2013-10-04 05:45:17

From: stata
Hey guys, just wondering why my code won't write to a text file. The text file is just empty, despite that the strings I'm writing to it are definitely not empty. Help would really be appreciated

P.S. code won't run since I'm not including the site url I'm scraping and filepath



Try this


program SiteScraper;
{$i SRL/SRL.simba}
{$loadlib Appa_Simba}
var
myUrl,currentStr,startStr,endStr: String;
user, i, bI, startIndex, endIndex, numEle,fileHandle: Integer;

e: TAppaElementArray;
const
URLNAME = 'url';
FILEPREF = 'C:\Users\myname\Desktop\filename';
WAITTIME = 500;

GOLDOFF = 12;

ERRORINDEX = 66;
DEL = ';';


begin

InputQuery('scraper', 'Start ID:' ,startStr);
startIndex := StrToInt(startStr);
InputQuery('scraper', 'End ID:' ,endStr);
endIndex := StrToInt(endStr); //maybe add if fileExists skip createFile
fileHandle := CreateFile(FILEPREF+startStr+'to'+endStr+'.txt');
if fileHandle = -1 then exit;
CloseFile(fileHandle); //added
fileHandle:=AppendFile(FILEPREF+startStr+'to'+endStr+'.txt'); //edit fixed

appa_ShowForm;
wait(1000);

for user := startIndex to endIndex do
begin
myUrl := URLNAME + IntToStr(user);
appa_Navigate(myUrl);
wait(WAITTIME);

try
appa_GetElements(e);
numEle := length(e);
if (numEle > ERRORINDEX-1) and (e[ERRORINDEX].innerText = 'Error') then
begin
writeln('no user');
continue;
end;

for i := length(e) - 1 downto 0 do
begin
{writeln(e[i].innerText + '----' + IntToStr(i));}
if (e[i].innerText <> 'Group:') then continue;
bI := i;
break;
end;

// writeln(IntToStr(i));
currentStr := (IntToStr(user)+DEL+
e[bI+GOLDOFF].innerText+DEL+#13#10);

WriteFileString(fileHandle,currentStr);
writeln(currentStr);


except
WriteFileString(fileHandle,IntToStr(user)+DEL+'fail'+#13#10);
writeln('Failed navigating to: ' + myURL);
{
finally
writeln('Success');
}
end;
end;
CloseFile(fileHandle); //added
writeln('Done');

end.



bg5

2013-10-05 14:51:02

It's weird,but when you create a file ,you need to close it, and open for writing again.

program new;

const
FILEPREF = 'C:\Users\user\Desktop\file';
var f : integer;
begin
f := CreateFile(FILEPREF+'.txt');
CloseFile(f);
f := AppendFile(FILEPREF+'.txt');
WriteFileString(f,'abc');
CloseFile(f);
end.

So, like tealc said, but AppendFile() instead of OpenFile()( because Openfile is for reading)

stata

2013-10-09 20:54:12

Thanks both of you!

I actually fixed this a while ago but haven't logged in to say so, sorry bout that. I did get it to work, but it was too slow for my liking. Ended up writing a python scraper using scrapy, was actually 1000x faster (ok, maybe 500x?), in case anyone was thinking of doing this as well.

I also ended up getting banned from the site lol…