urllib2.URLError: urlopen error [Errno 111] Connection refused
記錄個還沒解決的問題。下面爬蟲代碼是可以執(zhí)行的,但是在我的Ubuntu的虛擬中剛開始是可以運行的,但是,后來不知道改了什么東西,用urllib2寫的爬蟲和用scrapy 的爬蟲代碼都不能運行了!!。
import urllib2 import reclass Spider:def __init__(self):self.page = 1self.switch = Truedef loadPage(self):print 'loadPage'url = "http://www.neihan8.com/article/list_5_" + str(self.page) + ".html"headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}request = urllib2.Request(url, headers=headers)response = urllib2.urlopen(request)html = response.read()gbk_html = html.decode('gbk').encode('utf-8')pattern = re.compile('<div\sclass="f18 mb20">(.*?)</div>', re.S)content_list = pattern.findall(gbk_html)self.dealPage(content_list)def dealPage(self, content_list):for item in content_list:item = item.replace('<br />', '').replace('<p>', '').replace('</p>', '')self.writePage(item)def writePage(self, item):with open('duanzi.txt', 'a') as f:f.write(item)def startWork(self):while self.switch:self.loadPage()command = raw_input('please enter continue, q back')if command == 'q':self.switch = Falseself.page += 1print '3q use'if __name__ == '__main__':s = Spider()s.startWork()?爬蟲結(jié)果
在終端下的錯誤信息。
Traceback (most recent call last):File "01-neihan.py", line 44, in <module>s.startWork()File "01-neihan.py", line 34, in startWorkself.loadPage()File "01-neihan.py", line 15, in loadPageresponse = urllib2.urlopen(request)File "/usr/lib/python2.7/urllib2.py", line 154, in urlopenreturn opener.open(url, data, timeout)File "/usr/lib/python2.7/urllib2.py", line 429, in openresponse = self._open(req, data)File "/usr/lib/python2.7/urllib2.py", line 447, in _open'_open', req)File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chainresult = func(*args)File "/usr/lib/python2.7/urllib2.py", line 1228, in http_openreturn self.do_open(httplib.HTTPConnection, req)File "/usr/lib/python2.7/urllib2.py", line 1198, in do_openraise URLError(err) urllib2.URLError: <urlopen error [Errno 111] Connection refused>出現(xiàn)這個問題之前是我想fangqiang, 所以設(shè)置了代理,后來把代理關(guān)了也不行。這問題目前還沒解決,不知道是Ubuntu的環(huán)境問題,還是python的問題。
問題:定位出問題,確實是代理的問題了。
解決辦法:
1、首先查看下 /etc/apt/apt.conf,發(fā)現(xiàn)里面里面有:
http_proxy="http://192.168.16.109:13128/"
https_proxy="https://192.168.16.109:13128/"
也許內(nèi)容和我的不一樣。然后刪除這個文件,然后重啟電腦,發(fā)現(xiàn)里面還沒有解決。
2、查看一下:cat /etc/enviroment,發(fā)現(xiàn)有配置
http_proxy="http://192.168.16.109:13128/"
https_proxy="https://192.168.16.109:13128/"
把里面的配置文件刪除了,(切記PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"這一行不要刪除,不然Ubuntu開機就不能進入到桌面了)
3、然后重啟電腦,執(zhí)行代碼,問題解決。
?
?
?
總結(jié)
以上是生活随笔為你收集整理的urllib2.URLError: urlopen error [Errno 111] Connection refused的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【职场】遇到了个失业开滴滴的程序员
- 下一篇: 如何在IE浏览器播放RTSP或RTMP流