masroor_rajput
masroor_rajput

Reputation: 5

how do i grab first link from out put

`enter code here`
from bs4 import BeautifulSoup
import requests

url = "https://www.tutorialspoint.com/index.htm"
req = requests.get(url)
soup = BeautifulSoup(req.text, "html.parser")

for link in soup.find_all('a'):
    print(str(link.get('href')))

this is the out put

https://www.tutorialspoint.com/index.htm https://www.tutorialspoint.com/codingground.htm https://www.tutorialspoint.com/about/about_careers.htm

  1. i need to know how do i grab first link

    https://www.tutorialspoint.com/index.htm

Upvotes: -3

Views: 26

Answers (2)

Hanna
Hanna

Reputation: 1146

you can use find instead it only gets the first element

link = soup.find('a').get('href')

Upvotes: 0

Buddy Bob
Buddy Bob

Reputation: 5889

Just index the list.

links = soup.find_all('a')[0].get('href')

out

https://www.tutorialspoint.com/index.htm

Upvotes: 0

Related Questions