0

I wanted to learn, how to fetch any kind of integer value from any website (with a particular given link). suppose if I search by today's temperature in google, it shows like this:

enter image description here

I prefer to use Django. How can I fetch the value of temperature from this page? I mean what should be the views.py look like for this? I am totally new to this type of data fetching, having zero knowledge of this, please suggest to me some tutorial link or blog if you prefer.

Kanchon Gharami
  • 777
  • 1
  • 11
  • 30
  • 1
    You will need to use web-scraping for this, and as google is a dynamic website (loads its content using javascript), you will need to use selenium. Or better, you can try to find some API. A quick google search shows [OpenWeather](https://openweathermap.org/api) – Vthechamp Jan 09 '21 at 16:00
  • 1
    There are 17 values for temperature shown on that page, which were you trying to fetch? Your post is too broad, see [ask] – Sayse Jan 09 '21 at 16:00
  • 1
    does this help: https://stackoverflow.com/questions/1474489/python-weather-api? – ruddra Jan 09 '21 at 16:01

1 Answers1

1

I'd suggest operating with an API instead, since those are designed for interaction with an application. Google will probably get suspicious if you try to access search results programmatically, and ask you to fill out a recaptcha to prove that you're human.

That aside, here's how you would do it:

  1. Get the HTML content of the website using requests
  2. Parse the HTML using bs4
  3. Extract the needed data from the HTML

For example:

from bs4 import BeautifulSoup
import requests

url = 'https://www.accuweather.com/en/no/oslo/254946/current-weather/254946'
r = requests.get(url) # get the page content using requests

soup = BeautifulSoup(r, 'html.parser') # parse the page HTML

current_temp = soup.select('.display-temp').get_text() # locate the element of interest, and get the element's text content
dellitsni
  • 417
  • 2
  • 9