Answer the question
In order to leave comments, you need to log in
How to get href value of 'a' tag using BeautifulSoup Python?
Help with the following task: there is an html page that contains two links to two images, respectively. These links are exactly what you need to get. I'm trying the following code:
def parse(html):
soup = BeautifulSoup(html,'html.parser')
title = soup.find('h1')
image1 = soup.find('div', {'class': 'text'}).find('a').get('href')
image2 = soup.find_all('a', class_='highslide')[1]
post = []
post.append ({
'title': title.text,
'image1': image1,
'image2': image2,
})
print(post)
Answer the question
In order to leave comments, you need to log in
Thank you all for your help! Resolved my issue like this:
def parse(html):
soup = BeautifulSoup(html,'html.parser')
title = soup.find('h1')
image1 = soup.find_all('a', class_='highslide')[0]
image2 = soup.find_all('a', class_='highslide')[1]
post = []
post.append ({
'title': title.text,
'image1': image1.get('href'),
'image2': image2.get('href'),
})
print(post)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question