Created
May 7, 2025 07:27
-
-
Save sayeed205/411cad77bce813d226c62a6f80e71e7c to your computer and use it in GitHub Desktop.
Scrapes "Car Cover" listings from OLX India, saving title, price, location, and link to a CSV file. Skips incomplete listings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import requests | |
| from bs4 import BeautifulSoup | |
| # URL for OLX search for "Car Cover" | |
| url = "https://www.olx.in/items/q-car-cover" | |
| # Send GET request with user-agent to avoid blocking | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | |
| } | |
| response = requests.get(url, headers=headers) | |
| # Check if the request was successful | |
| if response.status_code == 200: | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| # Find the list of items using data-aut-id | |
| listings = soup.find("ul", {"data-aut-id": "itemsList1"}) | |
| if listings: | |
| results = [] | |
| for item in listings.find_all("li"): | |
| title = item.find("span", {"data-aut-id": "itemTitle"}) | |
| price = item.find("span", {"data-aut-id": "itemPrice"}) | |
| location = item.find("span", {"data-aut-id": "item-location"}) | |
| link_tag = item.find("a") | |
| # Skip the item if any required field is missing | |
| if not (title and price and location and link_tag): | |
| continue | |
| title = title.text.strip() | |
| price = price.text.strip() | |
| location = location.text.strip() | |
| link = "https://www.olx.in" + link_tag.get("href", "") | |
| results.append([title, price, location, link]) | |
| # Save results to CSV | |
| with open( | |
| "olx_car_cover_results.csv", "w", newline="", encoding="utf-8" | |
| ) as file: | |
| writer = csv.writer(file) | |
| writer.writerow(["Title", "Price", "Location", "Link"]) | |
| writer.writerows(results) | |
| print("Scraping completed. Results saved to olx_car_cover_results.csv") | |
| else: | |
| print("No listings found.") | |
| else: | |
| print(f"Failed to connect. Status code: {response.status_code}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment