小编sjk*_*204的帖子

BeautifulSoup“AttributeError: 'NoneType' 对象没有属性 'text'”

我正在使用 bs4进行网页抓取天气搜索 Google,而 Python<span>在有标签时找不到标签。我怎么解决这个问题?

我试图找到这<span>class id,但都失败了。

<div id="wob_dcp">
    <span class="vk_gy vk_sh" id="wob_dc">Clear with periodic clouds</span>    
</div>
Run Code Online (Sandbox Code Playgroud)

以上是我试图在页面中抓取的 HTML 代码:

response = requests.get('https://www.google.com/search?hl=ja&ei=coGHXPWEIouUr7wPo9ixoAg&q=%EC%9D%BC%EB%B3%B8+%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E+%EB%82%B4%EC%9D%BC+%EB%82%A0%EC%94%A8&oq=%EC%9D%BC%EB%B3%B8+%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E+%EB%82%B4%EC%9D%BC+%EB%82%A0%EC%94%A8&gs_l=psy-ab.3...232674.234409..234575...0.0..0.251.929.0j6j1......0....1..gws-wiz.......35i39.yu0YE6lnCms')
soup = BeautifulSoup(response.content, 'html.parser')

tomorrow_weather = soup.find('span', {'id': 'wob_dc'}).text
Run Code Online (Sandbox Code Playgroud)

但是这个代码失败了,错误是:

Traceback (most recent call last):
  File "C:\Users\sungn_000\Desktop\weather.py", line 23, in <module>
    tomorrow_weather = soup.find('span', {'id': 'wob_dc'}).text
AttributeError: 'NoneType' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)

请解决这个错误。

html python beautifulsoup web-scraping

4
推荐指数
1
解决办法
8394
查看次数

标签 统计

beautifulsoup ×1

html ×1

python ×1

web-scraping ×1