小编Chu*_*ieh的帖子

避免挂在 conn.getresponse() ( httplib.HTTPConnection ) 上

我用 python 编写了一个爬虫,根据一些给定的网址从网站下载一些网页。我注意到我的程序有时会挂在“conn.getresponse()”这一行。没有抛出任何异常,程序只是永远在那里等待。

conn = httplib.HTTPConnection(component.netloc)
conn.request("GET", component.path + "?" + component.query)
resp = conn.getresponse() #hang here
Run Code Online (Sandbox Code Playgroud)

我读了 api 文档,它说(添加超时):

conn = httplib.HTTPConnection(component.netloc, timeout=10)
Run Code Online (Sandbox Code Playgroud)

但是,它不允许我“重试”连接。超时后重试爬网的最佳做法是什么?

例如,我正在考虑以下解决方案:

trials = 3
while trials > 0:
    try:
        ... code here ...
    except:
        trials -= 1
Run Code Online (Sandbox Code Playgroud)

我的方向正确吗?

python timeout http

5
推荐指数
1
解决办法
3345
查看次数

为什么Scala中的隐式类必须驻留在另一个特征/类/对象中?

基于scala文档:http://docs.scala-lang.org/overviews/core/implicit-classes.html,隐式类有三个限制,我在这里引用的第一个是

它们必须在另一个trait/class/object中定义

解释/证明这种限制的直觉/理由是什么?

scala

4
推荐指数
1
解决办法
259
查看次数

在Scala中使用def或var声明一个函数

我试图找出在Scala中声明函数时def和之间的区别var/val.

假设我们有一个功能:

scala> def f(x: Int) = { x * 2 }
f: (x: Int)Int
Run Code Online (Sandbox Code Playgroud)

另一个功能g:

scala> var g = (x:Int) => x*2
g: Int => Int = <function1>
Run Code Online (Sandbox Code Playgroud)

显然它们在以下方面是相同的:

scala> f(2)
res0: Int = 4

scala> g(2)
res1: Int = 4
Run Code Online (Sandbox Code Playgroud)

但是,我能做到

g = f
g: Int => Int = <function1>
Run Code Online (Sandbox Code Playgroud)

但不是

scala> f = g
<console>:13: error: missing arguments for method f;
follow this method with `_' if you want to treat it as …
Run Code Online (Sandbox Code Playgroud)

scala

2
推荐指数
1
解决办法
240
查看次数

标签 统计

scala ×2

http ×1

python ×1

timeout ×1