2016-09-08 44 views
0

私はPythonでスクレイピングスクリプトを作成しています。私は最初に曲リストをスクラップしなければならない場所から映画のリンクを集めます。ここ は映画が同じ出力を与える同じPython関数

https://www.lyricsbogie.com/category/movies/a-flat-2010 https://www.lyricsbogie.com/category/movies/a-night-in-calcutta-1970 https://www.lyricsbogie.com/category/movies/a-scandall-2016 https://www.lyricsbogie.com/category/movies/a-strange-love-story-2011 https://www.lyricsbogie.com/category/movies/a-sublime-love-story-barsaat-2005 https://www.lyricsbogie.com/category/movies/a-wednesday-2008 https://www.lyricsbogie.com/category/movies/aa-ab-laut-chalen-1999 https://www.lyricsbogie.com/category/movies/aa-dekhen-zara-2009 https://www.lyricsbogie.com/category/movies/aa-gale-lag-jaa-1973 https://www.lyricsbogie.com/category/movies/aa-gale-lag-jaa-1994 https://www.lyricsbogie.com/category/movies/aabra-ka-daabra-2004 をリンク含むmovie.txtリストですここでは https://www.lyricsbogie.com/category/movies/aabroo-1956 https://www.lyricsbogie.com/category/movies/aabroo-1968 https://www.lyricsbogie.com/category/movies/aabshar-1953

私の最初のPythonの関数である:上記の機能の

import requests 
from bs4 import BeautifulSoup as bs 

def get_songs_links_for_movies1(): 
    url='https://www.lyricsbogie.com/category/movies/a-flat-2010' 
    source_code = requests.get(url) 
    plain_text = source_code.text 
    soup = bs(plain_text,"html.parser") 
    for link in soup.find_all('h3',class_='entry-title'): 
     href = link.a.get('href') 
     href = href+"\n" 
     print(href) 

出力:

https://www.lyricsbogie.com/movies/a-flat-2010/pyar-itna-na-kar.html 
https://www.lyricsbogie.com/movies/a-flat-2010/chal-halke-halke.html 
https://www.lyricsbogie.com/movies/a-flat-2010/meetha-sa-ishq.html 
https://www.lyricsbogie.com/movies/a-flat-2010/dil-kashi.html 
https://www.lyricsbogie.com/movies/ae-dil-hai-mushkil-2016/ae-dil-hai-mushkil-title.html 
https://www.lyricsbogie.com/movies/m-s-dhoni-the-untold-story-2016/kaun-tujhe.html 
https://www.lyricsbogie.com/movies/raaz-reboot-2016/raaz-aankhein-teri.html 
https://www.lyricsbogie.com/albums/akira-2016/baadal-2.html 
https://www.lyricsbogie.com/movies/baar-baar-dekho-2016/sau-aasmaan.html 
https://www.lyricsbogie.com/albums/gajanan-2016/gajanan-title.html 
https://www.lyricsbogie.com/movies/days-of-tafree-2016/jeeley-yeh-lamhe.html 
https://www.lyricsbogie.com/tv-shows/coke-studio-pakistan-season-9-2016/ala-baali.html 
https://www.lyricsbogie.com/albums/piya-2016/piya-title.html 
https://www.lyricsbogie.com/albums/sach-te-supna-2016/sach-te-supna-title.html 

それが正常に指定されたリンクの曲のURLをフェッチ。 しかし、私はプロセスを自動化しようとし、ファイルを渡すとmovie.txtは1つずつURLを読んで結果を得るが、その出力は私が自分で1つずつURLを追加する上記の関数と一致しない。また、この関数は曲のURLを取得しません。 正常に機能しない機能があります。 ..........

import requests 
from bs4 import BeautifulSoup as bs 

def get_songs_links_for_movies(): 
    file = open("movie.txt","r") 
    for url in file: 
     source_code = requests.get(url) 
     plain_text = source_code.text 
     soup = bs(plain_text,"html.parser") 
     for link in soup.find_all('h3',class_='entry-title'): 
      href = link.a.get('href') 
      href = href+"\n" 
      print(href) 

上記関数の出力

