1

Beautiful Soup 4 を使用してページをスクレイピングしています。不要なテキスト ブロックがあります。

<p class="MsoNormal" style="text-align: center"><b>
                            <span lang="EN-US" style="font-family: Arial; color: blue">
                            <font size="4">1 </font></span>
                            <span lang="AR-SA" dir="RTL" style="font-family: Arial; color: blue">
                            <font size="4">&#1600;</font></span><span lang="EN-US" style="font-family: Arial; color: blue"><font size="4"> 
                            с&#1199;р&#1241; фати&#1211;&#1241;</font></span></b></p>

特徴的なのは、タグが付いていることです。私はすでに findall() を使用してすべてを取得しました

タグ。だから今、私は次のようなループを持っています:

for el in doc.findall('p'):
    if el.hasChildTag('b'):
        break;

残念ながらbs4には「hasChildTag」機能がありません

4

2 に答える 2

4

css セレクターも使用できるはずです。

http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors

soup.select("p b")
于 2013-01-13T19:17:51.020 に答える
3
for elem in soup.findAll('p'):
    if elem.findChildren('b'):
        continue #skip the elem with "b", and continue with the loop
    #do stuff with the elem
于 2013-01-13T19:16:29.933 に答える