【发布时间】:2021-06-09 21:34:53
【问题描述】:
我正在尝试在 Eclipse 中使用 Python 创建一个简单的天气预报。到目前为止,我已经写了这个:
from bs4 import BeautifulSoup
import requests
def weather_forecast():
url = 'https://www.yr.no/nb/v%C3%A6rvarsel/daglig-tabell/1-92416/Norge/Vestland/Bergen/Bergen'
r = requests.get(url) # Get request for contents of the page
print(r.content) # Outputs HTML code for the page
soup = BeautifulSoup(r.content, 'html5lib') # Parse the data with BeautifulSoup(HTML-string, html-parser)
min_max = soup.select('min-max.temperature') # Select all spans with a "min-max-temperature" attribute
print(min_max.prettify())
table = soup.find('div', attrs={'daily-weather-list-item__temperature'})
print(table.prettify())
来自具有如下元素的 html 页面:
我在 HTML 页面的元素中找到了第一个温度的路径,但是当我尝试执行我的代码并打印以查看我是否正确完成时,没有打印任何内容。我的目标是打印一个包含日期和相应温度的表格,这似乎是一项简单的任务,但我不知道如何正确命名属性或如何在一次迭代中从 HTML 页面中将它们全部刮掉。
我想进入每个 我在 stackoverflow 上看过这个问题,但我想不通:
Python BeautifulSoup - Scraping Div Spans and p tags - also how to get exact match on div name 【问题讨论】:
标签: python html web-scraping beautifulsoup