簡體   English   中英

Python Selenium Scraping Javascript-找不到元素

[英]Python Selenium Scraping Javascript - Element not found

我正在嘗試抓取以下Javascript前端網站以練習我的Javascript抓取技能: https : //www.oplaadpalen.nl/laadpaal/112618

我試圖通過它們的xPath查找兩個不同的元素。 第一個是標題,它確實找到了。 第二個是實際的文本本身,但是找不到。 奇怪,因為我只是從Chrome瀏覽器復制了xPath。

from selenium import webdriver

link = 'https://www.oplaadpalen.nl/laadpaal/112618'
driver = webdriver.PhantomJS()
driver.get(link)

#It could find the right element
xpath_attribute_title = '//*[@id="main-sidebar-container"]/div/div[1]/div[2]/div/div[' + str(3) + ']/label'
next_page_elem_title = driver.find_element_by_xpath(xpath_attribute_title)
print(next_page_elem_title.text)

#It fails to find the right element
xpath_attribute_value = '//*[@id="main-sidebar-container"]/div/div[1]/div[2]/div/div[' + str(3) + ']/text()'
next_page_elem_value = driver.find_element_by_xpath(xpath_attribute_value)
print(next_page_elem_value.text)

我嘗試了幾件事:將“ text()”更改為“ text”,“(text)”,但是似乎都沒有用。

我有兩個問題:

  • 為什么找不到正確的元素?
  • 我們如何做才能使其找到正確的元素?

Selenium的find_element_by_xpath()方法返回與給定XPath查詢匹配的第一個元素節點 (如果有)。 但是,XPath的text()函數返回一個文本節點,而不是包含它的元素節點。

要使用Selenium的finder方法提取文本,您需要找到包含元素,然后從返回的對象中提取文本。

我建議一種稍微不同的方法。 我會抓緊整個文本然后分開 : 這將為您提供標題和價值。 下面的代碼將通過openstijden標簽獲取Paalcode。

for x in range(2, 8):
    s = driver.find_element_by_css_selector("div.leftblock > div.labels > div")[x].text
    t = s.split(":", 1)
    print(t[0]) # title
    print(t[1]) # value

您不希望拆分多次,因為狀態包含更多的分號。

使用@JeffC的方法,如果要首先使用xpath而不是CSS選擇器選擇所有這些元素,則可以使用以下代碼:

xpath_title_value = "//div[@class='labels']//div[label[contains(text(),':')] and not(div) and not(contains(@class,'toolbox'))]"
title_and_value_elements = driver.find_elements_by_xpath(xpath_title_value)

請注意find_elements_by_xpath方法中的復數元素 上面的xpath選擇div元素,它們是div元素的后代,該div元素的類屬性為“ labels”。 每個選定的div的嵌套標簽必須包含一個冒號。 此外,div本身可能不具有“工具箱”類(頁面上某些其他div具有),也不能包含任何其他嵌套的div。

接下來,您可以提取單個div元素(也包含來自嵌套標簽元素的文本)中的文本,然后使用“:\\ n”將其分開,從而將標題和值分隔在原始文本字符串中。

for element in title_and_value_elements:
    element = element.text
    title,value = element.split(":\n")
    print(title)
    print(value,"\n")

保持自己的邏輯完整,可以按以下方式提取標簽和關聯

for x in range(3, 8):
    label = driver.find_element_by_xpath("//div[@class='labels']//following::div[%s]/label" %x).get_attribute("innerHTML")
    value = driver.find_element_by_xpath("//div[@class='labels']//following::div[%s]" %x).get_attribute("innerHTML").split(">")[2]
    print("Label is %s and value is %s" % (label, value))

控制台輸出:

Label is Paalcode: and value is NewMotion 04001157
Label is Adres: and value is Deventerstraat 130
Label is pc/plaats: and value is 7321cd Apeldoorn

由於您想練習JS技能,因此您也可以在JS中執行此操作,實際上所有div都包含更多數據,您可以查看是否將其粘貼在瀏覽器控制台中:

labels = document.querySelectorAll(".labels");
divs = labels[0].querySelectorAll("div");
for (div of divs) console.log(div.firstChild, div.textContent); 

您可以push送到數組並僅檢查具有label div並在python變量中返回結果數組:

labels_value_pair.driver.execute_script('''
scrap = [];
labels = document.querySelectorAll(".labels");
divs = labels[0].querySelectorAll("div");
for (div of divs) if (div.firstChild.tagName==="LABEL") scrap.push(div.firstChild.textContent, div.textContent); 
return scrap;
''')

暫無
暫無

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

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