Home > Enterprise >  Scraping p tags and br tags, How Can I extract the text?
Scraping p tags and br tags, How Can I extract the text?

Time:02-22

Hi I have issue with scrapping with beautiful soup. I would like to have the text from :

url_example = 'https://www.verychic.fr/p/21/brunelleschi-hotel-s'

<p data-v-7816a06c="" >
      Piazza Santa Elisabetta 3<br data-v-7816a06c="">
      50 122<br data-v-7816a06c="">
      Florence<br data-v-7816a06c="">
      Italie
    </p> 

However when i try to use the code below :

    address  = (soup.find('p', attrs={'class': 'product-location'})).text

I have the following response/error from python: AttributeError: 'NoneType' object has no attribute 'text'

Could you me tell how to avoid the issue of NoneType in scraping with beautiful soup and "br" html tags.

What the error means , as the html element contains text ?

CodePudding user response:

address = soup.find('p', attrs={'class': 'product-location'}).text

try

CodePudding user response:

You can do that as follows:

from bs4 import BeautifulSoup
html_doc="""<p data-v-7816a06c="" >
      Piazza Santa Elisabetta 3<br data-v-7816a06c="">
      50 122<br data-v-7816a06c="">
      Florence<br data-v-7816a06c="">
      Italie
    </p> 
    """
soup = BeautifulSoup(html_doc, 'html.parser')
for e in soup.findAll('br'):
    p=e.replace_with('')
    #print(p)
address  = (soup.find('p', attrs={'class': 'product-location'})).text
print(address)

Output:

Piazza Santa Elisabetta 3
50 122
Florence
Italie
  • Related