Selenium Python -Text()与XPath一起使用,但与CSS_SELECTOR无关

发布于 2025-02-05 20:57:17 字数 1101 浏览 2 评论 0原文

我正在尝试在魁北克网站上为房屋上市网站进行封存,但我遇到了一个问题。

当我使用XPATH时,我设法提取电话号码,但是当我使用CSS选择器时,我的变量为空白。

但是,我很确定我的目标是正确的位置,我甚至使用Chrome中的CSS工具进行验证。

我需要使用CSS选择器为beagause之

所href =“ https://duproprio.com/fr/monteregie-rive-sud-montreal/ange-gardien-gardien-de-rouville/ferme--fermette-a-a-vendre/hab-707-rang-707-rang-saint-charles-charles-1008751” =“ nofollow noreferrer”> https://duproprio.com/fr/monteregie-rive-sud-montreal/ange-gardien-gardien-gardien-de-rouville/ferme-ferme-fermete-a-fermette-a-a-vendre/hab-707-rang-707-rang-saint-charles-charles-charles-charles-charles-charles-charles-charles-charles-charles-charles- 1008751

示例不起作用的

        try:
            house_phone = deal_box.find_element_by_xpath("/html/body/main/div[1]/div/div[2]/div[1]/div[1]/div[2]/section/div[1]/div[1]/div/a").text.strip()
        except: house_phone = "N/D"

示例,

       try:
           house_phone = deal_box.find_element(By.CSS_SELECTOR, 'a.gtm-listing-link-contact-owner-phone').text.strip()
       except: house_phone = "N/D"

谢谢

I am trying to webscrape a House listing website in Quebec and I encounter a problem.

When I use Xpath I manage to extract the phone number, but when I use CSS selectors my variable is Blank.

However I am pretty sure that I am aiming at the right spot I even used the css tool in chrome to verify.

The reason why i would need to use CSS selector is beacause the page is dynamic and not every listing is same, which means that sometimes I am not able to point out the real location using xpath like that

An example would be this URL: https://duproprio.com/fr/monteregie-rive-sud-montreal/ange-gardien-de-rouville/ferme-fermette-a-vendre/hab-707-rang-saint-charles-1008751

EXAMPLE THAT WORKS

        try:
            house_phone = deal_box.find_element_by_xpath("/html/body/main/div[1]/div/div[2]/div[1]/div[1]/div[2]/section/div[1]/div[1]/div/a").text.strip()
        except: house_phone = "N/D"

EXAMPLE THAT DOESNT WORK

       try:
           house_phone = deal_box.find_element(By.CSS_SELECTOR, 'a.gtm-listing-link-contact-owner-phone').text.strip()
       except: house_phone = "N/D"

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2025-02-12 20:57:17

您可以从textcontent属性中提取它

deal_box.get(url)
try:
    house_phone = deal_box.find_element(By.CSS_SELECTOR, 'a.gtm-listing-link-contact-owner-phone').get_attribute('textContent').strip()
except Exception as e:
    print(e)
    house_phone = "N/D"
    
house_phone

You can pull it from the textContent attribute

deal_box.get(url)
try:
    house_phone = deal_box.find_element(By.CSS_SELECTOR, 'a.gtm-listing-link-contact-owner-phone').get_attribute('textContent').strip()
except Exception as e:
    print(e)
    house_phone = "N/D"
    
house_phone
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文