Cant drop the<strong> tag

Viewed 39

I have been trying to scrape the website and cant seem to drop the tag from the results. I have tried .text and .get_text.Both not giving me any results

Below is the code :

excel=openpyxl.Workbook()

sheet = excel.active
sheet.title = "Coffee Data-India"
sheet.append(["product name","roast profile","price"])
website = requests.get("https://bluetokaicoffee.com/collections/coffee?cmp_id=16960797521&adg_id=140939617812&kwd=&device=c&gclid=Cj0KCQjwidSWBhDdARIsAIoTVb0H_6Zq4saXczwiZAtbSdQwyT_evp74MQXxI9upPMSh9vC-UW8eOeEaAmjuEALw_wcB")

website.raise_for_status()
soup = BeautifulSoup(website.content,"html.parser")
coffees = soup.find_all("div",class_="details")
for coffee in coffees:
    name = coffee.find("strong").text
    roast = coffee.find("div",class_="r-prop").find("div",class_="roast-section").find("strong")
    price = coffee.find('h6',class_="pd-price txt-of").text
    print(name,roast,price)
    sheet.append([name,roast,price])
    excel.save("Coffee-Indian.xls")
1 Answers

It seems that some items don't have <strong> tag, so you have to check that. For example:

import requests
from bs4 import BeautifulSoup

website = requests.get(
    "https://bluetokaicoffee.com/collections/coffee?cmp_id=16960797521&adg_id=140939617812&kwd=&device=c&gclid=Cj0KCQjwidSWBhDdARIsAIoTVb0H_6Zq4saXczwiZAtbSdQwyT_evp74MQXxI9upPMSh9vC-UW8eOeEaAmjuEALw_wcB"
)

website.raise_for_status()
soup = BeautifulSoup(website.content, "html.parser")
coffees = soup.find_all("div", class_="details")
for coffee in coffees:
    name = coffee.find("strong").text
    roast = (
        coffee.find("div", class_="r-prop")
        .find("div", class_="roast-section")
        .find("strong")
    )

    roast = roast.text if roast else "-"

    price = coffee.find("h6", class_="pd-price txt-of").text
    print("{:<50} {:<20} {}".format(name, roast, price))

Prints:

Customised Sampler Pack                            -                    From Rs. 490
Unakki Coffee Collective | Debut Series            -                    Rs. 800
Producer Series 2022 Lot 1 - Snakehead Naturals    light                Rs. 550
Yelnoorkhan Estate                                 medium               From Rs. 540
Kalledevarapura Pulp Sun Dried                     medium               From Rs. 540
Loyola Estate                                      medium dark          From Rs. 510
Monsoon Malabar AA- Hoysala Estate                 medium dark          From Rs. 540
Harley Estate (Pichia)                             medium               From Rs. 650
Howdia Estate                                      light                From Rs. 510
Hardoor Estate                                     medium               From Rs. 490
Baarbara Estate                                    medium dark          From Rs. 510
Thogarihunkal Estate                               medium               From Rs. 490
Sandalwood Estate                                  medium dark          From Rs. 490
South Indian Filter Blend                          medium dark          From Rs. 440
Silver Oak Cafe Blend                              medium               From Rs. 470
Attikan Estate                                     medium dark          From Rs. 490
French Roast                                       dark                 From Rs. 470
Vienna Roast                                       dark                 From Rs. 470
Cold Brew Blend Bold                               medium dark          From Rs. 490
Cold Brew Blend Light                              light                From Rs. 490
CASCARA                                            -                    Rs. 250
Related