【发布时间】:2017-10-15 22:49:13
【问题描述】:
用户可能会给出一堆 url 作为命令行参数。过去给出的所有 URL 都使用 pickle 进行序列化。该脚本检查所有给定的 URL,如果它们是唯一的,则将它们序列化并附加到文件中。至少这是应该发生的事情。没有附加任何内容。但是,当我以写入模式打开文件时,会写入新的唯一 URL。那么给了什么?代码是:
def get_new_urls():
if(len(urls.URLs) != 0): # check if empty
with open(urlFile, 'rb') as f:
try:
cereal = pickle.load(f)
print(cereal)
toDump = []
for arg in urls.URLs:
if (arg in cereal):
print("Duplicate URL {0} given, ignoring it.".format(arg))
else:
toDump.append(arg)
except Exception as e:
print("Holy bleep something went wrong: {0}".format(e))
return(toDump)
urlsToDump = get_new_urls()
print(urlsToDump)
# TODO: append new URLs
if(urlsToDump):
with open(urlFile, 'ab') as f:
pickle.dump(urlsToDump, f)
# TODO check HTML of each page against the serialized copy
with open(urlFile, 'rb') as f:
try:
cereal = pickle.load(f)
print(cereal)
except EOFError: # your URL file is empty, bruh
pass
【问题讨论】:
-
虽然创意很好,但请记住,这是一个适合儿童的网站;-(
-
"ain't dumpin' no thing" 只是错误