在我之前的一篇文章中,我能夠檢索所有 p 標簽import bs4from urllib.request import urlopen as uReqfrom bs4 import BeautifulSoup as soupmy_url='https://www.centralpark.com/things-to-do/central-park-zoo/polar-bears/'# opening up connectionuClient = uReq(my_url)page_html = uClient.read()# close connectionuClient.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。我已經搜索了解決方案,但建議的解決方案只適用于一種類型的標簽示例。我想要的只是檢索 p 和 /p 之間的所有內容,無論它們之間出現哪些其他標簽。如果那些其他標簽有內容,那些也應該包括在內。
1 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
ps=page_soup.find_all('p')
results = []
for s in ps:
#print(s.text)
results = results.append(s.text)
添加回答
舉報
0/150
提交
取消