[英]lxml not getting updated webpage
這里的腳本很簡單,我只是想每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
看到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.