【发布时间】:2021-12-01 07:44:58
【问题描述】:
感谢您花时间阅读我,我希望以一种清晰而简单的方式写作。我是python和selenium的新手,我正在尝试抓取链接https://www.inmuebles24.com/propiedades/bosques-de-las-lomas-departamento-a-la-venta-en-bosque-62355654.html的网页,另一个类似的链接,一个规范,是我想抓取更多这样的页面,但我正在尝试使用这。现在我在抓取一些标签时遇到了问题,因为有些标签包含在某些页面中,但其他页面中没有。通过这种方式,我想抓取可能包含某些网页的标签列表。下面是网页上的一段视图,我用红色圈出了标签。
The image shows in red the tags I'm interested
所以,我正在尝试使用“for”、“try”和“except”函数来抓取标签,然后将信息保存在不同的列表中。代码如下。
par2 = ['https://www.inmuebles24.com/propiedades/bosques-de-las-lomas-departamento-a-la-venta-en-bosque-62355654.html',
'https://www.inmuebles24.com/propiedades/tu-mejor-lugar-en-tulum-hyd-60498140.html']
links = []
titulo = []
sup_total = []
sup = ""
superficie_cons = []
cons_ = ""
baños = []
ban_ = ""
for pares in par2:
pares2 = pares
driver=webdriver.Chrome('C:/driver/chromedriver.exe')
driver.get(pares)
sleep(random.uniform(1,4))
tit = driver.find_element_by_xpath('//*[@class="section-title"]').text
titulo.append(tit)
links.append(pares2)
etiquetas = driver.find_elements_by_xpath('.//*[@class="icon-feature"]')
for etiq in etiquetas:
try:
valores = etiq.find_element_by_xpath('.//*[@class="icon feature"]').text.strip().lower()
print(valores)
if 'total' in valores:
sup = valores
if 'construido ' in valores:
cons_ = valores
if 'baños' in valores:
ban_ = valores
except:
print('null')
sup_total.append(sup)
superficie_cons.append(cons_)
baños.append(ban_)
driver.quit()
我的想法是,如果页面包含标签,代码必须下载每个标签。比如第一个网页有7个标签,而第二个链接有6个标签,这就是我考虑标签名称的原因。
【问题讨论】:
标签: python html selenium xpath