Python 3.x 如何根据行数从web读取数据

Python 3.x 如何根据行数从web读取数据,python-3.x,beautifulsoup,python-webbrowser,Python 3.x,Beautifulsoup,Python Webbrowser,如何根据行数从web读取数据。 示例:我只想从30行表中读取2行 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"

如何根据行数从web读取数据。 示例:我只想从30行表中读取2行

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)

您只需添加一个计数器变量并计算循环的迭代次数,如下所示:

用于枚举(zip(哈瓦,德里斯))中的i(温度,天气):
temp=temp.text
临时=临时替换(“\n”和“”)
weather=weather.text
天气=天气。替换(“\n”和“”)
打印(临时)
印刷品(天气)
如果i==1:
打破
上述代码将打印前2个值