lxml 未更新网页

问题描述 投票:0回答:1

这里是简单的脚本,我只是想每 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")

干杯

python python-2.7 lxml
1个回答
1
投票

您不会在每次循环后关闭

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
中看到。

© www.soinside.com 2019 - 2024. All rights reserved.