Answer the question
In order to leave comments, you need to log in
How to get first href from div?
Like from
<div class="dots-item"><a href="/index.php?r=crop/harvest&id=12245218"><img alt="Z" height="50" src="https://static/light/crops/gas_station.png" width="50"/></a><a href="/index.php?r=crop/harvest&id=12245218"><img alt="Harvest" class="crop_ico" src="https://static/i/../light/dots/st/dot-st-ready.png"/></a></div>
url = '/index.php?r=crop/harvest&id=12245218'
divs = [<div class="dots-item"><a href="/index.php?r=crop/harvest&id=12245218"><img alt="z" height="50" src="https://static.hata.mobi/light/crops/gas_station.png" width="50"/></a><a href="/index.php?r=crop/harvest&id=12245218"><img alt="Harvest" class="crop_ico" src="https://static/i/../light/dots/st/dot-st-ready.png"/></a></div>, <div class="dots-item"><a href="/index.php?r=crop/harvest&id=12245219"><img alt='zz"' height="50" src="https://static/light/crops/dining.png" width="50"/></a><a href="/index.php?r=crop/harvest&id=12245219"><img alt="Harvest" class="crop_ico" src="https://static/i/../light/dots/st/dot-st-ready.png"/></a></div>]
divs['href']
Answer the question
In order to leave comments, you need to log in
Works on your piece of code. Checked :)
If this answer helped you, mark it as solved.
#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
html = '<div class="dots-item"><a href="/index.php?r=crop/harvest&id=12245218"><img alt="Z" height="50" src="https://static/light/crops/gas_station.png" width="50"/></a><a href="/index.php?r=crop/harvest&id=12245218"><img alt="Harvest" class="crop_ico" src="https://static/i/../light/dots/st/dot-st-ready.png"/></a></div>'
soup = BeautifulSoup(html, 'html.parser')
href = soup.find('div', attrs = {'class':'dots-item'}).find('a').get('href')
print(href)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question