lxml没有获得更新的网页

时间:2016-01-27 06:28:04

标签: python python-2.7 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")

干杯

1 个答案:

答案 0 :(得分: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))

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