这是我的密码
SUSTAINABILITY = []
response = requests.get(URL, timeout=15)
page_src = response.text
SUSTAINABILITY.append(page_src.count("sustainability"))
我正在从response.text获得HTML,然后检查可持续性这个词出现了多少次。此代码正在运行,但我只想在body标记中计算单词“可持续性”。
如何从body标记中提取数据,然后计数()以查看单词“可持续性”发生了多少次?
发布于 2022-08-22 18:51:54
@得到一个好的建议。
from bs4 import BeautifulSoup
import requests
response = requests.get(URL, timeout=15)
# Make a "soup" from the response's text
soup = BeautifulSoup(response.text, 'html.parser')
# Take the <body> of HTML page as NavigableString (if I don't miss),
# convert it into string and count required string
print(str(soup.body).count("a"))
https://stackoverflow.com/questions/73449544
复制相似问题