我正在尝试获取我网站的 html,看看它是否与我在离线版本上的相同。
我一直在研究这个,我能找到的只是解析或者只处理 http://
到目前为止我有这个:
import urllib
url = "https://www.mywebsite.com/"
onlinepage = urllib.urlopen(url)
print(onlinepage.read())
offlinepage = open("offline.txt", "w+")
print(offlinepage.read())
if onlinepage.read() == offlinepage.read():
print("same") # for debugging
else:
print("different")
这总是说它们是相同的,即使我放入完全不同的网站也是如此。
请您参考如下方法:
当您第一次使用这些行打印在线和离线页面时:
print(onlinepage.read())
print(offlinepage.read())
...您现在已经使用了每个文件对象中的所有文本。对任一对象的后续读取都将返回一个空字符串。两个空字符串相等,因此您的 if 条件将始终评估为 True。
如果您纯粹使用文件,您可以 seek到两个文件的开头并再次阅读。由于 urlopen 中的文件对象没有 seek 方法,您需要使用新的 urlopen 命令重新获取页面或者,更好的是,将原始文本保存在一个变量中,并将其用于后续比较:
online = onlinepage.read()
print(online)
offline = offlinepage.read()
print(offline)
...
if online == offline:
...
