【发布时间】:2023-02-10 02:30:07
【问题描述】:
我是一名建筑修复学生,我正在学习刮擦。我正在收集西班牙教堂的数据。为此,我正在与 Catastro 网站合作。我正在收集数据,但无法获取图像的 src。
接下来,我将我创建的代码的一部分放在#Get the URL of the image 部分中向我抛出一个错误。当我从浏览器手动访问时,如果我能够找到图像,但我找不到使用 Selenium 的方法。可能是因为该元素位于嵌套的 ::before 中?
谢谢
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
# Start a webdriver session using Firefox
driver = webdriver.Firefox()
# Go to the website
driver.get("https://www1.sedecatastro.gob.es/Cartografia/mapa.aspx?refcat=9271101WJ9197A&from=OVCBusqueda&pest=rc&final=&RCCompleta=9271101WJ9197A0001BR&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem=&del=2&mun=900")
# Wait until the map element is present and click on its center
map_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="map"]'))
)
driver.execute_script("arguments[0].scrollIntoView(true);", map_element)
map_element.click()
# Get the URL of the image
img_element = driver.find_element_by_xpath('//*[@id="ImgFachada0"]')
# Get the src attribute of the image element
img_src = img_element.get_attribute("src")
# Print the src of the image
print(img_src)
【问题讨论】:
标签: python selenium web-scraping