简体   繁体   中英

Problem getting a certain element group from an html page using bs4

My main problem is targeting specific elements by 'a' tags with download links, now the only way I see it done is using the "title='Download'" in the page I'm trying to target. I have watched several questions with similar answers but did'nt find the thing I needed to solve this matter, any help would be much appreciated:

import urllib.request
import bs4 as bs

url = 'http://freemusicarchive.org/search/?quicksearch=drake/'
src = 'drake'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req).read()
soup = bs.BeautifulSoup(html, 'html.parser')


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

Now the main question is, is there any way to target ('get()') an element with the 'title' tag of "Download" for this matter and post the whole link?!

You seem to want to find all a tags with title attribute Download .

This can easily be achieved with find_all , that accepts an attributes-dict:

.
.
.

for download_a_tag in soup.find_all('a', {'title': 'Download'}):
    print(download_a_tag.get('href'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM