Почему в beautifoulsount find_all отсутствует этот элемент при поиске, если текст содержит строку? - PullRequest
2 голосов
/ 30 мая 2020

У меня есть этот тест HTML страница

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>    <html lang="en">
    	<head>
    		<title></title>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<meta name="created" content="2020-05-29T11:12:00.0000000" />
    	</head>
    	<body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
    		<div id="div:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{216}" style="position:absolute;left:72px;top:43px;width:696px">
    			<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
    			<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
    			<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3</span> ###qweqeqwe</p>
    			<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
    			<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>
    		</div>
    	</body>
    </html>

После превращения вышеуказанного в суп, я делаю это

 for element in soup.find_all(["p"],text=re.compile("###")):
     print(element)

В приведенном выше примере печатаются следующие

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>    <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
    <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
    <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
    <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>

Почему пропускается соответствующий p Test3?

Обновление:
Андрей, ваш предложение работает, но меня озадачивает, потому что поиск
soup.find_all (["p"], text = re.compile ("###")) должен иметь такой же эффект. Это

 for p in soup.html.body.find_all("p"):
    print(p.text)

Test1 ### yrdy
Test2 ### qweqwe
Test3 ### qweqeqwe
Test4 ### sfsfsdfds
Test5 ### 121212

1 Ответ

0 голосов
/ 30 мая 2020

Test3 не имеет ### с тегом span, P, чтобы исправить это, используйте soup.select('p')

Или просто используйте for element in soup.find_all(['p']): для всех тегов p


Пример


import re

from bs4 import BeautifulSoup

html = """
    <html lang="en">
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="created" content="2020-05-29T11:12:00.0000000" />
        </head>
        <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
            <div id="div:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{216}" style="position:absolute;left:72px;top:43px;width:696px">
                <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
                <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3 </span> ###qweqeqwe</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>
            </div>
        </body>
    </html>
"""

soup = BeautifulSoup(html, features='html.parser')
for element in soup.select('p'):
    print(element)

Распечатывает все


<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3 </span> ###qweqeqwe</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>

Process finished with exit code 0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...