首页 > 解决方案 > Selenium Python 返回异常数据

问题描述

我正在尝试从以下标签中提取 href

<a href="https://www.olx.ph/item/pioneer-pointe-condominium-unit-for-rent-1-br-fully-furnished-22k-ID8k7OP.html?h=ba76d6b70e&amp;utm_source=Opt_Homepage_Var_1&amp;utm_medium=Ad_Clicks&amp;utm_campaign=Phase_2" itemprop="url" class="funnel" data-category-id="137" data-funnel-type="Select Ad" data-action-type="Select Ad" data-funnel-userid="0">
                        <span class="title" itemprop="name">Pioneer Pointe Condominium unit for rent - 1 BR Fully Furnished - 22K</span>
                    </a>

我在 Selenium 和 python 中使用以下代码:

links=browser.find_elements_by_xpath('//a[@itemprop="url"]')
for l in links:
print(l)

我目前不寻常的输出是:

<selenium.webdriver.remote.webelement.WebElement (session="8b6a29a1af20221f48056d6a8f34bd63", element="0.8368598264582081-1")>
<selenium.webdriver.remote.webelement.WebElement (session="8b6a29a1af20221f48056d6a8f34bd63", element="0.8368598264582081-2")>
<selenium.webdriver.remote.webelement.WebElement (session="8b6a29a1af20221f48056d6a8f34bd63", element="0.8368598264582081-3")>

注意:这只是输出的一部分(前三行)

这些应该是 a 标签的 href

标签: python-3.xseleniumweb-scrapingbeautifulsoup

解决方案


只是 l 返回浏览器找到的对象你必须指定你想要的对象的哪一部分

for l in links:
    print(l.get_attribute("href"))

推荐阅读