【问题标题】:Get some tags through web scraping with selenium in python在 python 中使用 selenium 通过 web 抓取获取一些标签
【发布时间】: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


    【解决方案1】:

    尝试如下并确认:

    driver.get("https://www.inmuebles24.com/propiedades/bosques-de-las-lomas-departamento-a-la-venta-en-bosque-62355654.html")
    links = []
    titulo = []
    sup_total = []
    sup = ""
    superficie_cons = []
    cons_ = ""
    banos = []
    ban_ = ""
    
    title = driver.find_element_by_xpath("//section[contains(@class,'article-section-description')]//h1").text
    print(title)
    features = driver.find_elements_by_class_name("icon-feature")
    
    for feature in features:
        if 'Total' in feature.text:
            sup = feature.text
    
        if 'Construido' in feature.text:
            cons_ = feature.text
    
        if 'Baño' in feature.text:
            ban_ = feature.text
    
    sup_total.append(sup)
    superficie_cons.append(cons_)
    banos.append(ban_)
    print(sup_total)
    print(superficie_cons)
    print(banos)
    
    Bosques de Las Lomas Departamento a La Venta en Bosque de Tejocotes (Ao)
    ['220 m² Total']
    ['220 m² Construido']
    ['3 Baños']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 2020-10-31
      • 1970-01-01
      • 2021-02-11
      • 2020-11-15
      • 2017-11-23
      • 2021-08-27
      相关资源
      最近更新 更多