https://www.lyricsbogie.com/movies/ae-dil-hai-mushkil-2016/ae-dil-hai-mushkil-title.html 
https://www.lyricsbogie.com/movies/m-s-dhoni-the-untold-story-2016/kaun-tujhe.html 
https://www.lyricsbogie.com/movies/raaz-reboot-2016/raaz-aankhein-teri.html 
https://www.lyricsbogie.com/albums/akira-2016/baadal-2.html 
https://www.lyricsbogie.com/movies/baar-baar-dekho-2016/sau-aasmaan.html 
https://www.lyricsbogie.com/albums/gajanan-2016/gajanan-title.html 
https://www.lyricsbogie.com/movies/days-of-tafree-2016/jeeley-yeh-lamhe.html 
https://www.lyricsbogie.com/tv-shows/coke-studio-pakistan-season-9-2016/ala-baali.html 
https://www.lyricsbogie.com/albums/piya-2016/piya-title.html 
https://www.lyricsbogie.com/albums/sach-te-supna-2016/sach-te-supna-title.html 
https://www.lyricsbogie.com/movies/ae-dil-hai-mushkil-2016/ae-dil-hai-mushkil-title.html 
https://www.lyricsbogie.com/movies/m-s-dhoni-the-untold-story-2016/kaun-tujhe.html 
https://www.lyricsbogie.com/movies/raaz-reboot-2016/raaz-aankhein-teri.html 
https://www.lyricsbogie.com/albums/akira-2016/baadal-2.html 
https://www.lyricsbogie.com/movies/baar-baar-dekho-2016/sau-aasmaan.html 
https://www.lyricsbogie.com/albums/gajanan-2016/gajanan-title.html 
https://www.lyricsbogie.com/movies/days-of-tafree-2016/jeeley-yeh-lamhe.html 
https://www.lyricsbogie.com/tv-shows/coke-studio-pakistan-season-9-2016/ala-baali.html 
https://www.lyricsbogie.com/albums/piya-2016/piya-title.html 
https://www.lyricsbogie.com/albums/sach-te-supna-2016/sach-te-supna-title.html 
https://www.lyricsbogie.com/movies/ae-dil-hai-mushkil-2016/ae-dil-hai-mushkil-title.html 
https://www.lyricsbogie.com/movies/m-s-dhoni-the-untold-story-2016/kaun-tujhe.html 
https://www.lyricsbogie.com/movies/raaz-reboot-2016/raaz-aankhein-teri.html 
https://www.lyricsbogie.com/albums/akira-2016/baadal-2.html 
https://www.lyricsbogie.com/movies/baar-baar-dekho-2016/sau-aasmaan.html 
https://www.lyricsbogie.com/albums/gajanan-2016/gajanan-title.html 
https://www.lyricsbogie.com/movies/days-of-tafree-2016/jeeley-yeh-lamhe.html 
https://www.lyricsbogie.com/tv-shows/coke-studio-pakistan-season-9-2016/ala-baali.html 
https://www.lyricsbogie.com/albums/piya-2016/piya-title.html 
https://www.lyricsbogie.com/albums/sach-te-supna-2016/sach-te-supna-title.html 

ので、第1機能出力と第2の関数の出力を比較することによって。関数1がフェッチし、関数2が同じ出力を何度も何度も繰り返すという曲urlがないことがはっきり分かります。

誰でも私がなぜそれが起こっているのを助けることができます。

for url in file: 
    print(repr(url)) 
    ... 

印刷この表現(だけではなく、文字列)の特別参照することが容易になります:

+0

あり「https://www.lyricsbogie.com/category/ movies/a-flat-2010 'の行をmovie.txtに挿入しますか? –

+0

あなたの問題が何であるか分かりません: *最初の関数では、1つのURLからURLを取得し、出力に複数のURLを取得しています。 * 2番目の関数では、同じ構造を使用しますが、複数のURLを入力として使用するので、出力としてさらに多くのURLが得られますか? それは間違っていますか、または期待していませんでしたか? –

+0

はい、あなたは正しいですが、入力として複数のURLを渡す場合、正しい出力が得られません。 –

答えて

1

あなたはforループでファイルから読み込むURLの表現を印刷することができ、何が起こっているかを理解するために文字。この場合、出力は 'https://www.lyricsbogie.com/category/movies/a-flat-2010\n'となりました。ご覧のとおり、URLに改行があるため、取得されたURLは正しくありません。

たとえば、urlurl.rstrip()に置き換えることによって、改行文字を削除する方法を使用します(rstrip())。

-1

私はあなたがこのコードをテストすることができ、確かに、あなたのファイルは、単一の行として読まれていないことを確信している:

import requests 
from bs4 import BeautifulSoup as bs 

def get_songs_links_for_movies(url): 
    print("##Getting songs from %s" % url) 
    source_code = requests.get(url) 
    plain_text = source_code.text 
    soup = bs(plain_text,"html.parser") 
    for link in soup.find_all('h3',class_='entry-title'): 
     href = link.a.get('href') 
     href = href+"\n" 
     print(href) 

def get_urls_from_file(filename): 
    with open(filename, 'r') as f: 
    return [url for url in f.readlines()] 

urls = get_urls_from_file("movie.txt") 
for url in urls: 
    get_songs_links_for_movies(url) 
関連する問題