如何使用BeautifulSoup4(Python 2.7)获取<a href=“url”>没有类或ID的链接

2024-04-20 02:05:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力抓取一个不包含任何类或id的标记。它只是a href,然后是链接

html代码-有更多,但这只是它的一小部分。我试图抓取a href=“url is here”,但我不能只抓取“a”,因为它会抓取页面上的每个链接

<table>
<tbody>
<tr class="">
<td class="col1 align">
<a href="url is here">
1
</a>
</td>
<td class="col2">
<a href="www.example.com">
<img class="avatar" src="www.example.com" alt="le me">
le me
<img class="test" alt="test" title="test"    src="test-icon.png">
</a>
</td>
<td class="col3 align">
<a href="www.example.com">
2,715
</a>
</td>
<td class="col4 align">
<a href="www.example.com">
5,400,000,000
</a>
</td>
</tr>

我的代码:

source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.findAll():
    username = link.get()
    print(username)

我没有把这些填进去,因为我试过的东西都不管用。不知道还能做什么


Tags: 代码texttestcomurlhereis链接