我正在尝试从此website获取高程数据,以及开始和结束传递时间。
到目前为止,我已经查看了源代码并且无法使用Beautiful Soup来获得我想要的东西,因为源代码没有任何关于我感兴趣的信息的标签。该信息包含在函数中,名称为spStart ,这是相应的论点。我开始使用selenium来获取Javascript处理的代码,但我最终获得了与页面上的源代码相同的内容,现在我被卡住了。
这是我尝试使用selenium:
import datetime
import time
from bs4 import BeautifulSoup
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import selenium.webdriver.chrome.service as service
from lxml import html
try:
#Launching chrome in headless mode to access inspect element code''
service = service.Service('/correct_path/chromedriver.exe')
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'/correct_path/chromedriver.exe')
driver.get("https://www.n2yo.com/passes/?s=39090&a=1")
print("Chrome Browser Initialized in Headless Mode")
soup = BeautifulSoup(driver.execute_script("return document.documentElement.innerHTML;"), "lxml")
print(soup)
except KeyboardInterrupt:
driver.quit()
print("Driver Exited")
当我运行此代码时,它为我提供了在chrome中使用“view source”选项时看到的html。我的印象是,通过使用selenium以这种方式获取源代码,我会看到在chrome中的同一页面上使用“inspect element”选项时可用的内容。
有人会介意解释我出错的地方并建议一种可行的方法来获取我想要的数据,可能还有一个解释的例子吗?我真的很感激。
感谢您的时间。