【发布时间】:2020-10-09 16:52:31
【问题描述】:
我有一个任务,我可以做的一件事是找到网页的前 3 个句子并显示它。查找网页文本很容易,但我无法弄清楚如何找到前 3 个句子。
import requests
from bs4 import BeautifulSoup
url = 'https://www.troyhunt.com/the-773-million-record-collection-1-data-reach/'
res = requests.get(url)
html_page = res.content
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
output = ''
blacklist = [
'[document]',
'noscript',
'header',
'html',
'meta',
'head',
'input',
'script'
]
for t in text:
if (t.parent.name not in blacklist):
output += '{} '.format(t)
tempout = output.split('.')
for i in range(tempout):
if (i >= 3):
tempout.remove(i)
output = '.'.join(tempout)
print(output)
【问题讨论】:
-
检查我的答案是否满足你的要求。
标签: python html list beautifulsoup