Как получить текст из таблицы html? - PullRequest
0 голосов
/ 04 мая 2020

У меня есть html:

<table class= "tb1">
<thead>
<tr>
<th width="100">Country,<br>Other</br></th>
<th width="20">Total<br>Customers</br></th>
<th width="30">New<br>Customers</br></th>
<th width="30">Tests/<br/>
<nobr>1M cases</nobr>
</th>
<th style="display:none" width="30">Continent</th>
</tr>
</thead>
</table>

Я использую xpath для получения текстов из каждой строки.

'//table[@class="tb1"]//thead//tr//th/text()'

Результат:

['Country,', 'Other', 'Total', 'Customers', 'New', 'Customers', 'Tests/', '\n    ', '\n    ', 'Continent']

Желаемый результат:

['Country,Other', 'TotalCustomers', 'NewCustomers', 'Tests/1M cases', 'Continent']

Я пытался использовать:

'string(//table[@class="tb1"]//thead//tr//th)'

Но результат просто:

Country,Other

Ответы [ 2 ]

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

Я бы использовал BeautifulSoup4:

pip install beautifulsoup4

Это возьмет вашу таблицу и вернет список для каждой строки; заголовок или данные

from bs4 import BeautifulSoup


html_text = '''<table class= "tb1">
<thead>
    <tr>
        <th width="100">Country,<br>Other</br></th>
        <th width="20">Total<br>Customers</br></th>
        <th width="30">New<br>Customers</br></th>
        <th width="30">Tests/<br/>
            <nobr>1M cases</nobr>
        </th>
        <th style="display:none" width="30">Continent</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Country1</td>
        <td>20</td>
        <td>3</td>
        <td>1</td>
        <td>Europe</td>
    </tr>
    <tr>
        <td>Country2</td>
        <td>15</td>
        <td>1</td>
        <td>3</td>
        <td>North America</td>
    </tr>
</tr>

</table>'''

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

def get_table():
    table = []
    for tr in soup.find_all('tr'):
        # get headers
        th = tr.find_all('th')
        # get rows
        td = tr.find_all('td')
        # listify and combine them (just in case the html is structured weird somehow)
        row = [i.text for i in th] + [i.text for i in td]
        # append the new list to the table list
        table.append(row)
    return table

print(get_table())

Вывод:

[['Country,Other', 'TotalCustomers', 'NewCustomers', 'Tests/\n1M cases\n', 'Continent'], ['Country1', '20', '3', '1', 'Europe'], ['Country2', '15', '1', '3', 'North America']]

Вы также можете составить список словарей с заголовками в качестве ключей и данными в качестве значений, с которыми проще работать в python .

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

Сначала используйте xpath для получения каждого <th>, а затем используйте for -l oop для получения './/text()' в каждом <th>, а затем вы можете очистить (ie. Удалить «новую строку») ) и объедините элементы, чтобы создать одну строку для каждого <th>

import lxml.html

html ='''
<table class= "tb1">
<thead>
<tr>
<th width="100">Country,<br>Other</br></th>
<th width="20">Total<br>Customers</br></th>
<th width="30">New<br>Customers</br></th>
<th width="30">Tests/<br/>
<nobr>1M cases</nobr>
</th>
<th style="display:none" width="30">Continent</th>
</tr>
</thead>
</table>
'''

soup = lxml.html.fromstring(html)

results = []

for th in soup.xpath('//th'):
    text = ''.join(x.strip() for x in th.xpath('.//text()'))
    #text = ''.join(x.strip() for x in th.itertext())
    results.append(text)

print(results)
...