gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:02:55 24 4
gpt4 key购买 nike

我用Python 2.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了!

最佳答案

我发现使用 urllib2 库比 request 更好。

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)

关于python - 为什么我在尝试抓取特定网站时会收到 "Connection aborted"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34872598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com