Skip to main content
 首页 » 编程设计

python 美汤 : How to extract text next to a tag

2025年02月15日8tintown

我有以下 HTML

<p> 
<b>Father:</b> Michael Haughton 
<br> 
<b>Mother:</b> Diane 
<br><b>Brother:</b>  
Rashad Haughton<br> 
<b>Husband:</b> <a href="/people/540/000024468/">R. Kelly</a> (m. 1994, annulled that same year) 
<br><b>Boyfriend:</b> <a href="/people/420/000109093/">Damon Dash</a> (Roc-a-Fella co-CEO)<br></p> 

我必须将标题和文本分开,例如,母亲:Diane..

所以最后我会有一个字典列表:

[{"label":"Mother","value":"Diane"}] 

我正在尝试以下但没有工作:

def parse(u): 
    u = u.rstrip('\n') 
    r = requests.get(u, headers=headers) 
    if r.status_code == 200: 
        html = r.text.strip() 
        soup = BeautifulSoup(html, 'lxml') 
        headings = soup.select('table p') 
        for h in headings: 
            b = h.find('b') 
            if b is not None: 
                print(b.text) 
                print(h.text + '\n') 
                print('=================================') 
 
 
url = 'http://www.nndb.com/people/742/000024670/' 

请您参考如下方法:

from bs4 import BeautifulSoup 
from urllib.request import urlopen 
 
#html = '''<p> 
#<b>Father:</b> Michael Haughton 
#<br> 
#<b>Mother:</b> Diane 
#<br><b>Brother:</b>  
#Rashad Haughton<br> 
#<b>Husband:</b> <a href="/people/540/000024468/">R. Kelly</a> (m. 1994, annulled that same year) 
#<br><b>Boyfriend:</b> <a href="/people/420/000109093/">Damon Dash</a> (Roc-a-Fella co-CEO)<br></p>''' 
 
page = urlopen('http://www.nndb.com/people/742/000024670/') 
source = page.read() 
 
soup = BeautifulSoup(source) 
 
needed_p = soup.find_all('p')[8] 
 
bs = needed_p.find_all('b') 
 
res = {} 
 
for b in bs: 
    if b.find_next('a').text: 
        res[b.text] = b.find_next('a').text.strip().strip('\n') 
    if b.next_sibling != ' ': 
        res[b.text] = b.next_sibling.strip().strip('\n') 
 
res 

输出:

{'Brother:': 'Rashad Haughton', 
 'Mother:': 'Diane', 
 'Husband:': 'R. Kelly', 
 'Father:': 'Michael Haughton', 
 'Boyfriend:': 'Damon Dash'} 

编辑: 有关页面顶部的其他信息:

... (code above) ... 
soup = BeautifulSoup(source) 
 
needed_p = soup.find_all('p')[1:4] + [soup.find_all('p')[8]] # here explicitly selecting needed p-tags for further parsing 
 
res = {} 
 
for p in needed_p: 
    bs = p.find_all('b') 
    for b in bs: 
        if b.find_next('a').text: 
            res[b.text] = b.find_next('a').text.strip().strip('\n') 
        if b.next_sibling != ' ': 
            res[b.text] = b.next_sibling.strip().strip('\n') 
 
res 

输出:

{'Race or Ethnicity:': 'Black', 
 'Husband:': 'R. Kelly', 
 'Died:': '25-Aug', 
 'Nationality:': 'United States', 
 'Executive summary:': 'R&B singer, died in plane crash', 
 'Mother:': 'Diane', 
 'Birthplace:': 'Brooklyn, NY', 
 'Born:': '16-Jan', 
 'Boyfriend:': 'Damon Dash', 
 'Sexual orientation:': 'Straight', 
 'Occupation:': 'Singer', 
 'Cause of death:': 'Accident - Airplane', 
 'Brother:': 'Rashad Haughton', 
 'Remains:': 'Interred,', 
 'Gender:': 'Female', 
 'Father:': 'Michael Haughton', 
 'Location of death:': 'Marsh Harbour, Abaco Island, Bahamas'} 

对于正是这个页面,你也可以抓取高中,例如,这样:

res['High School'] = soup.find_all('p')[9].text.split(':')[1].strip()