简体   繁体   English

lxml没有更新网页

[英]lxml not getting updated webpage

Simple script here, i'm just trying to get the number of people in a gym from a webpage every 15 minutes and save the result in a text file. 这里的脚本很简单,我只是想每15分钟从网页中获取健身房中的人数,并将结果保存在文本文件中。 However, the script is outputting the result from the first time I ran it (39), as opposed to the updated number of 93 (which can be seen by refreshing the webpage). 但是,脚本是从我第一次运行它时输出的结果(39),而不是更新后的数字93(可以通过刷新网页看到)。 Any ideas why this is? 任何想法为什么会这样? Note, I set the time to sleep to 10 seconds incase you want to run it yourself. 注意,如果您想自己运行,我将睡眠时间设置为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")

Cheers 干杯

You are not closing the people.txt file after each loop, it is better to use Python's with function to do this as follows: 您不必在每次循环后都关闭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))

If you want to keep a log of all entries, you would need to move the with statement outside your while loop. 如果要保留所有条目的日志,则需要将with语句移至while循环之外。 Also I think you meant while x == 'x' . 我也认为你的意思是while x == 'x' Currently the site is showing 39 , which is seen in the people.txt . 目前,该网站显示的是39 ,可在people.txt看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM