簡體   English   中英

Selenium(Python)查找網頁上的所有可選窗口

[英]Selenium (Python) Finding all selectable windows on a webpage

因此,我試圖在此處解析出頁面上的所有href鏈接: https//data-wake.opendata.arcgis.com/datasets,但我注意到我所尋找的鏈接都沒有從我的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'))

這是來自chromes inspect的snipit: HTML Inspect

預期的結果將是返回類似以下內容:

“ / datasets / wakeforestnc :: state-system-streets”

我還注意到頁面上運行着名為Ember application.js的東西,我認為這可能阻止了我訪問嵌套在主ember標簽中的href屬性。 IM不熟悉emberair或如何解析這樣的復雜頁面,我們將不勝感激!

Ember.js用於構建SPA(單頁應用程序),並且通常是在客戶端呈現的。

我的猜測是,您的代碼正在頁面加載后但SPA呈現之前搜索所有錨。

您的代碼需要等待Ember應用程序呈現,也許要等到body元素具有ember-application類。

我相信您會在前端呈現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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM