首页 文章

从标记中检索内容

提问于
浏览
0

在我以前的一篇文章中,我能够检索所有p标签

import bs4
from urllib.request import  urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url='https://www.centralpark.com/things-to-do/central-park-zoo/polar-bears/'
# opening up connection
uClient = uReq(my_url)
page_html = uClient.read()
# close connection
uClient.close()
page_soup = soup(page_html, features="html.parser")

ps=list(page_soup.find_all('p'))

for s in ps:
    print(s)

我想要的是检索那些p标签内的任何内容 . 例如:

ex1='<p> this is example </p>' -> I want res1 = 'this is example' 
ex2='<p> this is <strong> nice </strong> example </p>' -> I want res2 = 'this is nice example' 
ex3='<p> this is <b> okeyish </b> example </p>' -> I want res3 = 'this is okeyish example'

所有结果(res1,res2,res3)可以转到List .

我已经搜索了解决方案,但解决方案建议只适用于一种类型的标签example . 我想要的只是检索p和/ p之间的所有内容,无论其他标签出现在哪里 . 如果那些其他标签有内容,那么也应该包含这些内容 .

1 回答

  • 1
    ps=page_soup.find_all('p')
    
    results = []
    for s in ps:
        #print(s.text)
        results = results.append(s.text)
    

相关问题