lxml 未获得更新的网页



简单的脚本在这里,我只是想每 15 分钟从网页上获取健身房的人数,并将结果保存在文本文件中。但是,脚本输出的是我第一次运行它时的结果 (39),而不是更新的数字 93(可以通过刷新网页看到)。知道为什么会这样吗?请注意,我将睡眠时间设置为 10 秒,以防您想自己运行它。

from lxml import html
import time
import requests
x = 'x'
while x == x: 

    time.sleep(10)
    page = requests.get('http://www.puregym.com/gyms/holborn/whats-happening')
    string = html.fromstring(page.content)
    people = string.xpath('normalize-space(//span[@class="people-number"]/text()[last()])')
    print people
    #printing it for debug purposes
    f = open("people.txt","w")
    f.write(people)
    f.write("n")

干杯

你不会在每次循环后关闭people.txt文件,最好使用 Python 的 with 函数来执行此操作,如下所示:

from lxml import html
import time
import requests
x = 'x'
while x == 'x': 
    time.sleep(10)
    page = requests.get('http://www.puregym.com/gyms/holborn/whats-happening')
    string = html.fromstring(page.content)
    people = string.xpath('normalize-space(//span[@class="people-number"]/text()[last()])')
    print people
    #printing it for debug purposes
    with open("people.txt", "w") as f:
        f.write('{}n'.format(people))

如果要保留所有条目的日志,则需要将 with 语句移到 while 循环之外。我也认为你的意思是while x == 'x'.目前该网站正在显示39,这在people.txt中看到。

最新更新