2016-09-08 4 views
0

Ich mache ein Scraping-Skript in Python. Ich sammle zuerst die Links des Films, von wo ich die Liederliste verschrotten muss. Hier ist die movie.txt Liste mit Filmen verbindenGleiche Python-Funktion, die unterschiedliche Ausgabe gibt

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 https://www.lyricsbogie.com/category/movies/aabroo-1943 https://www.lyricsbogie.com/category/movies/aabroo-1956 https://www.lyricsbogie.com/category/movies/aabroo-1968 https://www.lyricsbogie.com/category/movies/aabshar-1953

Hier ist meine erste Python-Funktion:

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) 

Ausgang der obigen Funktion:

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 

Es erfolgreich die Songs URL des angegebenen Link holt . Aber jetzt, wenn ich versuche, den Prozess zu automatisieren und übergibt eine Datei movie.txt, um URL nacheinander zu lesen und das Ergebnis zu erhalten, aber seine Ausgabe stimmt nicht mit der obigen Funktion überein, in der ich URL einzeln nacheinander hinzufüge. Auch diese Funktion bekommt die Lieder-URL nicht. Hier ist meine Funktion, die nicht richtig funktioniert.

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) 

Ausgang der obigen Funktion

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 

und so weiter ..........

Durch 1. Funktionsausgang und 2. Funktionsausgang verglichen wird. Sie sehen deutlich, dass es keine Lied-URL gibt, die Funktion 1 holt und auch Funktion 2, die dieselbe Ausgabe immer wieder wiederholt.

Kann mir jemand helfen, warum passiert das?

+0

wird ‚https://www.lyricsbogie.com/category/ Filme/a-Flat-2010 'Linie in der movie.txt? –

+0

Ich bin nicht sicher zu verstehen, was Ihr Problem ist: * Mit der ersten Funktion, holen Sie URLs von einer URL, und erhalten mehrere URLs in der Ausgabe. * Mit der zweiten Funktion verwenden Sie die gleiche Struktur, aber mit mehreren URLs als Eingabe, so dass Sie noch mehr URL als Ausgabe erhalten? Was ist daran falsch oder nicht erwartet? –

+0

Ja, Sie haben recht, aber wenn Sie mehrere URLs als Eingabe übergeben, gibt es keine richtige Ausgabe. –

Antwort

1

Um zu verstehen, was passiert ist, können Sie die Darstellung der URL aus der Datei in der for Schleife lesen drucken:

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

Druck dieser Darstellung (und nicht nur die Saite) besonders macht es leichter zu sehen Figuren. In diesem Fall ergab der Ausgang 'https://www.lyricsbogie.com/category/movies/a-flat-2010\n'. Wie Sie sehen, gibt es einen Zeilenumbruch in der URL, sodass die abgerufene URL nicht korrekt ist. Verwenden Sie zum Beispiel die Methode rstrip(), um das Zeilenumbruchzeichen zu entfernen, indem Sie url durch url.rstrip() ersetzen.

-1

Ich habe einen Zweifel daran, dass die Dateien nicht als eine einzige Zeile zu lesen, um sicher zu sein, können Sie diesen Code testen:

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) 
Verwandte Themen