我试图用BS4从网站解析html内容。我得到了我的html片段,但我需要删除所有标签类,ID' s,等等。
例如:
<div class="applinks">
<div class="appbuttons">
<a href="https://geo.itunes.apple.com/ru/app/cloud-hub-file-manager-document/id972238010?mt=8&at=11l3Ss" rel="nofollow" target="_blank" title="Cloud Hub - File Manager, Document Reader, Clouds Browser and Download Manager">Загрузить</a>
<span onmouseout="jQuery('.wpappbox-8429dd98d1602dec9a9fc989204dbf7c .qrcode').hide();" onmouseover="jQuery('.wpappbox-8429dd98d1602dec9a9fc989204dbf7c .qrcode').show();">QR-Code</span>
</div>
</div>
我需要得到:
<div>
<div>
<a href="https://geo.itunes.apple.com/ru/app/cloud-hub-file-manager-document/id972238010?mt=8&at=11l3Ss" rel="nofollow" target="_blank" title="Cloud Hub - File Manager, Document Reader, Clouds Browser and Download Manager">Загрузить</a>
<span>QR-Code</span>
</div>
</div>
我的代码:
# coding: utf-8
import requests
from bs4 import BeautifulSoup
url = "https://lifehacker.ru/2016/08/29/app-store-29-august-2016/"
r = requests.get(url)
soup = BeautifulSoup(r.content)
post_content = soup.find("div", {"class","post-content"})
print post_content
如何删除所有代码属性?
答案 0 :(得分:3)
import requests
from bs4 import BeautifulSoup
url = "https://lifehacker.ru/2016/08/29/app-store-29-august-2016/"
r = requests.get(url)
soup = BeautifulSoup(r.content)
for tag in soup():
for attribute in ["class"]: # You can also add id,style,etc in the list
del tag[attribute]
答案 1 :(得分:0)
要从报废数据中的标记中删除所有属性:
import requests
from bs4 import BeautifulSoup
def CleanSoup(content):
for tags in content.findAll(True):
tags.attrs = {}
return content
url = "https://lifehacker.ru/2016/08/29/app-store-29-august-2016/"
r = requests.get(url)
soup = BeautifulSoup(r.content,"html.parser")
post_content = soup.find("div", {"class","post-content"})
post_content = CleanSoup(post_content)