【问题标题】:Extract text from html tag从html标签中提取文本
【发布时间】:2021-05-05 15:58:50
【问题描述】:
<li itemprop="foundingLocation" itemscope="" itemtype="https://schema.org/Place"><i class="icon-location"></i><span itemprop="address" itemscope="" itemtype="https://schema.org/PostalAddress">India, Delhi, Delhi</span></li>
<li><i class="icon-phone text-success"></i><a class="link visit-website-tracking" data-container="body" data-content="+91 9643861253" data-info="phone:triazine-software-pvt-ltd" data-placement="top" data-toggle="popover" rel="nofollow" role="button">Show phone number</a></li>
<li><i class="icon-office"></i>Founded: <span itemprop="foundingDate">2015</span></li>
<li itemprop="numberOfEmployees" itemscope="" itemtype="https://schema.org/QuantitativeValue"><i class="icon-users"></i><span itemprop="value">50-100</span> employees</li>
<li><i class="icon-budget"></i>Avg. budget: $10k-$15k (USD)</li>
<li><i class="icon-hourly"></i>Hourly fee: $25/h (USD)</li>

我需要提取

India, Delhi, Delhi
+91 9643861253
2015
50-100
Avg. budget: $10k-$15k (USD)
Hourly fee: $25/h (USD)

我怎样才能进一步完成这项任务?

我的代码

from urllib.request import urlopen
from bs4 import BeautifulSoup

url = "https://www.appfutura.com/developers/triazine-software-pvt-ltd"
html = urlopen(url).read()
soup = BeautifulSoup(html,"lxml")
class_list = ["developer-description"] # can add any other classes to this list.
Title = soup.find('h1',{"class":"big-title no-mar-top no-mar-bot strong"})
Info = soup.find('ul',{"class":"list-inline no-mar"})
for i in range(len(Info)):
    print(Info.contents[i])
    soup = BeautifulSoup(Info.contents[i],"lxml")
    Title = soup.find('i',{"class":"icon-budget"})
    print(Title.contents)

【问题讨论】:

    标签: python web-scraping beautifulsoup html-parsing


    【解决方案1】:

    试试这个:

    from urllib.request import urlopen
    
    from bs4 import BeautifulSoup
    
    html = urlopen("https://www.appfutura.com/developers/triazine-software-pvt-ltd").read()
    soup = BeautifulSoup(html, "lxml").select_one('.profile .list-inline').find_all("li")
    
    info = [i.getText() for i in soup if not i.getText().startswith("Show")]
    phone = "".join(i.find("a")["data-content"] for i in soup if i.find("a"))
    
    info.insert(1, phone)
    print("\n".join(info))
    
    

    输出:

    India, Delhi, Delhi
    +91 9643861253
    Founded: 2015
    50-100 employees
    Avg. budget: $10k-$15k (USD)
    Hourly fee: $25/h (USD)
    
    

    【讨论】:

    • 感谢但不适用于电话号码..用于提取数据内容中的电话号码。有没有具体的方法?如果你能帮忙?@baduker !!
    • 我已经编辑了我的答案。别忘了stackoverflow.com/help/someone-answers
    猜你喜欢
    • 2021-10-22
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多