найти теги с частичным значением id с помощью BeautifulSoup - PullRequest
0 голосов
/ 14 апреля 2020

Вот пример моего html файла:

<div id="document_64219">
    <div id="part_12">
    <p class="keywords_HTAG">
        <strong> Key Words : </strong>
        <ol>
            <li>Decentralization</li>
            <li>Planning</li>
        </ol>
    </p>

Я хочу проанализировать каждый div с идентификатором, начинающимся с "part_", и получить его

.
myDivs = soup.findAll("div", id=re.compile("^brique_"))
myParagraph = myDivs.find_all_next("p", {"class" : "keywords_HTAG"})

for div in myDivs :
   for paragraph in myParagraph :
        li = paragraph.find_all_next("li")
        print (li)

Это мне ничего не возвращает. Я привык к Jsoup, который я нахожу достаточно интуитивным, но я не уверен, как это делается с BeautifulSoup.

Ответы [ 2 ]

0 голосов
/ 14 апреля 2020

Вы можете использовать селекторы * in css, которые соответствуют части значения в определенном атрибуте.

from bs4 import BeautifulSoup

html = """
<div id="document_64219">
    <div id="part_12">
    <p class="keywords_HTAG">
        <strong> Key Words : </strong>
        <ol>
            <li>first</li>
            <li>second</li>
        </ol>
    </p>
    <ol>
        <li>third</li>
        <li>fourth</li>
    </ol>
"""

soup = BeautifulSoup(html, 'html.parser')

# if you want the li with in the div with the id contains part_
# This will return first , second , third and fourth 
lis = [li.text for li in soup.select('div[id*="part_"] li')]
print('All li ==>',lis)


# if you want olny the li with in the div with the id contains part_ and with in the p tag only 
# This will return first , second
lis = [li.text for li in soup.select('div[id*="part_"] p.keywords_HTAG li')]
print('with in p only ==>',lis)


# if you want olny the li with in the div with the id contains part_ and without the li in the p tag  
# This will return third and fourth 
lis = [li.text for li in soup.select('div[id*="part_"] > ol  li')]
print('without li in p ==>',lis)

Выход:

All li ==> ['first', 'second', 'third', 'fourth']
with in p only ==> ['first', 'second']
without li in p ==> ['third', 'fourth']
0 голосов
/ 14 апреля 2020
import re
from bs4 import BeautifulSoup

html = """
<div id="document_64219">
    <div id="part_12">
    <p class="keywords_HTAG">
        <strong> Key Words : </strong>
        <ol>
            <li>Decentralization</li>
            <li>Planning</li>
        </ol>
    </p>
"""

soup = BeautifulSoup(html, 'html.parser')


for item in soup.findAll("div", id=re.compile("^part_")):
    print(item.find_all_next("li"))

Выход:

[<li>Decentralization</li>, <li>Planning</li>]

Или

for item in soup.findAll("div", id=re.compile("^part_")):
    print([item.text for item in item.find_all_next("li")])

Выход:

['Decentralization', 'Planning']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...