Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #2

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Scrapy_ project built following `Zyte’s web scraping tutorial`_.
Requirements
============

Python 3.8 or higher.
Python 3.9 or higher.


Setup
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scrapy
scrapy==2.11.2
scrapy-zyte-api
shub
zyte-spider-templates
2 changes: 1 addition & 1 deletion scrapinghub.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requirements:
file: requirements.txt
stacks:
default: scrapy:2.11
default: scrapy:2.11-20241022
29 changes: 11 additions & 18 deletions tutorial/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from itemadapter import ItemAdapter
from zyte_common_items import ZyteItemAdapter

ItemAdapter.ADAPTER_CLASSES.appendleft(ZyteItemAdapter)

# Scrapy settings for tutorial project
#
# For simplicity, this file contains only settings considered important or
Expand Down Expand Up @@ -96,27 +101,15 @@
FEED_EXPORT_ENCODING = "utf-8"

# Custom settings
DOWNLOAD_HANDLERS = {
"http": "scrapy_zyte_api.ScrapyZyteAPIDownloadHandler",
"https": "scrapy_zyte_api.ScrapyZyteAPIDownloadHandler",
ADDONS = {
"scrapy_zyte_api.Addon": 500,
"zyte_spider_templates.Addon": 700,
}
ZYTE_API_KEY = "YOUR_API_KEY"
DOWNLOADER_MIDDLEWARES = {
"scrapy_poet.InjectionMiddleware": 543,
"scrapy_zyte_api.ScrapyZyteAPIDownloaderMiddleware": 1000,
"scrapy.downloadermiddlewares.stats.DownloaderStats": None,
"scrapy_poet.DownloaderStatsMiddleware": 850,
}
REQUEST_FINGERPRINTER_CLASS = "scrapy_zyte_api.ScrapyZyteAPIRequestFingerprinter"
ZYTE_API_TRANSPARENT_MODE = True
SPIDER_MIDDLEWARES = {
"scrapy_zyte_api.ScrapyZyteAPISpiderMiddleware": 100,
"scrapy_poet.RetryMiddleware": 275,
"zyte_spider_templates.middlewares.CrawlingLogsMiddleware": 1000,
}
SCRAPY_POET_DISCOVER = [
"zyte_spider_templates.page_objects",
]
SCRAPY_POET_PROVIDERS = {
"scrapy_zyte_api.providers.ZyteApiProvider": 1100,
}
CLOSESPIDER_TIMEOUT_NO_ITEM = 600
SCHEDULER_DISK_QUEUE = "scrapy.squeues.PickleFifoDiskQueue"
SCHEDULER_MEMORY_QUEUE = "scrapy.squeues.FifoMemoryQueue"
42 changes: 42 additions & 0 deletions tutorial/spiders/quotes_toscrape_com_scroll_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
from base64 import b64decode

from scrapy import Request, Spider


class QuotesToScrapeComScrollCaptureSpider(Spider):
name = "quotes_toscrape_com_scroll_capture"

def start_requests(self):
yield Request(
"http://quotes.toscrape.com/scroll",
meta={
"zyte_api_automap": {
"browserHtml": True,
"actions": [
{
"action": "scrollBottom",
},
],
"networkCapture": [
{
"filterType": "url",
"httpResponseBody": True,
"value": "/api/",
"matchType": "contains",
},
],
},
},
)

def parse(self, response):
for capture in response.raw_api_response["networkCapture"]:
text = b64decode(capture["httpResponseBody"]).decode()
data = json.loads(text)
for quote in data["quotes"]:
yield {
"author": quote["author"]["name"],
"tags": quote["tags"],
"text": quote["text"],
}