准备写一个python脚本抓取网页数据,前面抓了几个都没有什么问题,但总会抓取不完整,在中间过程中没有反应,发现执行urlopen的地方总是提示超时,百度了一下,因为我使用的是urllib不是urllib2,所以无法直接在urlopen里面加timeout参数,只能是设置全局脚本的超时时间

首先:
import socket

然后:
socket.setdefaulttimeout(60)

设置全局的超时时间为60s。

之后再执行脚本的时候就没有什么大问题了。

当然,如果是urllib2就更简单,而且现实中,urllib,urllib2常会一起使用(两者分别提供不同的功能)

html = urllib.urlopen(url).read()
修改为
import urllib2
html = urllib2.urlopen(url, timeout=60).read()

python 使用urllib.urlopen超时问题的解决方法的更多相关文章

  1. Ubuntu下 fatal error: Python.h: No such file or directory 解决方法

    参考: fatal error: Python.h: No such file or directory Ubuntu下 fatal error: Python.h: No such file or ...

  2. Python使用easy-install安装时报UnicodeDecodeError的解决方法

    Python使用easy-install安装时报UnicodeDecodeError的解决方法,有需要的朋友可以参考下. 问题描述: 在使用easy-install安装matplotlib.pypar ...

  3. python抓取中文网页乱码通用解决方法

    注:转载自http://www.cnpythoner.com/ 我们经常通过python做采集网页数据的时候,会碰到一些乱码问题,今天给大家分享一个解决网页乱码,尤其是中文网页的通用方法. 首页我们需 ...

  4. Python使用suds调用webservice报错解决方法:AttributeError: 'Document' object has no attribute 'set'

    使用python的suds包调用webservice服务接口,报错:AttributeError: 'Document' object has no attribute 'set' 调用服务接口代码: ...

  5. python安装途中遇到的问题和解决方法

    一.setuptools安装错误:RuntimeError: Compression requires the (missing) zlib module 1. 描述 搞了个腾讯云的服务器,闲在手上没 ...

  6. Python中pip install MySQL-python报错解决方法

    环境 Centos 7(其他Centos或者RHEL一样) 问题 在执行 pip install MySQL-python 时报错如: Command "python setup.py eg ...

  7. Python: 安装 sklearn 包出现错误的解决方法

    今天在安装 Python 的 sklearn 包时出现了 Cannot uninstall 'numpy' 和 Cannot uninstall 'scipy' 错误,下面记录了我尝试了很多网上的方法 ...

  8. Python使用request包请求网页乱码解决方法

    使用requests请求网页时,返回的页面信息有时是乱码,如下代码 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) App ...

  9. 如何解决Python下 pip install module 下载慢解决方法?

    对于Python来编程的用户最大的一个痛点就是,下载模块是下载速度特别慢,那么有没有解决方法呢? 换Python的pip下载源 1.首先安装一个模块 pqi,在cmd下 pip install pqi ...

随机推荐

  1. [Android 源码] Android源码下载

    Android源码下载 为了能够顺利的下载Android的源码,同时也为了避免在网络上再次搜寻如何下载源码的麻烦,我把下载过程记录在这篇文档中. 官网中也有详细的介绍: http://source.a ...

  2. 使用R语言和XML包抓取网页数据-Scraping data from web pages in R with XML package

    In the last years a lot of data has been released publicly in different formats, but sometimes the d ...

  3. iOS: 字体样式

    获取iOS内置所有的字体如下: NSArray *familyNames = [UIFont familyNames]; for( NSString *familyName in familyName ...

  4. Sqlserver获取行号

    Sqlserver获取行号   select row_number()over(order by userid )as RowNum,*from OUM_User

  5. 用php编写我的第一段代码:hello world

    一.php环境的搭建 在编写php前,先搭建php环境,我选择了一站式安装软件WampServer,WampServer的下载地址:http://www.wampserver.com/ WampSer ...

  6. Flutter 动画使用

    旋转动画  透明度变换动画  在Android中,可以通过View.animate()对视图进行动画处理,那在Flutter中怎样才能对Widget进行处理 在Flutter中,可以通过动画库给wid ...

  7. Android Studio优秀插件汇总

  8. Unicode 与UTF-8 关系

    作者:邱昊宇链接:https://www.zhihu.com/question/23374078/answer/24385963来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  9. js 实现图片的无缝滚动

      js 实现图片的无缝滚动 CreateTime--2018年3月7日17:18:34 Author:Marydon 测试成功 <!DOCTYPE html> <html> ...

  10. jquery 图片懒加载

      jquery 图片懒加载 CreationTime--2018年7月1日14点45分 Author:Marydon 1.源码展示 (function(a){a.fn.lazyload=functi ...