- Retrieve current temperature using BeautifulSoup
#extract current temp import requests import bs4 #url="https://weather.com/en-IN/weather/today/l/12.96,77.59?par=google&temp=c" url="https://weather.com/en-IN/weather/today/l/17.31,76.81?par=google&temp=c" r=requests.get(url) soup=bs4.BeautifulSoup(r.text, "html.parser") val= soup.find('span', "data-testid='TemperatureValue'", class_='CurrentConditions--tempValue--3KcTQ') print("Current temp is : ", val.text)
Output of the above program is :
Current temp is : 30°
2. To check live price, title and ratings of particular product
#data science with Python - extracting TITLE and Price of product from Flipkart import bs4 import pandas as pd import requests url = 'https://www.flipkart.com/motorola-one-fusion-twilight-blue-128-gb/p/itm9c0e4b9b56acd?pid=MOBFRFXHZRMXDDNZ&lid=LSTMOBFRFXHZRMXDDNZW6P8X2&marketplace=FLIPKART&srno=s_1_1&otracker=search&otracker1=search&fm=SEARCH&iid=9a111e82-0d0e-49e1-a1c5-ea4033395054.MOBFRFXHZRMXDDNZ.SEARCH&ppt=sp&ppn=sp&ssid=s7stcbm6g00000001605944328129&qH=fc6b46fe888f1cbe' result = requests.get(url) soup = bs4.BeautifulSoup(result.content,'html.parser') #print(soup.title.text) title=soup.find_all('h1', class_='yhB1nd') price = soup.find_all('div', {"class":"_30jeq3 _16Jk6d"}) rvw= soup.find_all('div', {"class":"row _3AjFsn _2c2kV-"}) for item in title: print("Title:", item.text) for pr in price: print("Price:", pr.text) for rv in rvw: print("Reviews:", rv.text)
Output of the above program is :
3. To retrieve live stock price of any company [exa: infosys]
#data science with Python - extracting share price - BSE / NSE of Infosys import bs4 import pandas as pd import requests #url = 'https://www.moneycontrol.com/india/stockpricequote/auto-23-wheelers/heromotocorp/HHM' url = 'https://www.moneycontrol.com/india/stockpricequote/computers-software/infosys/IT' result = requests.get(url) soup = bs4.BeautifulSoup(result.content,'html.parser') #print(soup.title.text) shareD = soup.find_all('div' ,class_= 'pcnsb div_live_price_wrap') data = [] for i in shareD: span= i.find('span') data.append(span.string) #to remove duplicate mylist = list(dict.fromkeys(data)) print("Current market price of Infosys:",mylist) from tkinter import * root = Tk() t='Current market price of Infosys :'+str(mylist) w = Label(root, text=t, bg='gold', font='sans 14') w.pack() root.mainloop()
Output of the above example is: