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>
誰能一個告訴我什麼不妥之處,如何解決呢?
謝謝。有效。我希望我可以放棄你的答案,但我現在沒有足夠的聲望。 –
但是爲什麼它沒有處理請求? –
沒問題。 「處理請求」是什麼意思?您可以[將答案標記爲已接受](http://stackoverflow.com/help/someone-answers)與您的聲譽:) –