python - BeautifulSoup "AttributeError: ' NoneType'对象没有属性 'text'“

标签 python html web-scraping beautifulsoup

我正在抓取网页 weather-searched Google使用 bs4,Python 找不到 <span>当有一个标签时。我该如何解决这个问题?

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

<div id="wob_dcp">
    <span class="vk_gy vk_sh" id="wob_dc">Clear with periodic clouds</span>    
</div>

上面是我试图在 page 中抓取的 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

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

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'

请解决此错误。

最佳答案

这是因为天气部分是由浏览器通过 JavaScript 呈现的。因此,当您使用 requests 时,您只能获得页面的 HTML 内容,而没有您需要的内容。 如果您想使用网络浏览器呈现的元素来解析页面,您应该使用例如 selenium (或 requests-html)。

from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
response = session.get('https://www.google.com/search?hl=en&ei=coGHXPWEIouUr7wPo9ixoAg&q=%EC%9D%BC%EB%B3%B8%20%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E%20%EB%82%B4%EC%9D%BC%20%EB%82%A0%EC%94%A8&oq=%EC%9D%BC%EB%B3%B8%20%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E%20%EB%82%B4%EC%9D%BC%20%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
print(tomorrow_weather)

输出:

pawel@pawel-XPS-15-9570:~$ python test.py
Clear with periodic clouds

关于python - BeautifulSoup "AttributeError: ' NoneType'对象没有属性 'text'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55351871/

相关文章:

python - 错误 "TypeError: a bytes-like object is required, not ' int'"

python - 包装 np.arrays __pow__ 方法

html - CSS:可变图像(横向/纵向)不同尺寸..需要在 div 容器中居中

javascript - 隐藏值低于特定值的类别 Highcharts

python - 使用 pandas 进行网页抓取时在列表中显示 0 个元素

python - Numpy 的 matrix_power 函数给出大指数的错误结果

python - 如何训练模型添加新类别?

javascript - 我收到此错误 : SyntaxError: Unexpected end of input; What is wrong with my cookie creating code?

html - 如何使用 R 从 iframe 的输入标签中抓取数据

python - 无法使用请求从网页中抓取某个字段的值