Beautiful Soup 从每天更新的页面中获取价值

Beautiful Soup get value from page which updates daily

我正在尝试从每天更新的网站获取单个值。

我正在尝试在页面中获取维杰亚瓦达的最新价格

下面是我的代码,但我得到空 space 作为输出,但期望 440 作为输出。

import requests
from bs4 import BeautifulSoup
import csv
res = requests.get('https://www.e2necc.com/home/eggprice')
soup = BeautifulSoup(res.content, 'html.parser')
price = soup.select("Vijayawada")

希望获得价值:440 [今天的价值]有什么建议吗?

一种方法是 select tr 的文本并遍历其字符串以选择最后一个 isdigit():

[x for x in soup.select_one('tr:-soup-contains("Vijayawada")').stripped_strings if x.isdigit()][-1]
例子
import requests
from bs4 import BeautifulSoup

res = requests.get('https://www.e2necc.com/home/eggprice')
soup = BeautifulSoup(res.content, 'html.parser')
price = [x for x in soup.select_one('tr:-soup-contains("Vijayawada")').stripped_strings if x.isdigit()][-1]

print(price)

另一种是按天选元素:

import requests
from bs4 import BeautifulSoup
import datetime

d = datetime.datetime.now().strftime("%d")

res = requests.get('https://www.e2necc.com/home/eggprice')
soup = BeautifulSoup(res.content, 'html.parser')
soup.select('tr:-soup-contains("Vijayawada") td')[int(d)].text
输出
440