簡體   English   中英

如何從 html 內容中打印特定值?

[英]how to print a particular value from html content?

下面是 HTML 內容我想要 HTML 內容中唯一可用的值

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

我的代碼是 -

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

為此,我需要 output 使用 python -

'Ram', '010101010', '第 5 車道,kamathipura,kamathipura'

您是否嘗試過location.get_text()

您可以在此處閱讀 go 並閱讀更多相關信息。

使用標簽和 class 標識符,您可以獲取所需區域內的所有內容。 然后使用內容索引,您應該能夠 select 您希望這樣的確切內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM