Greenyuno
Greenyuno

Reputation: 29

Getting names from a website with a list doesn't always work

I have the following code:

Linksml=["https://articulo.mercadolibre.cl/MLC-554702642-samsung-note-9-dual-sim-128gb-carcasas-y-caja-_JM",
"https://articulo.mercadolibre.cl/MLC-554718846-audifonos-hyperx-cloud-ps4-_JM",
"https://articulo.mercadolibre.cl/MLC-554753668-disco-duro-ssd-_JM",
"https://articulo.mercadolibre.cl/MLC-554695355-celuar-samsung-j6-duos-_JM"]

for x in range(len(Linksml)):
    page=requests.get(Linksml[x])  
    soup=BeautifulSoup(page.content,'html.parser')
    tags=soup.find('h1', class_='item-title__primary ')
    print(tags)

It works, but if I run it 10 times, it will show me the name of product 1 and 2 on 5 occasions, and on the remaining 5, it will show me "none". It is random with the 3 products, in one execution the product 1 can show name, in two later it can show "none". It is like this for the 3 products, I don't know what to do anymore. Please help me, Olympian gods.

Upvotes: 3

Views: 47

Answers (1)

Sushil
Sushil

Reputation: 5531

Actually, the first link is different from the rest, so I have removed it from the list. This code works for the other links. Try this:

from bs4 import BeautifulSoup
from selenium import webdriver
import time

Linksml=["https://articulo.mercadolibre.cl/MLC-554718846-audifonos-hyperx-cloud-ps4-_JM",
"https://articulo.mercadolibre.cl/MLC-554753668-disco-duro-ssd-_JM",
"https://articulo.mercadolibre.cl/MLC-554695355-celuar-samsung-j6-duos-_JM"]

driver = webdriver.Chrome()

for x in range(len(Linksml)):
    driver.get(Linksml[x])
    time.sleep(3)

    soup=BeautifulSoup(driver.page_source,'html.parser')
    tags=soup.find('h1', class_= 'item-title__primary')
    if tags: print(tags.text.strip())

driver.close()

Output:

Audifonos Hyperx Cloud Ps4
Disco Duro Ssd
Celuar Samsung J6+ Duos

Upvotes: 1

Related Questions