Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
ScrapyTutorial
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anton Gusev
ScrapyTutorial
Merge requests
!5
Couldn't fetch the linked file.
Develop
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Develop
develop
into
master
Overview
0
Commits
45
Pipelines
0
Changes
1
Merged
Anton Gusev
requested to merge
develop
into
master
5 years ago
Overview
0
Commits
45
Pipelines
0
Changes
1
Expand
0
0
Merge request reports
Viewing commit
6c10a062
Prev
Next
Show latest version
1 file
+
11
−
6
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
6c10a062
add logging.debug for BooksSpider
· 6c10a062
Anton Gusev
authored
5 years ago
src/spiders/books_spider.py
+
11
−
6
Options
import
scrapy
from
scrapy
import
Spider
import
re
from
items.BookItem
import
BookItem
from
database.connectors.BookConnector
import
BookConnector
import
logging
class
BooksSpider
(
BookConnector
,
scrapy
.
Spider
):
class
BooksSpider
(
Spider
,
BookConnector
):
name
=
'
books
'
start_urls
=
[
'
http://books.toscrape.com/
'
]
page
=
1
def
parse
(
self
,
response
):
self
.
logger
.
debug
(
'
Current page: {}
'
.
format
(
self
.
page
))
# follow links to book pages
for
idx
,
href
in
enumerate
(
response
.
css
(
'
div.image_container a::attr(href)
'
)):
# TODO delete enumerate
yield
response
.
follow
(
href
,
self
.
parse_book
)
for
idx
,
href
in
enumerate
(
response
.
css
(
'
div.image_container a::attr(href)
'
)):
# TODO delete enumerate
yield
response
.
follow
(
href
,
self
.
parse_book
,
meta
=
{
'
idx
'
:
idx
}
)
# pagination
next_page
=
response
.
css
(
'
li.next a::attr(href)
'
).
get
()
if
next_page
is
not
None
:
self
.
page
+=
1
yield
response
.
follow
(
next_page
,
callback
=
self
.
parse
)
def
parse_book
(
self
,
response
):
self
.
logger
.
debug
(
'
Index book in page: {}
'
.
format
(
response
.
meta
.
get
(
'
idx
'
)))
book
=
BookItem
()
table_data
=
response
.
css
(
'
table td::text
'
).
getall
()
@@ -50,4 +55,4 @@ class BooksSpider(BookConnector, scrapy.Spider):
if
'
Four
'
in
class_all
:
return
4
if
'
Five
'
in
class_all
:
return
5
return
5
\ No newline at end of file
Loading