Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
0b9a18e1
S
scrapy
项目概览
looyolo
/
scrapy
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
scrapy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0b9a18e1
编写于
3月 07, 2017
作者:
P
Paul Tremberth
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Warn only once for all spiders
上级
978306a2
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
21 addition
and
14 deletion
+21
-14
scrapy/spiderloader.py
scrapy/spiderloader.py
+13
-9
tests/test_spiderloader/__init__.py
tests/test_spiderloader/__init__.py
+8
-5
未找到文件。
scrapy/spiderloader.py
浏览文件 @
0b9a18e1
...
@@ -23,18 +23,21 @@ class SpiderLoader(object):
...
@@ -23,18 +23,21 @@ class SpiderLoader(object):
self
.
_found
=
defaultdict
(
list
)
self
.
_found
=
defaultdict
(
list
)
self
.
_load_all_spiders
()
self
.
_load_all_spiders
()
def
_check_name_duplicates
(
self
):
dupes
=
[
"
\n
"
.
join
(
" {cls} named {name!r} (in {module})"
.
format
(
module
=
mod
,
cls
=
cls
,
name
=
name
)
for
(
mod
,
cls
)
in
locations
)
for
name
,
locations
in
self
.
_found
.
items
()
if
len
(
locations
)
>
1
]
if
dupes
:
msg
=
(
"There are several spiders with the same name:
\n\n
"
"{}
\n\n
This can cause unexpected behavior."
.
format
(
"
\n\n
"
.
join
(
dupes
)))
warnings
.
warn
(
msg
,
UserWarning
)
def
_load_spiders
(
self
,
module
):
def
_load_spiders
(
self
,
module
):
for
spcls
in
iter_spider_classes
(
module
):
for
spcls
in
iter_spider_classes
(
module
):
self
.
_found
[
spcls
.
name
].
append
((
module
.
__name__
,
spcls
.
__name__
))
self
.
_found
[
spcls
.
name
].
append
((
module
.
__name__
,
spcls
.
__name__
))
if
spcls
.
name
in
self
.
_spiders
.
keys
():
import
warnings
msg
=
(
"There are several spiders with the same name {!r}:
\n
"
"{}
\n
This can cause unexpected behavior."
.
format
(
spcls
.
name
,
"
\n
"
.
join
(
" {1} (in {0})"
.
format
(
mod
,
cls
)
for
(
mod
,
cls
)
in
self
.
_found
[
spcls
.
name
])))
warnings
.
warn
(
msg
,
UserWarning
)
self
.
_spiders
[
spcls
.
name
]
=
spcls
self
.
_spiders
[
spcls
.
name
]
=
spcls
def
_load_all_spiders
(
self
):
def
_load_all_spiders
(
self
):
...
@@ -47,6 +50,7 @@ class SpiderLoader(object):
...
@@ -47,6 +50,7 @@ class SpiderLoader(object):
"Check SPIDER_MODULES setting"
.
format
(
"Check SPIDER_MODULES setting"
.
format
(
modname
=
name
,
tb
=
traceback
.
format_exc
()))
modname
=
name
,
tb
=
traceback
.
format_exc
()))
warnings
.
warn
(
msg
,
RuntimeWarning
)
warnings
.
warn
(
msg
,
RuntimeWarning
)
self
.
_check_name_duplicates
()
@
classmethod
@
classmethod
def
from_settings
(
cls
,
settings
):
def
from_settings
(
cls
,
settings
):
...
...
tests/test_spiderloader/__init__.py
浏览文件 @
0b9a18e1
...
@@ -127,7 +127,9 @@ class DuplicateSpiderNameLoaderTest(unittest.TestCase):
...
@@ -127,7 +127,9 @@ class DuplicateSpiderNameLoaderTest(unittest.TestCase):
spider_loader
=
SpiderLoader
.
from_settings
(
self
.
settings
)
spider_loader
=
SpiderLoader
.
from_settings
(
self
.
settings
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertEqual
(
len
(
w
),
1
)
self
.
assertIn
(
"several spiders with the same name 'spider3'"
,
str
(
w
[
0
].
message
))
msg
=
str
(
w
[
0
].
message
)
self
.
assertIn
(
"several spiders with the same name"
,
msg
)
self
.
assertIn
(
"'spider3'"
,
msg
)
spiders
=
set
(
spider_loader
.
list
())
spiders
=
set
(
spider_loader
.
list
())
self
.
assertEqual
(
spiders
,
set
([
'spider1'
,
'spider2'
,
'spider3'
,
'spider4'
]))
self
.
assertEqual
(
spiders
,
set
([
'spider1'
,
'spider2'
,
'spider3'
,
'spider4'
]))
...
@@ -143,10 +145,11 @@ class DuplicateSpiderNameLoaderTest(unittest.TestCase):
...
@@ -143,10 +145,11 @@ class DuplicateSpiderNameLoaderTest(unittest.TestCase):
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
spider_loader
=
SpiderLoader
.
from_settings
(
self
.
settings
)
spider_loader
=
SpiderLoader
.
from_settings
(
self
.
settings
)
self
.
assertEqual
(
len
(
w
),
2
)
self
.
assertEqual
(
len
(
w
),
1
)
msgs
=
sorted
(
str
(
wrn
.
message
)
for
wrn
in
w
)
msg
=
str
(
w
[
0
].
message
)
self
.
assertIn
(
"several spiders with the same name 'spider1'"
,
msgs
[
0
])
self
.
assertIn
(
"several spiders with the same name"
,
msg
)
self
.
assertIn
(
"several spiders with the same name 'spider2'"
,
msgs
[
1
])
self
.
assertIn
(
"'spider1'"
,
msg
)
self
.
assertIn
(
"'spider2'"
,
msg
)
spiders
=
set
(
spider_loader
.
list
())
spiders
=
set
(
spider_loader
.
list
())
self
.
assertEqual
(
spiders
,
set
([
'spider1'
,
'spider2'
,
'spider3'
,
'spider4'
]))
self
.
assertEqual
(
spiders
,
set
([
'spider1'
,
'spider2'
,
'spider3'
,
'spider4'
]))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录