Python美丽的汤解析每个Div中的第一个Href

时间:2018-05-18 21:44:29

标签: python beautifulsoup

给出以下代码:

# import the module
import bs4 as bs
import urllib.request
import re

masterURL = 'http://www.metrolyrics.com/top100.html'
sauce = urllib.request.urlopen(masterURL).read()
soup = bs.BeautifulSoup(sauce,'lxml')




for div in soup.findAll('ul', {'class': 'song-list'}):
    for span in div:
        for link in span:
            for a in link:
                print(a)

我可以解析多个div,我得到如下结果:

enter image description here

我的问题是,我不是要获取div的全部内容,而是如何只返回突出显示的部分,即Href的URL?

2 个答案:

答案 0 :(得分:1)

试试这个。您需要指定正确的class来获取与其相关的网址。

from bs4 import BeautifulSoup
import urllib.request

masterURL = 'http://www.metrolyrics.com/top100.html'
sauce = urllib.request.urlopen(masterURL).read()
soup = BeautifulSoup(sauce,'lxml')

for div in soup.find_all(class_='subtitle'):
    print(div.get("href"))

输出:

http://www.metrolyrics.com/charles-goose-lyrics.html
http://www.metrolyrics.com/param-singh-lyrics.html
http://www.metrolyrics.com/westlife-lyrics.html
http://www.metrolyrics.com/luis-fonsi-lyrics.html
http://www.metrolyrics.com/grease-lyrics.html
http://www.metrolyrics.com/shanti-dope-lyrics.html

等等---

答案 1 :(得分:0)

if 'href' in a.attrs:
     a.attrs['href']

这将为您提供所需。