【问题标题】:How to fix 'AttributeError: 'NoneType' object has no attribute 'text''如何修复'AttributeError:'NoneType'对象没有属性'text''
【发布时间】:2019-05-21 16:01:48
【问题描述】:

我是网络抓取的新手,我正在尝试从 https://www.theweathernetwork.com/ca/hourly-weather-forecast/ontario/markham

我曾尝试使用soup.find 抓取该值,但是我不确定参数是否正确。我正在尝试从下面的代码中获取值 17:

<div class="info">
<div class="temp">17</div>
<div class="stackunits">
<div>
<span class="unitwrap">°C</span>
</div>
<div class="feels-like">
<span class="label">Feels like</span>
<span class="value">17</span>
</div>
</div>
</div>

# import libraries
from urllib.request import urlopen
from bs4 import BeautifulSoup

# specify url
quote_page = 'https://www.theweathernetwork.com/ca/hourly-weather-forecast/ontario/markham'

# query website and return html to the variable 'page'
page = urlopen(quote_page)

# parse html using BeautifulSoup and store in variable 'soup'
soup = BeautifulSoup(page, 'html.parser')

# take out the <div> of temp and get its value
atemp_box = soup.find('div', attrs={'class':'temp'})
atemp = atemp_box.text
print(atemp)

AttributeError: 'NoneType' 对象没有属性 'text'

【问题讨论】:

  • 你想达到什么结果?

标签: python web-scraping beautifulsoup


【解决方案1】:

使用相同的端点,一个 API,页面用于获取该信息

import requests

r = requests.get('https://www.theweathernetwork.com/api/data/caon0409/hourly/cm?ts=1632').json()

然后是温度

r['obs']['t']

返回了很多信息。探索回复here。例如,您可以在以下范围内找到短期预测:

r['sterm']['periods']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多