Skip to main content

How To Scrape MercadoLibre With Python And Beautiful Soup?

 

How To Scrape MercadoLibre With Python And Beautiful Soup

In this blog, you will come to know about how we can scrape MercadoLibre product data using Python and BeautifulSoup.

The blog aims is to be up-to-date and you will get every particular result in real-time.

First, you need to install Python 3. If not, you can just get Python 3 and get it installed before you proceed. Then you need to install beautiful soup with pip3 install beautifulsoup4.

We will require the library’s requests, soupsieve, and lxml to collect data, break it down to XML, and use CSS selectors. Install them using.

pip3 install requests soupsieve lxml

Once installed, open an editor and type in.

# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup

import requests

Now let’s go to the MercadoLibre search page and inspect the data we can get

This is how it looks.

How to Scrape MercadoLibre with Python and Beautiful Soup

Back to our code now. Let’s try and get this data by pretending we are a browser like this.

# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup

import requestsheaders = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9'}

url='https://listado.mercadolibre.com.mx/phone#D[A:phone]'

response=requests.get(url,headers=headers)

print(response)

Save this as scrapeMercado.py.

If you run it

python3 scrapeMercado.py

You will see the whole HTML page.

Now, let’s use CSS selectors to get to the data we want. To do that, let’s go back to Chrome and open the inspect tool. We now need to get to all the articles. We notice that class ‘.results-item.’ holds all the individual product details together.

How to Scrape MercadoLibre with Python and Beautiful Soup

If you notice that the article title is contained in an element inside the results-item class, we can get to it like this.

# -*- coding: utf-8 -*- from bs4 import BeautifulSoup import requests headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.11 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9', 'Accept-Encoding': 'identity' } #'Accept-Encoding': 'identity'url = 'https://listado.mercadolibre.com.mx/phone#D[A:phone]' response=requests.get(url,headers=headers) #print(response.content) soup=BeautifulSoup(response.content,'lxml') for item in soup.select('.results-item'): try: print('---------------------------') print(item.select('h2')[0].get_text()) except Exception as e: #raise e print('')

This selects all the pb-layout-item article blocks and runs through them, looking for the element and printing its text.

So when you run it, you get the product title

Now with the same process, we get the class names of all the other data like product image, the link, and price.

# -*- coding: utf-8 -*- from bs4 import BeautifulSoup import requests headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.11 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9', 'Accept-Encoding': 'identity' } #'Accept-Encoding': 'identity' url = 'https://listado.mercadolibre.com.mx/phone#D[A:phone]' response=requests.get(url,headers=headers) #print(response.content) soup=BeautifulSoup(response.content,'lxml') for item in soup.select('.results-item'): try: print('---------------------------') print(item.select('h2')[0].get_text()) print(item.select('h2 a')[0]['href']) print(item.select('.price__container .item__price')[0].get_text()) print(item.select('.image-content a img')[0]['data-src']) except Exception as e: #raise e print('')

What we run, should print everything we need from each product like this.

If you need to utilize this in production and want to scale to thousands of links, then you will get that you will get IP blocked rapidly by MercadoLibre. In this scenario, using a rotating proxy service to rotate IPs is a must. You can use a service like Proxies API to route your calls through a pool of millions of residential proxies.

If you need to scale the crawling speed and don’t want to set up your infrastructure, you can utilize our Cloud-based crawler by Web Screen Scraping to easily crawl thousands of URLs at high speed from our network of crawlers.

If you are looking for the best MercadoLibre with Python and Beautiful Soup, then you can contact Web Screen Scraping for all your requirements.

Comments

Popular posts from this blog

What Is The Impact Of Browser Fingerprints On Web Scraping?

  Web scraping is one of the most important aspects of delivering data to clients in a readable format. Since web scraping technology became popular, businesses and websites have become cautious about having their data scraped off the internet. As a result, businesses have discovered how to identify web crawlers and avoid having their data released. Many websites have created a variety of strategies to prevent data crawling or web scraping in the recent past. Although some of them are simple to hack, web scraping businesses may easily land on their websites and take data. The websites, on the other hand, have generated three identifiers that may be monitored using cookies, IP addresses, and fingerprints. You should be aware of how your system's IP address and cookies can be used to track it. However, one question must be asked, what is a browser fingerprint, and how does it prevent online scraping? Another approach employed by anti-scraping systems is to build a unique fingerprint ...

How An Amazon Dealer Can Be Benefitted With Web Scraping?

  Because of the growth of e-commerce stores as well as a progressively tech-savvy world, many dealers now get a chance to drastically improve their presence online as well as make a money-making business. Whereas Walmart and Amazon have chiefly dominated in the avenue, amongst others, online dealers mainly depend on these platforms for making increasing revenues using attractive online sales and deals. E-commerce has become more intelligent as well as targeted marketing. This big shift could be credited to the usage of Artificial Intelligence and Machine Learning in the bid for predicting the next huge shopping trends as well as influencing customer preferences. A huge amount of shoppers have moved to online shopping, as well as for that, the same has occurred with sellers and also who are creating their portfolios on different platforms like Flipkart, eBay, Amazon, Ali Baba, etc. Though, to convert typical online consumers into customers, e-commerce dealers require to use data an...

What Are The Benefits Of Web Scraping In The Healthcare Industry?

  Data breaches, insufficient information, and loss of records are some issues in the industry. Now to understand and solve this problem, old methods or methods with the latest touch can be used. Healthcare is one of those industries where there is a lot of data available but little attention is given to the solution of the same. The healthcare industry has maximum data but nobody is working on it with complete interest. Separating data manually on a large scale is almost impossible and too hard. So scrape Healthcare data automatically by using  web scraping services  that will help the industry as a whole and eliminate errors. Benefits of Web Scraping In the Healthcare Industry Web scraping is the best tool that can assist you in collecting health care data and eliminate all the errors of large-scale extraction. Web scraping can also assist the healthcare industry in several ways, such as: Extracting Essential Information There is a treasure of data in the healthcare ind...