为什么我在尝试抓取特定网站时会收到 "Connection aborted" 错误?

Why do I get a "Connection aborted" error when trying to crawl a specific website?

我在Python2.7写了一个网络爬虫,但是在浏览器中可以查看,但无法下载特定站点。

我的代码如下:

# -*- coding: utf-8 -*-

import requests

# OK
url = 'http://blog.ithome.com.tw/'
url = 'http://7club.ithome.com.tw/'
url = 'https://member.ithome.com.tw/'
url = 'http://ithome.com.tw/'
url = 'http://weekly.ithome.com.tw'

# NOT OK
url = 'http://download.ithome.com.tw'
url = 'http://apphome.ithome.com.tw/'
url = 'http://ithelp.ithome.com.tw/'

try:
    response = requests.get(url)
    print 'OK!'
    print 'response.status_code: %s' %(response.status_code)

except Exception, e:
    print 'NOT OK!'
    print 'Error: %s' %(e)
print 'DONE!'
print 'response.status_code: %s' %(response.status_code)

每次尝试都会出现此错误:

C:\Python27\python.exe "E:/python crawler/test_ConnectionFailed.py"
NOT OK!
Error: ('Connection aborted.', BadStatusLine("''",))
DONE!
Traceback (most recent call last):
  File "E:/python crawler/test_ConnectionFailed.py", line 29, in <module>
    print 'response.status_code: %s' %(response.status_code)
NameError: name 'response' is not defined

Process finished with exit code 1

为什么会发生这种情况,我该如何解决?

已解决!我只是用了另一个代理软件,然后OK!

无法解析这些域的连接,对 url 执行正常的 ping 操作会产生此结果

命令 运行:

ping http://download.ithome.com.tw

结果

The host could not be resolved

没有响应,因此没有状态行,在正常情况下会包含状态代码。

我发现使用 urllib2 库比请求更好。

import urllib2
def get_page(url):
  request = urllib2.Request(url)
  request = urllib2.urlopen(request)
  data = request.read()
  return data
url = "http://blog.ithome.com.tw/"
print get_page(url)

祝你有美好的一天。