如何根据行数从网络中读取数据
How do I read data from the web based on the number of rows
如何根据行数从网络中读取数据。
示例:我只想从 30 行 table
中读取 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)
您可以简单地添加一个计数器变量并计算循环迭代的次数,如下所示:
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
以上代码将打印前 2 个值。
如何根据行数从网络中读取数据。 示例:我只想从 30 行 table
中读取 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)
您可以简单地添加一个计数器变量并计算循环迭代的次数,如下所示:
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
以上代码将打印前 2 个值。