获取href标记中的链接

时间:2017-07-04 14:32:46

标签: html python-2.7 beautifulsoup

如何获取href标签中的链接?我如何编码它似乎整个' a'标签... 代码:

page = urllib2.urlopen('https://www.meetup.com/')
soup = BeautifulSoup(page, 'lxml')

categories = soup.find('ul', class_='gridList')

A = []
B = []

for category in categories.findAll('li'):
    text = category.findAll('h4')
    if len(text) != 0:
        A.append(text[0].find(text = True))

for link in categories.findAll('li'):
    url = link.findAll('a', href=True)
    if len(url) != 0:
        B.append(url)

1 个答案:

答案 0 :(得分:0)

...
(your code above)    
for link in categories.findAll('li'):
    url = link.find('a', href=True)
    if len(url) != 0:
        B.append(url['href'])