Home > database >  Is there a way to extract only the string of a paragraph when webscraping in python?
Is there a way to extract only the string of a paragraph when webscraping in python?

Time:03-10

We want to know if there is a way to extract only the string of a paragraph when web scraping in python?

The problem lies in our definition of 'price'

Anastacia <3

Code:

import requests
from bs4 import BeautifulSoup

def scraper(url):
    url.status_code
    url.headers
    c = url.content
    soup = BeautifulSoup(c, "html.parser")
    samples2 = soup.find_all(class_="product-price font-weight-bold mb-0")
    for p in samples2:
        price = (p)

    
        print(price)
        print()


url = "https://www.bevco.dk/spiritus/"
url = requests.get(url)
scraper(url)

CodePudding user response:

You only have to modify the variable price. Extract the text from between the tags and for esthetic purposes, strip the extracted string.

import requests
from bs4 import BeautifulSoup

def scraper(url):
    url.status_code
    url.headers
    c = url.content
    soup = BeautifulSoup(c, "html.parser")
    samples2 = soup.find_all(class_="product-price font-weight-bold mb-0")
    for p in samples2:
        price = (p)
        print(price.get_text().strip())
        print()


url = "https://www.bevco.dk/spiritus/"
url = requests.get(url)
scraper(url)

CodePudding user response:

import requests 
from bs4 import BeautifulSoup 
  
def getdata(url): 
    r = requests.get(url) 
    return r.text 
htmldata = getdata("https://www.bevco.dk/spiritus") 
soup = BeautifulSoup(htmldata, 'html.parser') 
data = '' 
for data in soup.find_all("p",{'class': 'product-price font-weight-bold mb-0'}): 
    print(data.get_text()) `enter code here`
  • Related