Hephaestion
Hephaestion

Reputation: 65

How can I scrape a <h1> tag using BeautifulSoup? [Python]

I am currently coding a price tracker for different websites, but I have run into an issue. I'm trying to scrape the contents of a h1 tag using BeautifulSoup4, but I don't know how. I've tried to use a dictionary, as suggested in https://stackoverflow.com/a/40716482/14003061, but it returned None. Can someone please help? It would be appreciated!

Here's the code:

from termcolor import colored
import requests
from bs4 import BeautifulSoup
import smtplib

def choice_bwfo():
    print(colored("You have selected Buy Whole Foods Online [BWFO]", "blue"))
    url = input(colored("\n[ 2 ] Paste a product link from BWFO.\n", "magenta"))
    url_verify = requests.get(url, headers=headers)
    soup = BeautifulSoup(url_verify.content, 'html5lib')

    item_block = BeautifulSoup.find('h1', {'itemprop' : 'name'})
    print(item_block)

choice_bwfo()

here's an example URL you can use:

https://www.buywholefoodsonline.co.uk/organic-spanish-bee-pollen-250g.html

Thanks :)

Upvotes: 1

Views: 1109

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

This script will print content of <h1> tag:

import requests
from bs4 import BeautifulSoup


url = 'https://www.buywholefoodsonline.co.uk/organic-spanish-bee-pollen-250g.html'

# create `soup` variable from the URL:
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

# print text of first `<h1>` tag:
print(soup.h1.get_text())

Prints:

Organic Spanish Bee Pollen 250g

Or you can do:

print(soup.find('h1', {'itemprop' : 'name'}).get_text())

Upvotes: 1

Related Questions