I am writing this little python program that will get new posts on the website http://www.fmylife.com and I want this program to print these new posts. An easy task even though I just started, but for some reason I cannot get the code to stop printing the same post.
import urllib.request,time
def getFml():
rawfml = urllib.request.urlopen("http://www.fmylife.com")
refml = rawfml.read()
fml = refml.decode("utf8")
rawfml.close()
return fml
def parseFml(fml):
fml=fml.split('Today,')
fml=fml[1]
for char in fml:
if char in '0123456789<>/="#()-;:"\',':
fml=fml.replace(char,'')
fml=fml.split('FMLapdiv')
fml=fml[0]
fml=fml.split('aa')
fml=''.join(fml)
return fml
listy= ['date','left_parta','votej','idvotea','onclickvote','jTipa','comments','right_partp','fmllink','clear','post','hrefwork','class','dyn','javascript','div','classright_partp','hrefmiscellaneous','classdyncomments''div','article','idpa','classclear','classpost','hreflove','classfmlling','FMLap','classdate','classleft_parta','id_','nameresume','classjTipa','span','classdyn-commentsspan','classright_partpspan','classdyn-vote-j','idcotea','hrefjavascript;:','classfmllink','href']
for x in range(len(listy)):
fml=fml.replace(listy[x],'')
return fml
oldfml=''
count=0
while True:
fml=getFml()
fml=parseFml(fml)
count=count+1
if count>1:
oldfml=fml
if oldfml == fml:
time.sleep(300)
else:
print('Today,'+fml)
time.sleep(300)
My expected output was to get the post (which I did) and to print it. That works fine. The only problem is that I only want it to print the post once, and when it regularly checks up on the website it is getting printed multiple times and I do not know why.
Thanks To everyone in advance!
In this part of your code:
You assign
oldfml=fmlbefore you check their equality. Thus, they are always equal.I suggest the following fix:
This way,
oldfmlwill actually be old.