我试着从https://www.yandex.com/weather/moscow下载7天的预报,问题是所有的日子,除了今天有相同的课。我如何获得7天(或至少9天)的预报?
我正在尝试BeautifulSoap库。我今天有天气,但其他几天都有问题。
下面是我的代码:
import urllib.request
from bs4 import BeautifulSoup
def get_html(url):
response = urllib.request.urlopen(url)
return response.read()
def parse_today(html):
soup = BeautifulSoup(html, "html.parser")
temp = soup.find('div', class_='temp fact__temp fact__temp_size_s').get_text().encode('utf-8').decode('utf-8', 'ignore')
return temp
def parse_next_day(day_num, html):
# ?????
pass
def main():
temp = parse_today(get_html('https://yandex.ru/weather/moscow'))
print("Now the temperature is: ", temp)
for i in range(1,6):
next_temp = parse_next_day(i+1, get_html('https://yandex.ru/weather/moscow'))
print("The day", i+1, "temperature is : ", next_temp)
if __name__ == '__main__':
main()
发布于 2019-09-08 13:14:52
数据是从网络选项卡中可以找到的url中动态提取的。它返回html。您可以使用css选择器.card:not(.adv)
隔离用于预测的日期块。使用bs4 4.7.1 +。解析出的示例感觉就像临时的:
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://www.yandex.com/weather/segment/details?offset=0&lat=55.753215&lon=37.622504&geoid=213&limit=10', headers = {'User-Agent':'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
for card in soup.select('.card:not(.adv)'):
date = ' '.join([i.text for i in card.select('[class$=number],[class$=month]')])
print(date)
temps = list(zip(
[i.text for i in card.select('.weather-table__daypart')]
, [i.text for i in card.select('.weather-table__body-cell_type_feels-like .temp__value')]
))
print(temps)
https://stackoverflow.com/questions/57833347
复制相似问题