Search code examples
python-3.xbeautifulsouppython-webbrowser

How do I read data from the web based on the number of rows


How do I read data from the web based on the number of rows. Example: I want to read only 2 rows from a 30-row table

from bs4 import BeautifulSoup
import requests
url="https://weather.day.az/az/"
response=requests.get(url)
icerik=response.content
soup=BeautifulSoup(icerik,"html.parser")
hava=soup.find_all("",{"class":"location_name"})
derece=soup.find_all("",{"class":"weather_factical"})
for temp,weather in zip(hava,derece):
  temp=temp.text
  temp = temp.replace("\n", "")
  weather=weather.text
  weather=weather.replace("\n","")
  print(temp)
  print(weather)

Solution

  • You can simply add a counter variable and count the times you loop has iterated, like so:

    for i,(temp,weather) in enumerate(zip(hava,derece)):
        temp=temp.text
        temp = temp.replace("\n", "")
        weather=weather.text
        weather=weather.replace("\n","")
        print(temp)
        print(weather)
        if i==1:
            break
    

    The above code will print the top 2 values.