Selenium Python -Text()与XPath一起使用,但与CSS_SELECTOR无关
我正在尝试在魁北克网站上为房屋上市网站进行封存,但我遇到了一个问题。
当我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从textcontent属性中提取它
You can pull it from the textContent attribute