【问题标题】:Extracting specific page links from a <a href tag using BeautifulSoup使用 BeautifulSoup 从 <a href 标记中提取特定页面链接
【发布时间】:2018-11-11 04:08:23
【问题描述】:

我正在使用 BeautifulSoup 提取此页面的所有链接:http://kern.humdrum.org/search?s=t&keyword=Haydn

我通过这种方式获取所有这些链接:

# -*- coding: utf-8 -*-

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'http://kern.humdrum.org/search?s=t&keyword=Haydn'

#opening up connecting, grabbing the page
uClient = uReq(my_url)

# put all the content in a variable
page_html = uClient.read()

#close the internet connection
uClient.close()

#It does my HTML parser
page_soup = soup(page_html, "html.parser")

# Grab all of the links
containers = page_soup.findAll('a', href=True)
#print(type(containers))

for container in containers:
    link = container
    #start_index = link.index('href="') 
    print(link)
    print("---")
    #print(start_index)

我的部分输出是:

请注意,它返回了几个链接,但我真的想要所有带有 >Someting 的链接。 (例如,">Allegro" 和 "Allegro vivace" 等等)。

我很难获得以下类型的输出(图像示例): "快板 - http://kern.ccarh.org/cgi-bin/ksdata?location=users/craig/classical/beethoven/piano/sonata&file=sonata01-1.krn&format=info"

换句话说,此时,我有一堆锚标签(+- 1000)。从所有这些标签中,有一堆只是“垃圾”和 +- 350 个我想提取的标签。所有这些标签看起来几乎相同,但唯一的区别是我需要的标签最后有一个“>某人的名字”。我想只提取具有此特征的所有锚标签的链接。

【问题讨论】:

  • 你需要用beautifulsoup吗?如果使用允许 xpath 表达式的 html 解析器,这会容易得多。见here
  • @bunji,我不需要使用它。我刚刚看到网上必须有人使用beautifulsoup,这就是我关注的原因。我会检查其他方法,谢谢。

标签: python beautifulsoup


【解决方案1】:

从图片中我可以看到,带有 info 的那些具有包含 format="info"href 属性,因此您可以使用 [href*=format="info"] 的属性=值 CSS 选择器,其中 * 表示 包含;属性值包含第一个等号之后的子字符串。

import bs4 , requests

res = requests.get("http://kern.humdrum.org/search?s=t&keyword=Haydn")
soup = bs4.BeautifulSoup(res.text,"html.parser")
for link in soup.select('[href*=format="info"]'):
    print(link.getText(), link['href'])

【讨论】:

    【解决方案2】:

    最好和最简单的方法是在打印链接时使用文本属性。像这样 : print link.text

    【讨论】:

      【解决方案3】:

      假设您已经有一个需要搜索的子字符串列表,您可以执行以下操作:

      for link in containers:
          text = link.get_text().lower()
          if any(text.endswith(substr) for substr in substring_list):
              print(link)
              print('---')
      

      【讨论】:

      • 这不能满足我的需要。我抓取页面的全部目的是尝试自动抓取所有链接。如果我过滤“allegro”,这将只有一个链接。
      • 不知道你想做什么。那个循环为我打印了一大堆锚标签。您能否提供有关您的问题的更多背景信息?
      • 当然。此时,我有一堆锚标签(+- 1000)。从所有这些标签中,有一堆只是“垃圾”和 +- 350 个我想提取的标签。所有这些标签看起来几乎相同,但唯一的区别是我需要的标签最后有一个“>某人的名字”。我想只提取具有此特征的所有锚标签的链接。
      • 也就是说,你需要找到所有文本以某个子字符串结尾的标签?还是您需要搜索多个不同的子字符串?
      • 我需要搜索多个子字符串。例如,它可能是“Allegro con brio”或“Presto”或......识别此标签的唯一方法是在标签的末尾我会有类似“某人的名字”(即“ > Presto")
      【解决方案4】:

      您想提取带有指定锚文本的链接吗?

      for container in containers:
          link = container
          # match exact
          #if 'Allegro di molto' == link.text:
          if 'Allegro' in link.text: # contain
              print(link)
              print("---")
      

      【讨论】:

        猜你喜欢
        • 2021-01-05
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-20
        • 1970-01-01
        相关资源
        最近更新 更多