简体   繁体   中英

how to print a particular value from html 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'

Have you tried location.get_text() ?

You can go here and read more about it.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM