skip to Main Content

below is HTML content I want the only value that is available in HTML content

    <div class="list-group-item">
     <div class="row">
      <div class="col" style="min-width: 0;">
       <h2 class="h5 mt-0 text-truncate">
        <a class="text-warning" href="www.example.com">
         Ram
        </a>
       </h2>
       <p class="mob-9 text-truncate">
        <small>
         <i class="fa fa-fw fa-mobile-alt">
         </i>
         Contact:
        </small>
        010101010
       </p>
       <p class="mb-2 text-truncate">
        <small>
         <i class="fa fa-fw fa-map-marker-alt">
         </i>
         Location:
        </small>
        5th lane, kamathipura, Kamathipura
       </p>
        </a>
       </p>
      </div>
     </div>
    </div>

my code is –

import pandas as pd
import requests
from bs4 import BeautifulSoup as soup
url = requests.get("www.example.com")
page_soup = soup(url.content, 'html.parser')
name = shop.findAll("div", {"class": "list-group-item"})
print(name.h2.text)
number = shop.findAll("p", {"class": "fa fa-fw fa-map-marker-alt"})
print(?)
location = shop.findAll("p", {"class": "fa fa-fw fa-map-marker-alt"})
print(?)

I need output for this by using python –

‘Ram’, ‘010101010’, ‘5th lane, kamathipura, Kamathipura’

2

Answers


  1. Have you tried location.get_text() ?

    You can go here and read more about it.

    Login or Signup to reply.
  2. Using the tags and class identifiers, you can grab all contents within the regions you want. Then with content indicies you should be able to select the exact content you wish like this:

    from bs4 import BeautifulSoup
    url = 'myhtml.html'
    with open(url) as fp:
        soup = BeautifulSoup(fp, 'html.parser')
        contnt1 = [soup.find('a').contents[0].replace(' ','').replace('n','')]
        contnt2 = [x.contents[2].replace(' ', '').replace('n', '') for x in soup.find_all("p", "text-truncate")]
        print(*(contnt1 + contnt2))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search