Web Scraping iterator taking only elements with odd index

Asked

Viewed 61 times

-1

was making Webscraping from a sales page of cars and for some reason while iterating the data to collect the Mileage data round the iterator simply repeated the items, ie : 27000 27000 48000 48000 1000 1000

I wonder how I only get the values with Odd index in a Python iterator

soup = soup.find('div',class_="nm-features-container") 

for item in soup.findAll('div',class_="nm-features nm-km"):
    print(item.get_text().replace('\n','').replace(' ',''))

url = https://busca.autoline.com.br/comprar/carros/novos-seminovos-usados/todos-os-estados/todas-as-cidades/todas-as-marcas/todos-os-modelos/todas-as-versoes/todos-os-anos/todas-as-cores/todos-os-precos/

  • Important you [Dit] your post and explain in detail the part that is having problem, describing what you tried and where is the current difficulty, preferably with a [mcve]. Studying the post available on this link can make a very positive difference in your use of the site: Stack Overflow Survival Guide in English

1 answer

1


The problem is that you have 2 Divs with class nm-features-container for each element. The right one then would be for you to be more specific about which div you want. Looking at HTML, there is a div that has a second class (nm-automaker-list-view). Filtering by 2, it is possible to pick up only one element.

Example of scraping:

import requests
import bs4

URL = 'https://busca.autoline.com.br/comprar/carros/novos-seminovos-usados/todos-os-estados/todas-as-cidades/todas-as-marcas/todos-os-modelos/todas-as-versoes/todos-os-anos/todas-as-cores/todos-os-precos/'

response = requests.get(URL)
soup = bs4.BeautifulSoup(response.content)

containers = soup.find_all('div', attrs={'class': "nm-features-container nm-automaker-list-view"}) 
for container in containers:
    km = container.find('div', attrs={'class': 'nm-features nm-km '}).get_text().strip()
    city = container.find('div', attrs={'class': 'nm-city-tooltip'}).get_text().strip()

    print (km, city)
>> 27.000 Frederico Westphalen, RS
>> 50.000 Três Passos, RS
>> 120.000 Três Passos, RS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.