0

I got a result from BeautifulSoup, but instead of printing all links, I want to print the first three links:

for link in links and i in range(3):
    print link

Which is not correct. What should I do in this case?

4

2 回答 2

3

只需对列表进行切片,以便您仅获得前三个元素:

for link in links[:3]:
    print link
于 2013-09-19T07:59:03.810 回答
1

你不需要循环。简直了print links[:3]

编辑。另见 sberry 的评论

于 2013-09-19T07:59:37.877 回答