import requests
from bs4 import BeautifulSoup
import time
# URL of the website to monitor
url = 'https://nbeub.ca/index.php?page=current-petroleum-prices-2'
# Function to fetch the number from the website
def fetch_number():
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Adjust the selector to find the specific number
number = soup.select_one('body > table > tbody > tr:nth-child(5) > td > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(3) > td:nth-child(2)')
return str(number)
# Main monitoring function
def monitor():
last_number = fetch_number()
print(f"Initial number: {last_number}")
while True:
time.sleep(2592000) # Wait for 30 days before checking again
current_number = fetch_number()
if current_number != last_number:
print(f"Number updated: {current_number}")
last_number = current_number
# Start monitoring
monitor()
我的终端是正常的要求我提供输入
大多数桌子都没有<tbody>
<table>
中。而不是使用所有这些直接子选择器,而是使用没有
> tbody >
的后代选择器,因此它都可以匹配两种方式。
number = soup.select_one('body > table tr:nth-child(5) > td > table tr > td > table tr > td:nth-child(3) > table tr:nth-child(3) > td:nth-child(2)')