2016-11-23 3 views
1

私はウェブサイトの一部の情報を掻き集めるためのウェブスクレイパーを書いていますJW Pepper楽譜データベース。私はこれを行うためにBeautifulSoupとPythonを使用しています。beatifulsoupによって生成されたリンクのpythonリストから特定の項目をフィルタリングする

# a barebones program I created to scrape the description and audio file off the JW pepper website, will eventually be used in a music database 
import urllib2 
import re 
from bs4 import BeautifulSoup 
linkgot = 0 
def linkget(): 
    search = "http://www.jwpepper.com/sheet-music/search.jsp?keywords=" # this is the url without the keyword that comes up when searching something 
    print("enter the name of the desired piece") 
    keyword = raw_input("> ") # this will add the keyword to the url 
    url = search + keyword 
    page = urllib2.urlopen(url) 
    soup = BeautifulSoup(page) 
    all_links = soup.findAll("a") 
    link_dict = [] 
    item_dict = [] 
    for link in all_links: 
     link_dict.append(link.get('href')) # adds a list of the the links found on the page to link_dict 
    item_dict.append(x for x in link_dict if '.item' in x) #sorts them occording to .item 
    print item_dict 

linkget() 

"印刷" コマンドが返すこの:[0x10ec6dc80で>]、私はそれをグーグルとき何も返さない

は、ここに私のコードです。

答えて

0

あなたのリストのフィルタリングは間違っていました。あなたの助けのための

/Festival-of-Carols/4929683.item 
/Festival-of-Carols/4929683.item 
/Festival-of-Carols/4929683.item 
... 
+0

ありがとう:

from bs4 import BeautifulSoup import urllib2 def linkget(): search = "http://www.jwpepper.com/sheet-music/search.jsp?keywords=" # this is the url without the keyword that comes up when searching something print("enter the name of the desired piece") keyword = raw_input("> ") # this will add the keyword to the url url = search + keyword page = urllib2.urlopen(url) soup = BeautifulSoup(page, "html.parser") link_dict = [] item_dict = [] for link in soup.findAll("a", href=True): href = link.get('href') link_dict.append(href) # adds a list of the the links found on the page to link_dict if '.item' in href: item_dict.append(href) for href in item_dict: print href linkget() 

はあなたのようなものを与える:次のように.itemが存在する場合はむしろ別々のループ内フィルタよりも、あなただけのリストを構築することができます。私はまだ多くのことをフィルタで学ぶように見えます。 –

+0

あなたは大歓迎です!答えとして灰色の目盛りをクリックすることを忘れないでください(そしてバッジを取得してください)。 –

関連する問題