|
| 1 | +from bs4 import BeautifulSoup |
| 2 | +import requests |
| 3 | +import openpyxl |
| 4 | + |
| 5 | + |
| 6 | +def extract_brand_name_and_title(name): |
| 7 | + # Split the name and return the first word as the brand name and the rest as title |
| 8 | + brand, title = name.split(' ', 1) |
| 9 | + return brand, title |
| 10 | + |
| 11 | + |
| 12 | +def scrape_graphics_cards_data(): |
| 13 | + try: |
| 14 | + # Create a new Excel workbook and set up the worksheet |
| 15 | + excel = openpyxl.Workbook() |
| 16 | + sheet = excel.active |
| 17 | + sheet.title = "price" |
| 18 | + sheet.append(['Brand', 'Name', 'Price']) |
| 19 | + |
| 20 | + url = 'https://www.techlandbd.com/pc-components/graphics-card?sort=p.price&order=ASC&fq=1&limit=100' |
| 21 | + response = requests.get(url) |
| 22 | + response.raise_for_status() |
| 23 | + |
| 24 | + # Parse the HTML content |
| 25 | + soup = BeautifulSoup(response.text, 'html.parser') |
| 26 | + |
| 27 | + # Find all product cards on the webpage |
| 28 | + cards = soup.find('div', class_='main-products product-grid').find_all( |
| 29 | + 'div', class_='product-layout has-extra-button') |
| 30 | + |
| 31 | + for card in cards: |
| 32 | + # Extract the product name |
| 33 | + name = card.find('div', class_='name').a.text |
| 34 | + |
| 35 | + # Split the name to get the brand and title |
| 36 | + brand, title = extract_brand_name_and_title(name) |
| 37 | + |
| 38 | + # Extract the product price |
| 39 | + price = card.find('div', class_='price').span.text |
| 40 | + |
| 41 | + # Print the product details and add them to the Excel sheet |
| 42 | + print(brand, title, price) |
| 43 | + sheet.append([brand, title, price]) |
| 44 | + |
| 45 | + # Save the Excel file |
| 46 | + excel.save('Graphics Card.xlsx') |
| 47 | + |
| 48 | + except Exception as e: |
| 49 | + print("An error occurred:", e) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + # Call the main scraping function |
| 54 | + scrape_graphics_cards_data() |
0 commit comments