我正在编写这个 Python 小程序,它将在 www.fmylife.com 网站上获得新帖子,我希望这个程序能够打印这些新帖子。即使我刚刚开始,这也是一项简单的任务,但由于某种原因,我无法让代码停止打印同一篇文章。
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)
我的预期输出是获取帖子(我做了)并打印它。这很好用。唯一的问题是我只希望它打印一次帖子,当它定期在网站上检查时,它会打印多次,我不知道为什么。提前感谢大家!