<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提取链接,任何人都可以帮助我吗?
答案 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']