Home > database >  Beautiful Soup get value from page which updates daily
Beautiful Soup get value from page which updates daily

Time:05-25

I am trying to get a single value from website which will update daily.

I am trying to get latest price in Vijayawada in page

below is my code but I am getting empty space as output but expecting 440 as output.

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

Looking to get value: 440 [Which is today value] any suggestions?

CodePudding user response:

To get the value 440, You can follow the next example

import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.e2necc.com/home/eggprice')
soup = BeautifulSoup(res.content, 'html.parser')
price = soup.select_one('td:-soup-contains("Vijayawada")').parent
print(price.find_all('td')[24].text)

Output:

440

CodePudding user response:

One approach could be to select the tr by its text and iterate over its strings to pick the last one that isdigit():

[x for x in soup.select_one('tr:-soup-contains("Vijayawada")').stripped_strings if x.isdigit()][-1]
Example
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)

Another one is to pick the element by day:

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
Output
440
  • Related