1

私はpythonが初めてです。私がやりたいことは、今年のグラストンベリー フェスティバルで発表されたすべてのバンドを、python と美しいスープを使用して抽出することです。すべてのバンドをテキスト ファイルにダンプし、最終的に各アーティストのトップ トラックに基づいて Spotify プレイリストを作成したいと考えています。

www.efestivals.co.uk/festivals/glastonbury/2013/lineup.shtml#から抽出したいアーティストのリスト(実際には、[金曜日] タブではなく [AZ] タブに表示したい)

最初に端末にバンドを出力しようとしましたが、空白の結果が得られます。これが私が試したものです

from bs4 import BeautifulSoup
import urllib2

#efestivals page with all glastonbury acts
url = "http://www.efestivals.co.uk/festivals/glastonbury/2013/lineup.shtml#"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

bands = soup.findAll('a')
for eachband in bands:
   print eachband.string

基本的に、AZ タブにアクセスし、各バンドを抽出するには助けが必要です。私も確認済みのバンド( が付いているものimg src="/img2009/lineup_confirmed.gif")だけ欲しいです。私はhtmlにあまり詳しくありませんが、これは妥当な出発点のようです。

4

3 に答える 3

1

これには多くの方法があります。これはうまくいくように見える一例です:

from bs4 import BeautifulSoup
import urllib2 as ul

url = "http://www.efestivals.co.uk/festivals/glastonbury/2013/lineup.shtml#"
page = ul.urlopen(url)
soup = BeautifulSoup(page.read())

elements = soup.findAll('img', {'src': '/img2009/lineup_confirmed.gif'})

bands = [e.next_element.next_element.text for e in elements]

print bands[1:11]

出力:

[u'Arctic Monkeys', u'Dizzee Rascal', u'The Vaccines', u'Kenny Rogers']
于 2013-03-31T19:47:44.497 に答える
1

AZ テーブルから確認済みのバンドのリンクを抽出するには:

#!/usr/bin/env python
import re

try:
    from urllib2 import urlopen
except ImportError: # Python 3
    from urllib.request import urlopen

from bs4 import BeautifulSoup, NavigableString

def table_after_atoz(tag):
    '''Whether tag is a <table> after an element with id="LUA to Z".'''
    if tag.name == 'table' and 'TableLineupBox' in tag.get('class', ''):
        for tag in tag.previous_elements: # go back
            if not isinstance(tag, NavigableString): # skip strings
                return tag.get('id') == "LUA to Z"

def confirmed_band_links(soup):
    table = soup.find(table_after_atoz) # find A to Z table
    for tr in table.find_all('tr'): # find all rows (including nested tables)
        if tr.find('img', alt="confirmed"): # row with a confirmed band?
            yield tr.find('a', href=re.compile(r'^/festivals/bands')) # a link

def main():
    url = "http://www.efestivals.co.uk/festivals/glastonbury/2013/lineup.shtml"
    soup = BeautifulSoup(urlopen(url))
    for link in confirmed_band_links(soup):
        print("%s\t%s" % (link['href'], link.string))

main()
于 2013-03-31T20:08:55.963 に答える