首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Yandex.Weather解析

Yandex.Weather解析
EN

Stack Overflow用户
提问于 2019-09-07 03:43:16
回答 1查看 834关注 0票数 2

我试着从https://www.yandex.com/weather/moscow下载7天的预报,问题是所有的日子,除了今天有相同的课。我如何获得7天(或至少9天)的预报?

我正在尝试BeautifulSoap库。我今天有天气,但其他几天都有问题。

下面是我的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-08 13:14:52

数据是从网络选项卡中可以找到的url中动态提取的。它返回html。您可以使用css选择器.card:not(.adv)隔离用于预测的日期块。使用bs4 4.7.1 +。解析出的示例感觉就像临时的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57833347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文