Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我得到这个Python脚本的连接拒绝异常?_Python_Web Scraping_Beautifulsoup_Python Requests_Urllib - Fatal编程技术网

为什么我得到这个Python脚本的连接拒绝异常?

为什么我得到这个Python脚本的连接拒绝异常?,python,web-scraping,beautifulsoup,python-requests,urllib,Python,Web Scraping,Beautifulsoup,Python Requests,Urllib,我正在编写一个Python脚本,使用请求模块从AzLyps中获取歌曲的歌词。这是我写的剧本: 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.parse

我正在编写一个Python脚本,使用请求模块从AzLyps中获取歌曲的歌词。这是我写的剧本:

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',))
<urlopen error [Errno 111] Connection refused>
但是没有运气

因此,我对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)
但随后出现了不同的例外情况:

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',))
<urlopen error [Errno 111] Connection refused>


有谁能告诉我它出了什么问题以及如何修复它吗?

在您的web浏览器中尝试一下;当你尝试去拜访时,它会很好,但当你尝试去拜访时,它不会起作用


因此,请删除您的
link=link。替换('http','https')
行,然后重试。

谢谢。成功了。我希望我能对你的答案投赞成票,但我现在没有足够的声誉。没问题。“处理请求”是什么意思?您可以使用您的声誉:)为了让您的代码运行,我必须进行一些编辑,例如将库导入从
从urllib更改为
从urllib导入urlopen
从urllib导入urlopen