92325


code：

[code]

    blok[0].find_all('span')
    
[/code]

out

[code]

    [<span>(11/20 11:46)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 11:04)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 10:49)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 10:45)</span>,
     <span>(11/20 08:58)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 07:43)</span>,
    
[/code]


CC BY-SA 4.0
2022-11-20T05:04:44.790
92324
2022-11-21T08:23:23.440
2022-11-20T10:25:57.663
43025
55506
question
python web-scraping beautifulsoup
pythonのwebスクレイピングでタグのclassが入っていないタグを収集するには


そのサンプルなら HTML パーサーを使わずにただ文字列正規表現で検索するだけでよさそう

[code]

    import re
    
    blok = ['''
    <span>(11/20 11:46)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 11:04)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 10:49)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 10:45)</span>
    <span>(11/20 08:58)</span>
    <span class="PhotoIcon"></span>,
    <span>(11/20 07:43)</span>
    ''']
    print(re.findall('<span>.*</span>', blok[0]))
    
[/code]

実行結果

[code]

    ['<span>(11/20 11:46)</span>', '<span>(11/20 11:04)</span>', '<span>(11/20 10:49)</span>', '<span>(11/20 10:45)</span>', '<span>(11/20 08:58)</span>', '<span>(11/20 07:43)</span>']
    
[/code]


CC BY-SA 4.0
2022-11-20T06:02:59.940
92325
2022-11-20T06:02:59.940
92324
answer


たぶん BeautifulSoup かと思うので

[code]

    In [1]: from bs4 import BeautifulSoup
       ...:
       ...: html = '''<block>
       ...:  <span>(11/20 11:46)</span>,
       ...:  <span class="PhotoIcon"></span>,
       ...:  <span>(11/20 11:04)</span>,
       ...:  <span class="PhotoIcon"></span>,
       ...:  <span>(11/20 10:49)</span>,
       ...:  <span class="PhotoIcon"></span>,
       ...:  <span>(11/20 10:45)</span>,
       ...:  <span>(11/20 08:58)</span>,
       ...:  <span class="PhotoIcon"></span>,
       ...:  <span>(11/20 07:43)</span>,
       ...: '''
       ...:
    
    In [2]: soup = BeautifulSoup(html)
       ...: block = soup.block
       ...: block.find_all('span')
       ...:
    Out[2]:
    [<span>(11/20 11:46)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 11:04)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 10:49)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 10:45)</span>,
     <span>(11/20 08:58)</span>,
     <span class="PhotoIcon"></span>,
     <span>(11/20 07:43)</span>]
    
    In [3]: block('span', class_=True)
    Out[3]:
    [<span class="PhotoIcon"></span>,
     <span class="PhotoIcon"></span>,
     <span class="PhotoIcon"></span>,
     <span class="PhotoIcon"></span>]
    
    In [4]: block('span', class_=False)
    Out[4]:
    [<span>(11/20 11:46)</span>,
     <span>(11/20 11:04)</span>,
     <span>(11/20 10:49)</span>,
     <span>(11/20 10:45)</span>,
     <span>(11/20 08:58)</span>,
     <span>(11/20 07:43)</span>]
    
    In [5]:
    
[/code]

* * *

**追記**  
ちなみに `block('span', class_=False)` は  
`block.find_all('span', class_=False)` の記述でも可能 (短縮してるだけ)


CC BY-SA 4.0
2022-11-20T06:27:32.690
92326
2022-11-20T10:35:51.497
2022-11-20T10:35:51.497
43025
43025
92324
answer


> class が入っていないタグを収集する

CSS セレクタの場合。

[code]

    from bs4 import BeautifulSoup
    from pprint import pprint
    
    html = '''
    <span>(11/20 11:46)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 11:04)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 10:49)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 10:45)</span>
    <span>(11/20 08:58)</span>
    <span class="PhotoIcon"></span>
    <span>(11/20 07:43)</span>
    '''
    soup = BeautifulSoup(html, 'html.parser')
    elms = soup.select('span:not([class])')
    pprint(elms)
    
    #
    # [<span>(11/20 11:46)</span>,
    #  <span>(11/20 11:04)</span>,
    #  <span>(11/20 10:49)</span>,
    #  <span>(11/20 10:45)</span>,
    #  <span>(11/20 08:58)</span>,
    #  <span>(11/20 07:43)</span>]
    
[/code]


CC BY-SA 4.0
2022-11-20T06:45:32.913
92328
2022-11-20T06:45:32.913
47127
92324
answer


わかりやすい回答、あありがとうございました。

sopu.find_all('div',True)でするのは、とても良かったです。


CC BY-SA 4.0
2022-11-21T08:12:13.807
92345
2022-11-21T08:12:13.807
55506
92324
answer


pythonのwebスクレイピングで不要タグを出力しない方法を教えてください。  
例  
IN：blok.find_all('li')  
out： **< a href="/articles/ASQCP5DZ2QCPULFA00L.html" _> 第２次補正予算案の基金、過去最高の8.9兆円
無駄の温床と批判も_**

0 0

,


CC BY-SA 4.0
2022-11-21T08:23:23.440
92346
2022-11-21T08:23:23.440
55506
92324
answer