bgigurtsis
bgigurtsis

Reputation: 131

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. 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). Any ideas why this is? Note, I set the time to sleep to 10 seconds incase you want to run it yourself.

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

Upvotes: 1

Views: 69

Answers (1)

Martin Evans
Martin Evans

Reputation: 46769

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:

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. Also I think you meant while x == 'x'. Currently the site is showing 39, which is seen in the people.txt.

Upvotes: 1

Related Questions