2017-04-26 61 views
1

我正在寫一個Python腳本來使用請求模塊從azlyrics中獲取歌曲的歌詞。這是我寫的劇本:爲什麼我會爲此Python腳本獲取連接拒絕異常?

import requests, re 
from bs4 import BeautifulSoup as bs 
url = "http://search.azlyrics.com/search.php" 
payload = {'q' : 'shape of you'} 
r = requests.get(url, params = payload) 
soup = bs(r.text,"html.parser") 
try: 
    link = soup.find('a', {'href':re.compile('http://www.azlyrics.com/lyrics/edsheeran/shapeofyou.html')})['href'] 
    link = link.replace('http', 'https') 
    print(link) 
    raw_data = requests.get(link) 
except Exception as e: 
    print(e) 

,但我得到了一個異常,指出:

Max retries exceeded with url: /lyrics/edsheeran/shapeofyou.html (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fbda00b37f0>: Failed to establish a new connection: [Errno 111] Connection refused',)) 

我,我可能試圖發送太多的請求在互聯網上閱讀。所以我讓腳本睡了一段時間:

import requests, re 
from bs4 import BeautifulSoup as bs 
from time import sleep 
url = "http://search.azlyrics.com/search.php" 
payload = {'q' : 'shape of you'} 
r = requests.get(url, params = payload) 
soup = bs(r.text,"html.parser") 
try: 
    link = soup.find('a', {'href':re.compile('http://www.azlyrics.com/lyrics/edsheeran/shapeofyou.html')})['href'] 
    link = link.replace('http', 'https') 
    sleep(60) 
    print(link) 
    raw_data = requests.get(link) 
except Exception as e: 
    print(e) 

但沒有運氣!

所以我試圖用urllib.request裏

import requests, re 
from bs4 import BeautifulSoup as bs 
from time import sleep 
from urllib.request import urlopen 
url = "http://search.azlyrics.com/search.php" 
payload = {'q' : 'shape of you'} 
r = requests.get(url, params = payload) 
soup = bs(r.text,"html.parser") 
try: 
    link = soup.find('a', {'href':re.compile('http://www.azlyrics.com/lyrics/edsheeran/shapeofyou.html')})['href'] 
    link = link.replace('http', 'https') 
    sleep(60) 
    print(link) 
    raw_data = urlopen(link).read() 
except Exception as e: 
    print(e) 

相同,但隨後得到了不同的異常說明:

<urlopen error [Errno 111] Connection refused> 

誰能一個告訴我什麼不妥之處,如何解決呢?

回答

0

在您的網絡瀏覽器中試用它;當您嘗試訪問http://www.azlyrics.com/lyrics/edsheeran/shapeofyou.html時它可以正常工作,但是當您嘗試訪問https://www.azlyrics.com/lyrics/edsheeran/shapeofyou.html時,它將無法工作。

因此刪除您的link = link.replace('http', 'https')行,然後重試。

+0

謝謝。有效。我希望我可以放棄你的答案,但我現在沒有足夠的聲望。 –

+0

但是爲什麼它沒有處理請求? –

+0

沒問題。 「處理請求」是什麼意思?您可以[將答案標記爲已接受](http://stackoverflow.com/help/someone-answers)與您的聲譽:) –

相關問題