El desguace de enlaces debería ser una hazaña simple, generalmente simplemente tomando el valor src
de la etiqueta a.
Recientemente me encontré con este sitio web ( https://sunteccity.com.sg/promotions ) donde no se puede encontrar el valor href de las etiquetas de cada artículo, pero la redirección aún funciona. Estoy tratando de encontrar una manera de obtener los elementos y sus enlaces correspondientes. Mi código típico de python selenium se parece a algo así
all_items = bot.find_elements_by_class_name('thumb-img') for promo in all_items: a = promo.find_elements_by_tag_name("a") print("a[0]: ", a[0].get_attribute("href"))
Sin embargo, parece que no puedo recuperar ningún atributo href
, onclick
, y me pregunto si esto es posible. Me di cuenta de que no podía hacer clic con el botón derecho, abrir el enlace en una nueva pestaña también.
¿Hay alguna forma de evitar obtener los enlaces de todos estos elementos?
Editar: ¿Hay alguna forma de recuperar todos los enlaces de los elementos en las páginas?
es decir
https://sunteccity.com.sg/promotions/724 https://sunteccity.com.sg/promotions/731 https://sunteccity.com.sg/promotions/751 https://sunteccity.com.sg/promotions/752 https://sunteccity.com.sg/promotions/754 https://sunteccity.com.sg/promotions/280 ...
Editar: agregar una imagen de una de esas etiquetas de anclaje para una mayor claridad:
Está utilizando un localizador incorrecto. Te trae muchos elementos irrelevantes.
En lugar de find_elements_by_class_name('thumb-img')
, intente find_elements_by_css_selector('.collections-page .thumb-img')
para que su código sea
all_items = bot.find_elements_by_css_selector('.collections-page .thumb-img') for promo in all_items: a = promo.find_elements_by_tag_name("a") print("a[0]: ", a[0].get_attribute("href"))
También puede obtener los enlaces deseados directamente por .collections-page .thumb-img a
localizador para que su código pueda ser:
links = bot.find_elements_by_css_selector('.collections-page .thumb-img a') for link in links: print(link.get_attribute("href"))
Mediante la ingeniería inversa del Javascript que lo lleva a las páginas de promociones (visto en https://sunteccity.com.sg/_nuxt/d4b648f.js ) que le brinda una forma de obtener todos los enlaces, que se basan en HappeningID
. Puede verificar ejecutando esto en la consola JS, lo que le otorga la primera promoción:
window.__NUXT__.state.Promotion.promotions[0].HappeningID
Basado en eso, puedes crear un bucle de Python para obtener todas las promociones:
items = driver.execute_script("return window.__NUXT__.state.Promotion;") for item in items["promotions"]: base = "https://sunteccity.com.sg/promotions/" happening_id = str(item["HappeningID"]) print(base + happening_id)
Eso generó la siguiente salida:
https://sunteccity.com.sg/promotions/724 https://sunteccity.com.sg/promotions/731 https://sunteccity.com.sg/promotions/751 https://sunteccity.com.sg/promotions/752 https://sunteccity.com.sg/promotions/754 https://sunteccity.com.sg/promotions/280 https://sunteccity.com.sg/promotions/764 https://sunteccity.com.sg/promotions/766 https://sunteccity.com.sg/promotions/762 https://sunteccity.com.sg/promotions/767 https://sunteccity.com.sg/promotions/732 https://sunteccity.com.sg/promotions/733 https://sunteccity.com.sg/promotions/735 https://sunteccity.com.sg/promotions/736 https://sunteccity.com.sg/promotions/737 https://sunteccity.com.sg/promotions/738 https://sunteccity.com.sg/promotions/739 https://sunteccity.com.sg/promotions/740 https://sunteccity.com.sg/promotions/741 https://sunteccity.com.sg/promotions/742 https://sunteccity.com.sg/promotions/743 https://sunteccity.com.sg/promotions/744 https://sunteccity.com.sg/promotions/745 https://sunteccity.com.sg/promotions/746 https://sunteccity.com.sg/promotions/747 https://sunteccity.com.sg/promotions/748 https://sunteccity.com.sg/promotions/749 https://sunteccity.com.sg/promotions/750 https://sunteccity.com.sg/promotions/753 https://sunteccity.com.sg/promotions/755 https://sunteccity.com.sg/promotions/756 https://sunteccity.com.sg/promotions/757 https://sunteccity.com.sg/promotions/758 https://sunteccity.com.sg/promotions/759 https://sunteccity.com.sg/promotions/760 https://sunteccity.com.sg/promotions/761 https://sunteccity.com.sg/promotions/763 https://sunteccity.com.sg/promotions/765 https://sunteccity.com.sg/promotions/730 https://sunteccity.com.sg/promotions/734 https://sunteccity.com.sg/promotions/623