【发布时间】: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