如何用python提取html数据?

时间:2018-05-28 01:03:35

标签: python html extract

    <td><img src="/images/cflags/png/id1.png" alt="Indonesia" title="Indonesia"></td>
                                <td></td>
                                <td>link.here/python.php
                                </td>
                                <td>Linux</td>
<td><img src="/images/cflags/png/id2.png" alt="Indonesia" title="Indonesia"></td>
                            <td></td>
                            <td>link2.here/python.php
                            </td>
                            <td>Linux</td>
<td><img src="/images/cflags/png/id3.png" alt="Indonesia" title="Indonesia"></td>
                            <td></td>
                            <td>link3.here/python.php
                            </td>
                            <td>Linux</td>

这是一个代码示例,我想使用python提取链接,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用BeautifulSoup

如果您的所有链接都以php结尾,则可以执行以下操作:

>>> from bs4 import BeautifulSoup
>>> text = '''<td><img src="/images/cflags/png/id1.png" alt="Indonesia" title="Indonesia"></td>
...                                 <td></td>
...                                 <td>link.here/python.php
...                                 </td>
...                                 <td>Linux</td>
... <td><img src="/images/cflags/png/id2.png" alt="Indonesia" title="Indonesia"></td>
...                             <td></td>
...                             <td>link2.here/python.php
...                             </td>
...                             <td>Linux</td>
... <td><img src="/images/cflags/png/id3.png" alt="Indonesia" title="Indonesia"></td>
...                             <td></td>
...                             <td>link3.here/python.php
...                             </td>
...                             <td>Linux</td>'''
>>> soup = BeautifulSoup(text, 'html.parser')
>>> [url.text.strip() for url in soup.find_all('td') if url.text.strip().endswith('php')]
['link.here/python.php', 'link2.here/python.php', 'link3.here/python.php']