我使用 Selenium 驅動程式爬過許多網站頁面。每次我得到一個新頁面時,我都會將 html 附加到一個名為“All_APP_Pages”的變數中。變數 All_APP_Pages 是一個保存許多頁面的 html 的變數。沒有發布代碼,因為它很長并且與問題無關。Python 將“All_APP_Pages”列為位元組型別。
from lxml import html
from lxml import etree
import xml.etree.ElementTree as ET
from selenium.webdriver.common.by import By
dom = etree.HTML(All_APP_Pages)
xp = "//tr[.//span[contains(.,'Product Data Solutions (UHC MR)')] and .//td[contains(.,'SQLServer')] and .//td[contains(.,'MR')]]//a"
link = dom.xpath(xp)
print(link)
掃描完所有頁面后,我需要從此 xpath 獲取鏈接
"//tr[.//span[contains(.,'Product Data Solutions (ABC MR)')] and .//td[contains(.,'SQLServer')] and .//td[contains(.,'MR')]]//a"
此處列出的 xpath 有效。但是,如果驅動程式位于存在此鏈接的頁面上,則它僅適用于 selenium 驅動程式。這就是為什么所有頁面都在一個變數中,因為我不知道鏈接將在哪個頁面上。列印顯示此結果
[<Element a at 0x1c39dea1180>]
如何從鏈接中獲取此值,以便檢查值是否正確?
uj5u.com熱心網友回復:
您需要迭代串列并獲取href值
dom = etree.HTML(All_APP_Pages)
xp = "//tr[.//span[contains(.,'Product Data Solutions (UHC MR)')] and .//td[contains(.,'SQLServer')] and .//td[contains(.,'MR')]]//a"
link = dom.xpath(xp)
hrefs=[l.attrib["href"] for l in link]
print(hrefs)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/444608.html
