使用Python 2.7
我正在尝试运行以下程序,但它返回连接已中止10060 错误
import requests
from bs4 import BeautifulSoup
def re_crawler(pages):
page = 1
while page <= pages:
url = 'http://www.digitalmarketplace.service.gov.uk/g-cloud/search?page=' + str(page) + '&lot=saas'
code = requests.get(url)
text = code.text
soup = BeautifulSoup(text)
for link in soup.select('div.search-result h2.search-result-title a'):
href = "http://www.digitalmarketplace.service.gov.uk" + link.get('href')
print (href)
page += 1
re_crawler(2)
运行此程序后,我得到以下输出
Traceback (most recent call last):
File "C:/Users/PB/PycharmProjects/crawler/SaaS UK.py", line 18, in <module>
re_crawler(2)
File "C:/Users/PB/PycharmProjects/crawler/SaaS UK.py", line 10, in re_crawler
code = requests.get(url)
File "C:\Python27\lib\requests\api.py", line 68, in get
return request('get', url, **kwargs)
File "C:\Python27\lib\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\requests\sessions.py", line 464, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\requests\sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\requests\adapters.py", line 415, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
Process finished with exit code 1
请告知此错误的内容以及解决方法
非常感谢
答案 0 :(得分:1)
尝试https://而不是http://。 似乎解决了我这方面的问题。
try:
r = requests.get('http://www.digitalmarketplace.service.gov.uk', timeout=1)
except:
print("http connection failed, trying https now")
r = requests.get('https://www.digitalmarketplace.service.gov.uk', timeout=1)
print('{} ({})'.format(r.url, r.status_code))
http连接失败,现在尝试https