- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
基本上,如果网站上的值发生变化,我会尝试运行一些代码 (Python 3.2),否则稍等片刻,稍后再检查。
首先,我认为我可以将值保存在一个变量中,并将其与下次脚本运行时获取的新值进行比较。但这很快就遇到了问题,因为当脚本再次运行并初始化该变量时,该值被覆盖了。
然后我尝试将网页的 html 保存为文件,然后将其与下次脚本运行时调用的 html 进行比较。那里也不走运,因为即使没有任何变化,它也会不断出现 False。
接下来是 pickle 网页,然后尝试将其与 html 进行比较。有趣的是,这在脚本中也不起作用。但是,如果我在脚本运行后键入 file = pickle.load( open( 'D:\Download\htmlString.p', 'rb')) 然后 file == html,当没有任何变化。
我有点困惑为什么它在脚本运行时不起作用,但如果我执行上述操作,它会显示正确答案。
编辑:感谢大家到目前为止的回复。我的问题并不是关于其他方法来解决这个问题(尽管学习更多方法来完成任务总是好的!)而是为什么下面的代码在作为脚本运行时不起作用,但是如果我脚本运行后在提示符处重新加载 pickle 对象,然后根据 html 对其进行测试,如果没有任何更改,它将返回 True。
try:
file = pickle.load( open( 'D:\\Download\\htmlString.p', 'rb'))
if pickle.load( open( 'D:\\Download\\htmlString.p', 'rb')) == htmlString:
print("Values haven't changed!")
sys.exit(0)
else:
pickle.dump( htmlString, open( 'D:\\Download\\htmlString.p', "wb" ) )
print('Saving')
except:
pickle.dump( htmlString, open( 'D:\\Download\\htmlString.p', "wb" ) )
print('ERROR')
最佳答案
编辑:我没有意识到您只是在寻找脚本的问题。这是我认为的问题所在,然后是我的原始答案,它解决了您要解决的更大问题的另一种方法。
您的脚本是使用一揽子 except
语句的危险的一个很好的例子:您捕获了一切。在这种情况下,包括您的 sys.exit(0)
。
我假设您正在使用 try
block 来捕获 D:\Download\htmlString.p
尚不存在的情况。该错误称为 IOError
,您可以使用 except IOError:
这是您的脚本加上一些代码,以解决您的 except
问题:
import sys
import pickle
import urllib2
request = urllib2.Request('http://www.iana.org/domains/example/')
response = urllib2.urlopen(request) # Make the request
htmlString = response.read()
try:
file = pickle.load( open( 'D:\\Download\\htmlString.p', 'rb'))
if file == htmlString:
print("Values haven't changed!")
sys.exit(0)
else:
pickle.dump( htmlString, open( 'D:\\Download\\htmlString.p', "wb" ) )
print('Saving')
except IOError:
pickle.dump( htmlString, open( 'D:\\Download\\htmlString.p', "wb" ) )
print('Created new file.')
作为旁注,您可以考虑使用 os.path
对于您的文件路径——它会帮助以后想在另一个平台上使用您的脚本的任何人,并且它会为您省去丑陋的双反斜杠。
编辑 2:适用于您的特定网址。
该页面上的广告有一个动态生成的编号,该编号会随着每次页面加载而变化。它在所有内容之后接近尾声,因此我们可以在该点拆分 HTML 字符串并获取前半部分,丢弃带有动态数字的部分。
import sys
import pickle
import urllib2
request = urllib2.Request('http://ecal.forexpros.com/e_cal.php?duration=weekly')
response = urllib2.urlopen(request) # Make the request
# Grab everything before the dynabic double-click link
htmlString = response.read().split('<iframe src="http://fls.doubleclick')[0]
try:
file = pickle.load( open( 'D:\\Download\\htmlString.p', 'r'))
if pickle.load( open( 'D:\\Download\\htmlString.p', 'r')) == htmlString:
print("Values haven't changed!")
sys.exit(0)
else:
pickle.dump( htmlString, open( 'D:\\Download\\htmlString.p', "w" ) )
print('Saving')
except IOError:
pickle.dump( htmlString, open( 'D:\\Download\\htmlString.p', "w" ) )
print('Created new file.')
如果这很重要,您的字符串不再是有效的 HTML 文档。如果是,您可能只是删除该行或其他内容。可能有一种更优雅的方法来执行此操作——也许使用正则表达式删除数字——但这至少可以满足你的问题。
原始答案 -- 解决您问题的另一种方法。
Web 服务器的响应 header 是什么样的? HTTP 指定一个 Last-Modified
可用于检查内容是否已更改的属性(假设服务器说的是实话)。如 Uku 在他的回答中所示,将此请求与 HEAD
请求一起使用。如果您想节省带宽并对轮询的服务器友善。
还有一个 If-Modified-Since
听起来像您可能正在寻找的标题。
如果我们将它们结合起来,您可能会想出这样的结果:
import sys
import os.path
import urllib2
url = 'http://www.iana.org/domains/example/'
saved_time_file = 'last time check.txt'
request = urllib2.Request(url)
if os.path.exists(saved_time_file):
""" If we've previously stored a time, get it and add it to the request"""
last_time = open(saved_time_file, 'r').read()
request.add_header("If-Modified-Since", last_time)
try:
response = urllib2.urlopen(request) # Make the request
except urllib2.HTTPError, err:
if err.code == 304:
print "Nothing new."
sys.exit(0)
raise # some other http error (like 404 not found etc); re-raise it.
last_modified = response.info().get('Last-Modified', False)
if last_modified:
open(saved_time_file, 'w').write(last_modified)
else:
print("Server did not provide a last-modified property. Continuing...")
"""
Alternately, you could save the current time in HTTP-date format here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
This might work for some servers that don't provide Last-Modified, but do
respect If-Modified-Since.
"""
"""
You should get here if the server won't confirm the content is old.
Hopefully, that means it's new.
HTML should be in response.read().
"""
还有 check out this blog post由 Stii 提供,可能会提供一些灵感。我对 ETags
了解不够,无法将它们放在我的示例中,但他的代码也会检查它们。
关于python - 如何检查网站上的值是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11252576/
我有这个网站,这个特定页面是安全的,但是当它回发时,它回发到一个不安全的页面。如何解决? 我正在使用 ASP.NET 向导。我有这个页面 - checkout.aspx,页面包含这个控件 checko
我有 2 个 azure 网站 - 两个独立的项目 我现在有 2 个网址: myazurewebsite.azurewebsites.net myazureblog.azurewebsites.net
我有 2 个 azure 网站 - 两个独立的项目 我现在有 2 个网址: myazurewebsite.azurewebsites.net myazureblog.azurewebsites.net
环境: 旧网站: React 托管在 Heroku URL( http://sameurl.com ) 新网站: Java 托管在 Heroku URL ( http://sameurl.com )
我已在 Windows Azure 上注册了一个测试帐户来对其进行测试。我构建了一个 Hello world ASP.NET Web 应用程序 + 数据库只是为了测试。 我安装了 Visual Stu
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
这个问题在这里已经有了答案: Software keyboard resizes background image on Android (16 个答案) 关闭 8 年前。 我有一个类似的问题:So
这个问题似乎很常见,但我真的无法根据现有答案解决问题。 我有一个简单的 maven 项目,没有任何复杂的部署配置等,并且想在点击“mvn site”时生成一个 Maven CheckStyle 报告。
有没有人看过有关何时进行横向扩展与纵向扩展的最佳选择的任何分析或信息。什么时候一个比另一个更有意义。 目前,在标准模式和基本模式下,2 个小型实例的费用与 1 个中型实例的费用相同。 拥有 2 个小型
有没有办法找到 azure 网站何时停止? (我通过门户网站停止了网站,但我不记得是什么时候......) 我正在寻找一些日志,但没有找到任何有用的内容。 谢谢。 最佳答案 您拥有的最接近的是 azu
我目前在 Azure VM 的 IIS 中拥有一个网站。我已将该站点复制到 2 个可用区域中的 2 个虚拟机上。 这可以保护网站免遭停机。 我需要为高负载时刻实现一些可扩展性。这似乎就是创建音阶集的目
我有一个托管在 Azure 上的网站 ( http://mike-ward.azurewebsites.net/ )。我从 Azure 门户设置了一个指向(引用?)我的网站的 Azure CDN。根据
我有一个 Azure 网站(不是 Web 角色),有 2 个槽:生产和暂存。 我只想为生产插槽启用 CDN,而不是为登台启用,问题是我找不到识别主机插槽的方法。 RoleEnvironment 不可用
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等建议的问题。您可以编辑问题,以便可以用事实和引用来回答它。 4年前关
我们正在考虑将一些网站从 Azure 云服务迁移到 Azure 网站(事情似乎就是这样进行的)。显然,我们被明确告知云服务不会保留文件系统状态,因为它们会在机器故障时重新部署。 我假设网站是基于 Bl
我有一个 Azure 网站,需要使用在 VM 上运行的 Elasticsearch 服务。 虽然我需要能够锁定对 Elasticsearch 的访问,以便只有 Azure 网站可以访问它,但我似乎无法
我有一个 azure 网站,位于 yis3.azurewebsites.net - 我已将其提升为“共享”网站,以便我可以使用自定义域。我拥有从 123-reg.co.uk 购买的域名 yorkshi
我正在使用 abcPDF 动态创建 PDF。 我想保存这些 PDF,以便客户随时检索。最简单的方法(也是我现在在当前服务器上所做的方法)是将完成的 PDF 保存到文件系统。 看来我一直坚持使用 blo
我们正在尝试了解 Windows Azure 管理 API 为 Azure 网站(而非 Webroles)返回的监控数据的复杂性 例如,下图描述了为 CPUTime 检索的数据点。它似乎表明,在晚上
看起来真的很愚蠢,因为我找不到它: 门户网站似乎不太直观,我如何为一个“网站”付费并在其中运行最多 500 个网站?我想当我通过单击左下角的加号添加“网站”时,我添加了整个虚拟机而不是子站点。如何仅添
我是一名优秀的程序员,十分优秀!