gpt4 book ai didi

python - Beautifulsoup 分解()

转载 作者:太空狗 更新时间:2023-10-30 00:43:30 27 4
gpt4 key购买 nike

我正在尝试摆脱 <script>标签和标签内的内容使用 beatifulsoup。我去看了文档,似乎是一个非常简单的调用函数。有关该功能的更多信息是 here .这是我到目前为止解析的 html 页面的内容...

<body class="pb-theme-normal pb-full-fluid">
<div class="pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links" id="wp-adb-c" style="width: 1px !important;
height: 1px !important;
position: absolute !important;
left: -10000px !important;
top: -1000px !important;
">
</div>
<div id="pb-f-a">
</div>
<div class="" id="pb-root">
<script>
(function(a){
TWP=window.TWP||{};
TWP.Features=TWP.Features||{};
TWP.Features.Page=TWP.Features.Page||{};
TWP.Features.Page.PostRecommends={};
TWP.Features.Page.PostRecommends.url="https://recommendation-hybrid.wpdigital.net/hybrid/hybrid-filter/hybrid.json?callback\x3d?";
TWP.Features.Page.PostRecommends.trackUrl="https://recommendation-hybrid.wpdigital.net/hybrid/hybrid-filter/tracker.json?callback\x3d?";
TWP.Features.Page.PostRecommends.profileUrl="https://usersegment.wpdigital.net/usersegments";
TWP.Features.Page.PostRecommends.canonicalUrl=""
})(jQuery);

</script>
</div>
</body>

假设您有一些类似的网络内容,并且您将其保存在名为 soup_html 的 BeautifulSoup 对象中.如果我运行 soup_html.script.decompose()他们称对象为soup_html脚本标签仍然存在。我怎样才能摆脱 <script>以及这些标签中的内容?

markup = 'The html above'
soup = BeautifulSoup(markup)
html_body = soup.body

soup.script.decompose()

html_body

最佳答案

soup.script.decompose()

这只会从“Soup”中删除单个脚本元素。相反,我认为你打算分解所有这些:

for script in soup("script"):
script.decompose()

关于python - Beautifulsoup 分解(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39885359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com