python 使用urllib.urlopen超时问题的解决方法
准备写一个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超时问题的解决方法的更多相关文章
- 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 ...
- Python使用easy-install安装时报UnicodeDecodeError的解决方法
Python使用easy-install安装时报UnicodeDecodeError的解决方法,有需要的朋友可以参考下. 问题描述: 在使用easy-install安装matplotlib.pypar ...
- python抓取中文网页乱码通用解决方法
注:转载自http://www.cnpythoner.com/ 我们经常通过python做采集网页数据的时候,会碰到一些乱码问题,今天给大家分享一个解决网页乱码,尤其是中文网页的通用方法. 首页我们需 ...
- Python使用suds调用webservice报错解决方法:AttributeError: 'Document' object has no attribute 'set'
使用python的suds包调用webservice服务接口,报错:AttributeError: 'Document' object has no attribute 'set' 调用服务接口代码: ...
- python安装途中遇到的问题和解决方法
一.setuptools安装错误:RuntimeError: Compression requires the (missing) zlib module 1. 描述 搞了个腾讯云的服务器,闲在手上没 ...
- Python中pip install MySQL-python报错解决方法
环境 Centos 7(其他Centos或者RHEL一样) 问题 在执行 pip install MySQL-python 时报错如: Command "python setup.py eg ...
- Python: 安装 sklearn 包出现错误的解决方法
今天在安装 Python 的 sklearn 包时出现了 Cannot uninstall 'numpy' 和 Cannot uninstall 'scipy' 错误,下面记录了我尝试了很多网上的方法 ...
- Python使用request包请求网页乱码解决方法
使用requests请求网页时,返回的页面信息有时是乱码,如下代码 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) App ...
- 如何解决Python下 pip install module 下载慢解决方法?
对于Python来编程的用户最大的一个痛点就是,下载模块是下载速度特别慢,那么有没有解决方法呢? 换Python的pip下载源 1.首先安装一个模块 pqi,在cmd下 pip install pqi ...
随机推荐
- [Ubuntu]明明白白安装中文字体
Ubuntu 6.06 dapper对中文的支持应该说是一向不错的,包括现在默认安装就包含了scim输入法.然而,一般安装的ubuntu虽然能够显示.输入中文了,但总觉 得所显示的中文像歪瓜裂枣,很是 ...
- tornado框架介绍
一.安装tornado 手动安装: 下载 tornado-1.2.1.tar.gz tar xvzf tornado-1.2.1.tar.gz cd tornado-1.2.1 python setu ...
- js 小数点 取整数
取整数 Math.round() 小数点 (10/3).toFixed(2)
- swfupload js中 file 对象的属性
name=3cc68cfc60b87e6dd6887d8a.jpg modificationdate=Wed Apr 21 15:48:30 UTC+0800 2010 filestatus=-1 ...
- SVN-如何删除 SVN 文件夹下面的小图标
第一步:右键新建文本文件. 第二步:打开 并复制一下文件 到 新建文本文件. @echo on color 2f mode con: cols=80 lines=25 @REM ...
- Sqlserver 使用CTE如何按子查询排序?
需求:查出最近有更改的客户信息(按最后更改时间排序,来自SystemLog表LogDateTime字段) 说明: Customer:客户信息表SystemLog:系统日志表,记录所有表信息的增,删,改 ...
- Git使用sublime_text作用默认编辑器
Git使用的是Vim来作用默认的编辑器,但一直都用不好这个编辑器,所以打算把他换成sublime_text 使用windows默认的记事本 git config --global core.edito ...
- 分布式系统漫谈一 —— Google三驾马车: GFS,mapreduce,Bigtable
分布式系统学习必读文章!!!! 原文:http://blog.sina.com.cn/s/blog_4ed630e801000bi3.html 分布式系统漫谈一 —— Google三驾马车: GFS, ...
- [React] Refactor a Class Component with React hooks to a Function
We have a render prop based class component that allows us to make a GraphQL request with a given qu ...
- git config 的全局配置
使用git的全局配置 .gitconfig 一:修改 用户下的.gitconfig 修改如图信息,添加你的信息 二: 命令添加 $ git config --global user.name ...