为什么我的HTML解析器未输出想要的数字

问题描述 投票:0回答:1
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>

元素,
python beautifulsoup css-selectors html-parsing
1个回答
0
投票
直接嵌套在

<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)')
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.