我刮类似于下面的HTML数据:排除在Beautifulsoup基于内容标签
<div class="target-content">
<p id="random1">
"the content of the p"
</p>
<p id="random2">
"the content of the p"
</p>
<p>
<q class="semi-predictable">
"q tag content that I don't want
</q>
</p>
<p id="random3">
"the content of the p"
</p>
</div>
我的目标是让所有的<p>
标签,与他们一起的内容,同时能够排除<q>
标签及其内容。目前,我让所有的<p>
标签有以下方法:
contentlist = soup.find('div', class_='target-content').find_all('p')
我的问题,之后我发现结果集所有<p>
标签的,我怎么能过滤掉单<p>
,连同它的内容,包含<q>
?
注:正从soup.find('div', class_='target-content')find_all('p')
的结果集后,我反复地增加从结果以下列方式设置为列表中的每个<p>
:
content = ''
for p in contentlist:
content += str(p)
谢谢,这正是我试图理解。谢谢你的解释;我不认为像使用Beautifulsoup那样经常使用CSS选择器。 – theeastcoastwest