如何获取包含html元素的列表内的链接?

时间:2019-05-19 12:56:48

标签: python beautifulsoup

我有这样的代码

import bs4
import requests

page = requests.get('https://nicmusic.net/category/%D8%A2%D9%87%D9%86%DA%AF- 
    %D9%87%D8%A7%DB%8C-%D8%A7%D8%AD%D8%B3%D8%A7%D9%86-        
    %D8%AE%D9%88%D8%A7%D8%AC%D9%87-%D8%A7%D9%85%DB%8C%D8%B1%DB%8C/')

soup = bs4.BeautifulSoup(page.content, 'html.parser')
button = soup.findAll('a', 'more')
button_link = ([x['href'] for x in button])
the_list = ([bs4.BeautifulSoup(requests.get(z).content,'html.parser').findAll("div", {"class": "dlbox"}) for z in button_link])
    print(the_list)

输出是这样

[[<div class="dlbox">
<a href="http://dl.nicmusic.net/nicmusic/021/030/Ehsan%20Khajeh%20Amiri%20-%20Bayad%20Bargasht.mp3" rel="external nofollow" target="_blank" title="دانلود">دانلود آهنگ - ( کیفیت عالی 320 )</a>
<a href="http://dl.nicmusic.net/nicmusic/021/030/Ehsan%20Khajeh%20Amiri%20-%20Bayad%20Bargasht%20-%20128.mp3" rel="external nofollow" target="_blank" title="دانلود">دانلود آهنگ - ( کیفیت خوب 128 )</a>
<p> </p>
<audio controls="controls" preload="none">
<source src="http://dl.nicmusic.net/nicmusic/021/030/Ehsan%20Khajeh%20Amiri%20-%20Bayad%20Bargasht%20-%20128.mp3" type="audio/mpeg"/>
</audio>
</div>], 
[<div class="dlbox">
<a href="http://dl.nicmusic.net/nicmusic/020/088/10%20Sahneh.mp3" rel="external nofollow" target="_blank" title="دانلود">دانلود آهنگ - ( کیفیت عالی 320 )</a>
<a href="http://dl.nicmusic.net/nicmusic/020/088/10%20Sahneh%20128.mp3" rel="external nofollow" target="_blank" title="دانلود">دانلود آهنگ - ( کیفیت خوب 128 )</a>
<p> </p>
<audio controls="controls" preload="none">
<source src="http://dl.nicmusic.net/nicmusic/020/088/10%20Sahneh%20128.mp3" type="audio/mpeg"/>
</audio>

问题是,如何获得此列表中每个元素的第一个链接?

3 个答案:

答案 0 :(得分:0)

我会在列表的每个元素上使用正则表达式。

import re

for elt in list:
    match = re.search(r'href=[\'"]?([^\'" >]+)', elt)
    if match:
        print match.group(0)

答案 1 :(得分:0)

您似乎在寻找地址中的第一个“ .mp3”链接。 我使用波纹管链接在specific link中查找所有“ .txt”文件:

Downloading files from web using Python

import requests 
from bs4 import BeautifulSoup 
archive_url="https://github.com/PedramNavid/trump_speeches/blob/master/data/"  
def get_Text_links():       
    # create response object 
    r = requests.get(archive_url)       
    # create beautiful-soup object 
    soup = BeautifulSoup(r.content,'html5lib') 
    # find all links on web-page 
    links = soup.findAll('a') 
    # filter the link sending with .mp4 
    Text_links = [archive_url + link['href'] for link in links if   link['href'].endswith('txt')] 
    return Text_links
get_Text_links()

答案 2 :(得分:0)

您可以使用request.Session对象来提高效率和以操作符结尾的属性

from bs4 import BeautifulSoup as bs
import requests

with requests.Session() as s:
    page = s.get('https://nicmusic.net/category/%D8%A2%D9%87%D9%86%DA%AF-%D9%87%D8%A7%DB%8C-%D8%A7%D8%AD%D8%B3%D8%A7%D9%86-%D8%AE%D9%88%D8%A7%D8%AC%D9%87-%D8%A7%D9%85%DB%8C%D8%B1%DB%8C/')
    soup = bs(page.content, 'html.parser')
    links = [item['href'] for item in soup.select('.more')]

    for link in links:
        soup = bs(s.get(link).text, 'html.parser')
        downloads = [item['href'] for item in soup.select('[href$=".mp3"]')]
        if downloads: print(downloads[0])