Python BeautifulSoup4从h1标签内的标签获取字符串信息

时间:2014-09-04 14:29:42

标签: html5 python-3.x tags beautifulsoup web-crawler

我试图获取标签内的字符串信息,但是a标签位于h1标签内。

<h1 class="branded-page-header-title">
      <span class="qualified-channel-title ellipsized"><span class="qualified-channel-title-wrapper"><span dir="ltr" class="qualified-channel-title-text" ><a dir="ltr" href="/user/viralvideoslmao" class="spf-link branded-page-header-title-link yt-uix-sessionlink" title="ViralVideos" data-sessionlink="ei=lXIIVM-_CvKQigahpIHgDA"      >ViralVideos</a></span></span></span>
    </h1>

我希望在这种情况下的信息是&#39; ViralVideos&#39; a.t.m我有这个:

import requests
from bs4 import BeautifulSoup

def get_yt_links():
    url = "https://youtube.com"
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text)
    for code in soup.findAll('a'):
        href = "http://youtube.com" + code.get('href')
        if "channel/U" in href:
            get_user(href)
            print(href)

def get_user(url):
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text)
    for user in soup.findAll('h1', {'class': 'branded-page-header-title'}).a:
        print(user.string)

提前致谢

2 个答案:

答案 0 :(得分:0)

您现在遇到的问题是findAll()会返回结果列表,并且列表中没有a属性。

要获取a代码,您可以使用CSS selectors并检查h1a代码的类名:

soup = BeautifulSoup(data)
for link in soup.select('h1.branded-page-header-title a.branded-page-header-title-link'):
    print link.text  

对于您提供的HTML,它会打印ViralVideos

答案 1 :(得分:0)

只需将find_all更改为find

即可
soup = BeautifulSoup(plain_text)
print soup.find('h1', {'class': 'branded-page-header-title'}).a.text