我正在尝试编写一个可以重复使用的函数,以便从搜刮的元素中去除空白。我正在搜刮
h2
、
li
和
p
标签;它们目前被返回为
<tag> string </tag>
,我想去除空白并使用
*.get_text(strip=True)
保存内容。
h_content = soup.select('h2')
将存储所有发现的
h2
标签。
p_content = soup.select('p')
将存储所有找到的
p
标签。
以此类推。
I have been trying this but am not sure how to return the items to the original location, that is to say, return them here -->
*_content
def remove_whitespace(tags):
for item in tags:
item.get_text(strip=True)
return item
最理想的情况是最终得到一个我可以重用的函数。
remove_whitespace(*_content)