urllib2.URLError: urlopen error [Errno 111] Connection refused
记录个还没解决的问题。下面爬虫代码是可以执行的,但是在我的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()
爬虫结果
在终端下的错误信息。
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>
出现这个问题之前是我想fangqiang, 所以设置了代理,后来把代理关了也不行。这问题目前还没解决,不知道是Ubuntu的环境问题,还是python的问题。
问题:定位出问题,确实是代理的问题了。
解决办法:
1、首先查看下 /etc/apt/apt.conf,发现里面里面有:
http_proxy="http://192.168.16.109:13128/"
https_proxy="https://192.168.16.109:13128/"
也许内容和我的不一样。然后删除这个文件,然后重启电脑,发现里面还没有解决。
2、查看一下:cat /etc/enviroment,发现有配置
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、然后重启电脑,执行代码,问题解决。
urllib2.URLError: urlopen error [Errno 111] Connection refused相关推荐
- GAE使用时的一些问题:urllib2.URLError: urlopen error [Errno 10061]
问题描述: Win7系统下,在用GAE自己架设代理时,用appcfg.py update myapp/上传时出现urllib2.URLError: <urlopen error [Errno 1 ...
- windows或Ubuntu中请求github.com请求超时,或在下载GitHub文件出现:<urlopen error [Errno 110] Connection timed out>
欢迎大家关注笔者,你的关注是我持续更博的最大动力 原创文章,转载告知,盗版必究 windows或Ubuntu中请求github.com请求超时,或在下载GitHub文件出现:urlopen error ...
- 解决:consumer: Cannot connect to amqp://admin:**@127.0.0.1:5672//: [Errno 111] Connection refused.
为了避免大家机毁人亡,徒耗精力搜索到一堆没用的重复旧文,我决定写下这篇关于docker容器化的celery 无法连接rabbitmq的文章,以增强互联网的技术氛围和工程师的原创精神 worker_1 ...
- python:urllib2.URLError urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
现在随处可见 https 开头的网站,urllib2可以为 HTTPS 请求验证SSL证书,就像web浏览器一样,如果网站的SSL证书是经过CA认证的,则能够正常访问, 如果SSL证书验证不通过,或者 ...
- 成功解决urllib.error.URLError urlopen error Errno 11004 getaddrinfo failed
成功解决urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed> 目录 解决问题 解决思路 解决方法 ...
- NFS服务启动:rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
nfs重启时提示: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused) 解决办法: 1 #service rpc ...
- 如何解决 类似 nltk安装Wordnet出错[nltk_data] Error loading wordnet: <urlopen error [Errno 111] 问题
参考:https://blog.csdn.net/liu16659/article/details/109691534 最近想做一个NLP的数据增强,于是找到了 EDA: Easy Data Augm ...
- <urlopen error [Errno 104] Connection reset by peer>
使用python写一个脚本调用接口时,报了这个错,在"urllib.request.urlopen(req, context=context)"这个方法中报错. 错误原因 发送的r ...
- Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused(MongoDB启动异常)
2019独角兽企业重金招聘Python工程师标准>>> 以下是解决方案地址: http://blog.csdn.net/liwuyang2013/article/details/50 ...
最新文章
- 图论 ---- Codeforces Round #649 (Div. 2)D题[dfs求环+深度分层求图中独立集]
- Linux中光盘使用的文件类型,linux下mount命令使用详解---linux挂载光盘等文件系统...
- Dockerfile使用,怎么通过Dockerfile完成docker映像配置
- 牛客 - 牛牛的滑动窗口(单调栈+思维+差分)
- 使用Java EE的ManagedExecutorService异步执行事务
- 随想录(网站api的设计)
- SAP自学指南:案例公司的概括
- Hessian 3.2.0的两个bug(转)
- 两万字《Java完全自学手册》15张导图,送给所有的零基础小白(建议收藏)
- 用Redis客户端工具连接Redis
- cocosBuilder使用总结
- EditText属性整理
- docker快速搭建http服务器
- 利用东方财富网获取股票代码
- python语言百分号的含义_python中百分号意思的是什么
- rootkit后门程序开发测试过程
- utf8编码和utf8mb4编码(其它编码简介)
- Sicily 1140. 国王的遗产
- Android Studio——飞入文字动画演示
- 解读中国版存款保险制度:差别费率+强监管色彩
热门文章
- Oracle sqlldr
- c语言的报告一,C语言实验报告(一).doc
- python读取raw图片文件_在python下读取并展示raw格式的图片实例
- treeset java_Java TreeSet clear()方法与示例
- 插入排序算法 ,递归实现_C程序实现递归插入排序
- Java LinkedList getFirst()方法与示例
- python 编码问题_Python电源挑战| 竞争编码问题
- type c pin定义_在C中定义宏以设置和清除PIN的位
- python mpi开销_GitHub - hustpython/MPIK-Means
- 华为鸿蒙系统正式登场,华为自研鸿蒙系统将于8月9日正式登场,还有全新的鸿鹄芯片...