Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当从Google Weather中获取风速时,我可以看到它,但可以';别刮了_Python_Python 3.x_Beautifulsoup_Python Requests - Fatal编程技术网

Python 当从Google Weather中获取风速时,我可以看到它,但可以';别刮了

Python 当从Google Weather中获取风速时,我可以看到它,但可以';别刮了,python,python-3.x,beautifulsoup,python-requests,Python,Python 3.x,Beautifulsoup,Python Requests,我正试图从谷歌7天预报中获取风速。当我查看网页代码时,我可以看到风速,但当我在课堂上使用find_all()时,它只返回7天预报中的温度数据和今天的风速 import requests from bs4 import BeautifulSoup page = requests.get("https://www.google.co.nz/search?ei=CQmzW9_zHsaiwAPuvruwCQ&q=tauranga+weather+forecast&oq=tauranga

我正试图从谷歌7天预报中获取风速。当我查看网页代码时,我可以看到风速,但当我在课堂上使用
find_all()
时,它只返回7天预报中的温度数据和今天的风速

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.google.co.nz/search?ei=CQmzW9_zHsaiwAPuvruwCQ&q=tauranga+weather+forecast&oq=tauranga++forecast&gs_l=psy-ab.3.0.0i7i30k1l10.9062.9062.0.11810.1.1.0.0.0.0.205.205.2-1.1.0....0...1c.1.64.psy-ab..0.1.205....0.R-r6_9AWgnA")
soup = BeautifulSoup(page.content, "html.parser")

wind = soup.find_all("span", class_="wob_t")

for i, e in enumerate(wind):
    print(i, e.get_text())

我做错了什么

我看到您正在查看的所需div内容是由一些javascript代码生成的。i、 e:当您点击页面上显示的“Wind”按钮时,javascript会修改HTML并生成Wind div,持续7天

使用给定URL生成的soup,我看到只有一个条目是wind speed div。这是页面上显示的内容

In [7]: soup.findAll("span", text = re.compile("km/h"))
Out[7]: [<span class="wob_t" style="display:inline">16 km/h</span>]
[7]中的
:soup.findAll(“span”,text=re.compile(“km/h”))
输出[7]:[16公里/小时]

所以,使用请求模块删除这些基于javascript修改更新HTML的页面并不是一个好主意。我建议访问您应该使用的这些类型的页面

如果布局更改,您的应用程序将中断。考虑使用天气API代替。我同意亚力山大。但只是为了好玩,用“e”类为div做一个查找所有。你将到达风跨度相等的潜水舱谢谢,我原以为硒元素可能超出我的技能水平,但我会尝试一下。我可以在源代码中看到7天预报的所有风数据:13公里/小时,这是javascript修改的吗?谢谢你的帮助