Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 组合数提取_Python - Fatal编程技术网

Python 组合数提取

Python 组合数提取,python,Python,``因此,我试图从这个气象站点获取温度。但我一直都在回一个空白的答案。这是我的密码 这些值可能是动态呈现的,即这些值可能由页面中的javascript填充 requests.get()只返回从服务器接收到的标记,而不做任何进一步的客户端更改,因此不完全是等待 您可以使用加载页面URL并获取页面源。(也可以使用Firefox驱动程序) 转到chrome://settings/help检查您当前的chrome版本,并从下载该版本的驱动程序。确保将驱动程序文件保存在路径中,或者保存在python脚本

``因此,我试图从这个气象站点获取温度。但我一直都在回一个空白的答案。这是我的密码


这些值可能是动态呈现的,即这些值可能由页面中的javascript填充

requests.get()
只返回从服务器接收到的标记,而不做任何进一步的客户端更改,因此不完全是等待

您可以使用加载页面URL并获取页面源。(也可以使用Firefox驱动程序)

转到
chrome://settings/help
检查您当前的chrome版本,并从下载该版本的驱动程序。确保将驱动程序文件保存在
路径中,或者保存在python脚本所在的文件夹中

试试这个:

from bs4 import BeautifulSoup as bs
from selenium.webdriver import Chrome # pip install selenium
from selenium.webdriver.chrome.options import Options

url = "https://www.theweathernetwork.com/ca/hourly-weather-forecast/ontario/oakville"

#Make it headless i.e. run in backgroud without opening chrome window
chrome_options = Options()  
chrome_options.add_argument("--headless")

# use Chrome to get page with javascript generated content
with Chrome(executable_path="./chromedriver", options=chrome_options) as browser:
     browser.get(url)
     page_source = browser.page_source

#Parse the final page source
soup = bs(page_source, 'html.parser')

weatherdata = soup.find('span', class_='temp')

print(weatherdata.text)
参考资料:


问题似乎在于数据是通过JavaScript加载的,因此加载特定跨度的值需要一段时间。当你做你的请求时,它似乎是空的,只在一段时间后加载。一种可能的解决方案是使用selenium等待页面加载,然后提取html

from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://www.theweathernetwork.com/ca/hourly-weather-forecast/ontario/oakville"
browser = webdriver.Chrome()
browser.get(url)
html = browser.page_source

soup = BeautifulSoup(html, 'html.parser')
elem = soup.find('span', class_='temp')

print(elem.text)

欢迎来到堆栈溢出。请复制/粘贴代码,而不是附加图片。仍然不返回值。我在找工作temperature@NoahHamilton,很高兴它成功了,你能帮我接受答案吗?:)谢谢
10
from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://www.theweathernetwork.com/ca/hourly-weather-forecast/ontario/oakville"
browser = webdriver.Chrome()
browser.get(url)
html = browser.page_source

soup = BeautifulSoup(html, 'html.parser')
elem = soup.find('span', class_='temp')

print(elem.text)