Selenium (Python) 在网页上查找所有可选择的 windows
Selenium (Python) Finding all selectable windows on a webpage
所以我试图在这里解析页面上的所有 href 链接:https://data-wake.opendata.arcgis.com/datasets 但我注意到 none 我正在寻找的链接是 return 来自我的python 代码在这里:
driver = webdriver.PhantomJS("C:\Users\Jlong\Desktop\phantomjs.exe")
driver.get(r"https://data-wake.opendata.arcgis.com/datasets")
pagesource = driver.page_source
bsobj = BeautifulSoup(pagesource,'lxml')
for line in bsobj.find_all('a'):
print(line.get('href'))
这是来自 chrome inspect 的 html 的片段:
Html Inspect
预期结果将是 return 类似以下内容:
"/datasets/wakeforestnc::州系统街道"
我还注意到页面上有一个叫做 Ember application.js 运行 的东西,我认为这可能会阻止我访问深深嵌套在主 ember 标签。我不熟悉 ember 或如何解析像这样的复杂页面,任何帮助将不胜感激!
Ember.js 用于构建 SPA(单页应用程序),通常 client-side 呈现。
我的猜测是您的代码在页面加载之后但在 SPA 呈现之前搜索所有锚点。
您的代码需要等待 Ember 应用程序呈现,也许要等到 body
元素具有 class ember-application
.
我相信您会在 front-end 呈现之前获得 page_source。
我在访问 page_source
:
之前添加一个简单的 wait
通过 chromedriver 获得了这些链接(对于 phantomjs 应该是一样的)
from selenium import webdriver
from bs4 import BeautifulSoup
import time
driver = webdriver.Chrome()
driver.get("https://data-wake.opendata.arcgis.com/datasets")
time.sleep(5)
soup = BeautifulSoup(driver.page_source,'lxml')
for line in soup.find('ul', {'id':'search-results'}).find_all('a', {'class': 'result-name ember-view'}):
print(line.get('href'))
输出:
/datasets/tofv::fuquay-varina-utility-as-built-drawings
/datasets/tofv::private-sewer-manhole
/datasets/tofv::fuquay-varina-town-development
/datasets/tofv::blowoff-valve
/datasets/tofv::fuquay-varina-zoning
/datasets/tofv::drainage-point
/datasets/tofv::gravity-sewer-line
/datasets/tofv::water-meter-vault
/datasets/tofv::fuquay-varina-sidewalks
/datasets/tofv::water-line
所以我试图在这里解析页面上的所有 href 链接:https://data-wake.opendata.arcgis.com/datasets 但我注意到 none 我正在寻找的链接是 return 来自我的python 代码在这里:
driver = webdriver.PhantomJS("C:\Users\Jlong\Desktop\phantomjs.exe")
driver.get(r"https://data-wake.opendata.arcgis.com/datasets")
pagesource = driver.page_source
bsobj = BeautifulSoup(pagesource,'lxml')
for line in bsobj.find_all('a'):
print(line.get('href'))
这是来自 chrome inspect 的 html 的片段: Html Inspect
预期结果将是 return 类似以下内容:
"/datasets/wakeforestnc::州系统街道"
我还注意到页面上有一个叫做 Ember application.js 运行 的东西,我认为这可能会阻止我访问深深嵌套在主 ember 标签。我不熟悉 ember 或如何解析像这样的复杂页面,任何帮助将不胜感激!
Ember.js 用于构建 SPA(单页应用程序),通常 client-side 呈现。
我的猜测是您的代码在页面加载之后但在 SPA 呈现之前搜索所有锚点。
您的代码需要等待 Ember 应用程序呈现,也许要等到 body
元素具有 class ember-application
.
我相信您会在 front-end 呈现之前获得 page_source。
我在访问 page_source
:
wait
通过 chromedriver 获得了这些链接(对于 phantomjs 应该是一样的)
from selenium import webdriver
from bs4 import BeautifulSoup
import time
driver = webdriver.Chrome()
driver.get("https://data-wake.opendata.arcgis.com/datasets")
time.sleep(5)
soup = BeautifulSoup(driver.page_source,'lxml')
for line in soup.find('ul', {'id':'search-results'}).find_all('a', {'class': 'result-name ember-view'}):
print(line.get('href'))
输出:
/datasets/tofv::fuquay-varina-utility-as-built-drawings
/datasets/tofv::private-sewer-manhole
/datasets/tofv::fuquay-varina-town-development
/datasets/tofv::blowoff-valve
/datasets/tofv::fuquay-varina-zoning
/datasets/tofv::drainage-point
/datasets/tofv::gravity-sewer-line
/datasets/tofv::water-meter-vault
/datasets/tofv::fuquay-varina-sidewalks
/datasets/tofv::water-line