mirror of https://github.com/scrapy/scrapy.git
Compare commits
146 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
4d4a04f318 | |
|
|
e80f94fe8a | |
|
|
7faf20c6b5 | |
|
|
a591d15c04 | |
|
|
11d7a05a6f | |
|
|
d8ba1571e7 | |
|
|
b3670369b8 | |
|
|
c9446931a8 | |
|
|
9d9950df69 | |
|
|
61f99f2df1 | |
|
|
bdf3067935 | |
|
|
c5ec881f1d | |
|
|
feb692f552 | |
|
|
9523e1ec8c | |
|
|
5ccc8dbe8a | |
|
|
dd10cb8e9a | |
|
|
870803b7fb | |
|
|
361f689df7 | |
|
|
fc5216f156 | |
|
|
a6d6a48aa6 | |
|
|
00098cb596 | |
|
|
deb7e2861e | |
|
|
6ad8a043ca | |
|
|
52147017b4 | |
|
|
6591cb756c | |
|
|
4b2b56f384 | |
|
|
9559cbee1e | |
|
|
185d6b9a20 | |
|
|
4b40d2d06a | |
|
|
cf5607f8bc | |
|
|
edc353c975 | |
|
|
1b940a75ac | |
|
|
7ab404c725 | |
|
|
0ccddb4f61 | |
|
|
0007676e8d | |
|
|
d8d7de2339 | |
|
|
74e6b61071 | |
|
|
c690eac770 | |
|
|
65e8954a06 | |
|
|
dd4549e6f9 | |
|
|
b78ab3d6c8 | |
|
|
fb3455304d | |
|
|
f5a62a293f | |
|
|
f605defefc | |
|
|
7499d17e28 | |
|
|
b6596de317 | |
|
|
75f05d4e80 | |
|
|
c9f952c258 | |
|
|
6393858c7e | |
|
|
d2842a205c | |
|
|
7b3f88f8ab | |
|
|
699c93f6b2 | |
|
|
3f3cb885ed | |
|
|
e5e48883b5 | |
|
|
abbf3b95fc | |
|
|
fada8be1db | |
|
|
b7824db573 | |
|
|
0a4a92e843 | |
|
|
e74647572d | |
|
|
cfef12392a | |
|
|
4f241b73be | |
|
|
9893a7fac6 | |
|
|
f63a3aff25 | |
|
|
3a36955261 | |
|
|
af30cfea12 | |
|
|
983e6c1182 | |
|
|
b08ed1cf05 | |
|
|
7afc875081 | |
|
|
a8ffdcf851 | |
|
|
c99f6e2209 | |
|
|
e0a7de7213 | |
|
|
e7f229f5b2 | |
|
|
4cb049cb15 | |
|
|
ad4549673b | |
|
|
ba28630c98 | |
|
|
2d0a898e2d | |
|
|
93a627ba1c | |
|
|
cd25ece58a | |
|
|
beb6c51c17 | |
|
|
d59f9b644a | |
|
|
ddafb37a7c | |
|
|
4e956bd2de | |
|
|
d2290c35c2 | |
|
|
d9e2f5fbf7 | |
|
|
5149e2c679 | |
|
|
58af57a3ea | |
|
|
b2d8b06be6 | |
|
|
13c1c1faf8 | |
|
|
df2f3d708e | |
|
|
fed75a6c76 | |
|
|
90deebe75e | |
|
|
44406806f8 | |
|
|
4a16550859 | |
|
|
abe9c63841 | |
|
|
a84b7850fc | |
|
|
55c17a8985 | |
|
|
f875af4a86 | |
|
|
a8a8f20d9c | |
|
|
85c616c5c7 | |
|
|
ae4a8e39e1 | |
|
|
4cfe7a08cd | |
|
|
2d007bc450 | |
|
|
2798c03bb0 | |
|
|
3b34ab88c0 | |
|
|
f7db039d1c | |
|
|
7fc84d372a | |
|
|
7f15ca92fc | |
|
|
5223dbe3fd | |
|
|
fc14a0ce59 | |
|
|
33452f3aeb | |
|
|
9776a72a6a | |
|
|
8d69a7c865 | |
|
|
f3868e11fb | |
|
|
9ca206da64 | |
|
|
d05b241f64 | |
|
|
55c61646da | |
|
|
c62a81d7dd | |
|
|
a9324fbf76 | |
|
|
9f02f6c16a | |
|
|
6cb2fe1fc3 | |
|
|
f24bc749ea | |
|
|
5fc40f07f3 | |
|
|
af7dcabebb | |
|
|
8ecfd20fcd | |
|
|
14f49ab63c | |
|
|
b9c2240040 | |
|
|
dd36fb7859 | |
|
|
30a54b72f0 | |
|
|
3d5ca9f433 | |
|
|
3a88cd0e2b | |
|
|
988afe1454 | |
|
|
528745b059 | |
|
|
068aa69b35 | |
|
|
fc4c57e795 | |
|
|
4e25686b20 | |
|
|
f3c5a6e75f | |
|
|
416a454dc5 | |
|
|
3561748280 | |
|
|
41f43f4649 | |
|
|
b7cd42da39 | |
|
|
da6dfae750 | |
|
|
320e40a044 | |
|
|
294abed138 | |
|
|
27092b2cb7 | |
|
|
9da14cdff1 | |
|
|
508367664f |
|
|
@ -12,13 +12,13 @@ Key takeaways:
|
||||||
> Note: What follows is based on
|
> Note: What follows is based on
|
||||||
> https://raw.githubusercontent.com/jackyzha0/quartz/acfaa472253a432d350e9b6904c0cde14f8c487f/.github/pull_request_template.md
|
> https://raw.githubusercontent.com/jackyzha0/quartz/acfaa472253a432d350e9b6904c0cde14f8c487f/.github/pull_request_template.md
|
||||||
|
|
||||||
We more than welcome contributions, and are OK with the use of LLMs tools. How
|
We more than welcome contributions, and are OK with the use of LLM tools. How
|
||||||
you use those tools depends on whether or not they make you more productive.
|
you use those tools depends on whether or not they make you more productive.
|
||||||
|
|
||||||
But one thing that bugs us a lot are PRs that are made entirely with these
|
But one thing that bugs us a lot are PRs that are made entirely with these
|
||||||
tools, without any revision or any effort trying to refine their output
|
tools, without any revision or any effort trying to refine their output
|
||||||
whatsoever. This is just pure laziness, and unacceptable. Doing so will just
|
whatsoever. This is just pure laziness, and unacceptable. Doing so will just
|
||||||
end up wasting everyone time (ours and yours).
|
end up wasting everyone's time (ours and yours).
|
||||||
|
|
||||||
So to be the most productive for all parties, we would encourage any
|
So to be the most productive for all parties, we would encourage any
|
||||||
contributors to, at the very least, pay attention to what the model is doing,
|
contributors to, at the very least, pay attention to what the model is doing,
|
||||||
|
|
|
||||||
|
|
@ -17,23 +17,23 @@ jobs:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: pylint
|
TOXENV: pylint
|
||||||
- python-version: "3.10"
|
- python-version: "3.10"
|
||||||
env:
|
env:
|
||||||
TOXENV: typing
|
TOXENV: mypy
|
||||||
- python-version: "3.10"
|
- python-version: "3.10"
|
||||||
env:
|
env:
|
||||||
TOXENV: typing-tests
|
TOXENV: mypy-tests
|
||||||
# Keep in sync with pyproject.toml tool.sphinx-scrapy.python-version.
|
# Keep in sync with pyproject.toml tool.sphinx-scrapy.python-version.
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: docs
|
TOXENV: docs
|
||||||
- python-version: "3.13"
|
- python-version: "3.13"
|
||||||
env:
|
env:
|
||||||
TOXENV: docs-tests
|
TOXENV: docs-tests
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: twinecheck
|
TOXENV: twinecheck
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ jobs:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v6
|
||||||
- uses: actions/setup-python@v6
|
- uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: "3.13"
|
python-version: "3.14"
|
||||||
- run: |
|
- run: |
|
||||||
python -m pip install --upgrade build
|
python -m pip install --upgrade build
|
||||||
python -m build
|
python -m build
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,11 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
||||||
env:
|
env:
|
||||||
- TOXENV: py
|
- TOXENV: py
|
||||||
include:
|
include:
|
||||||
- python-version: '3.13'
|
- python-version: '3.14'
|
||||||
env:
|
env:
|
||||||
TOXENV: no-reactor
|
TOXENV: no-reactor
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,13 @@ jobs:
|
||||||
- python-version: "3.13"
|
- python-version: "3.13"
|
||||||
env:
|
env:
|
||||||
TOXENV: py
|
TOXENV: py
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
|
env:
|
||||||
|
TOXENV: py
|
||||||
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: default-reactor
|
TOXENV: default-reactor
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: no-reactor
|
TOXENV: no-reactor
|
||||||
# pinned due to https://github.com/pypy/pypy/issues/5388
|
# pinned due to https://github.com/pypy/pypy/issues/5388
|
||||||
|
|
@ -42,43 +45,40 @@ jobs:
|
||||||
env:
|
env:
|
||||||
TOXENV: pypy3
|
TOXENV: pypy3
|
||||||
|
|
||||||
# pinned deps
|
# min deps
|
||||||
- python-version: "3.10.19"
|
- python-version: "3.10.19"
|
||||||
env:
|
env:
|
||||||
TOXENV: pinned
|
TOXENV: min
|
||||||
- python-version: "3.10.19"
|
- python-version: "3.10.19"
|
||||||
env:
|
env:
|
||||||
TOXENV: default-reactor-pinned
|
TOXENV: min-default-reactor
|
||||||
- python-version: "3.10.19"
|
- python-version: "3.10.19"
|
||||||
env:
|
env:
|
||||||
TOXENV: no-reactor-pinned
|
TOXENV: min-no-reactor
|
||||||
# pinned due to https://github.com/pypy/pypy/issues/5388
|
# pinned due to https://github.com/pypy/pypy/issues/5388
|
||||||
- python-version: pypy3.11-7.3.20
|
- python-version: pypy3.11-7.3.20
|
||||||
env:
|
env:
|
||||||
TOXENV: pypy3-pinned
|
TOXENV: min-pypy3
|
||||||
- python-version: "3.10.19"
|
- python-version: "3.10.19"
|
||||||
env:
|
env:
|
||||||
TOXENV: extra-deps-pinned
|
TOXENV: min-extra-deps
|
||||||
- python-version: "3.10.19"
|
- python-version: "3.10.19"
|
||||||
env:
|
env:
|
||||||
TOXENV: botocore-pinned
|
TOXENV: min-botocore
|
||||||
|
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: extra-deps
|
TOXENV: extra-deps
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: no-reactor-extra-deps
|
TOXENV: no-reactor-extra-deps
|
||||||
# pinned due to https://github.com/pypy/pypy/issues/5388
|
# pinned due to https://github.com/pypy/pypy/issues/5388
|
||||||
- python-version: pypy3.11-7.3.20
|
- python-version: pypy3.11-7.3.20
|
||||||
env:
|
env:
|
||||||
TOXENV: pypy3-extra-deps
|
TOXENV: pypy3-extra-deps
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: botocore
|
TOXENV: botocore
|
||||||
- python-version: "3.13"
|
|
||||||
env:
|
|
||||||
TOXENV: mitmproxy
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v6
|
||||||
|
|
@ -89,11 +89,14 @@ jobs:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
- name: Install system libraries
|
- name: Install system libraries
|
||||||
if: contains(matrix.python-version, 'pypy') || contains(matrix.env.TOXENV, 'pinned')
|
if: contains(matrix.python-version, 'pypy') || contains(matrix.env.TOXENV, 'min')
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install libxml2-dev libxslt-dev
|
sudo apt-get install libxml2-dev libxslt-dev
|
||||||
|
|
||||||
|
- name: Install mitmproxy
|
||||||
|
run: pipx install mitmproxy
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
env: ${{ matrix.env }}
|
env: ${{ matrix.env }}
|
||||||
run: |
|
run: |
|
||||||
|
|
|
||||||
|
|
@ -31,22 +31,25 @@ jobs:
|
||||||
- python-version: "3.13"
|
- python-version: "3.13"
|
||||||
env:
|
env:
|
||||||
TOXENV: py
|
TOXENV: py
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
|
env:
|
||||||
|
TOXENV: py
|
||||||
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: default-reactor
|
TOXENV: default-reactor
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: no-reactor
|
TOXENV: no-reactor
|
||||||
|
|
||||||
# pinned deps
|
# min deps
|
||||||
- python-version: "3.10.11"
|
- python-version: "3.10.11"
|
||||||
env:
|
env:
|
||||||
TOXENV: pinned
|
TOXENV: min
|
||||||
- python-version: "3.10.11"
|
- python-version: "3.10.11"
|
||||||
env:
|
env:
|
||||||
TOXENV: extra-deps-pinned
|
TOXENV: min-extra-deps
|
||||||
|
|
||||||
- python-version: "3.13"
|
- python-version: "3.14"
|
||||||
env:
|
env:
|
||||||
TOXENV: extra-deps
|
TOXENV: extra-deps
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ exclude: |
|
||||||
)
|
)
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.15.2
|
rev: v0.15.20
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff-check
|
- id: ruff-check
|
||||||
args: [ --fix ]
|
args: [ --fix ]
|
||||||
|
|
@ -16,7 +16,7 @@ repos:
|
||||||
hooks:
|
hooks:
|
||||||
- id: blacken-docs
|
- id: blacken-docs
|
||||||
additional_dependencies:
|
additional_dependencies:
|
||||||
- black==25.9.0
|
- black==26.5.1
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v6.0.0
|
rev: v6.0.0
|
||||||
hooks:
|
hooks:
|
||||||
|
|
@ -27,6 +27,6 @@ repos:
|
||||||
hooks:
|
hooks:
|
||||||
- id: sphinx-lint
|
- id: sphinx-lint
|
||||||
- repo: https://github.com/scrapy/sphinx-scrapy
|
- repo: https://github.com/scrapy/sphinx-scrapy
|
||||||
rev: 0.7.1
|
rev: 0.8.8
|
||||||
hooks:
|
hooks:
|
||||||
- id: sphinx-scrapy
|
- id: sphinx-scrapy
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ version: 2
|
||||||
build:
|
build:
|
||||||
os: ubuntu-24.04
|
os: ubuntu-24.04
|
||||||
tools:
|
tools:
|
||||||
python: "3.13"
|
python: "3.14"
|
||||||
commands:
|
commands:
|
||||||
- pip install tox
|
- pip install tox
|
||||||
- tox -e docs
|
- tox -e docs
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
cff-version: 1.2.0
|
||||||
|
message: If you use Scrapy in published research, please cite it as below.
|
||||||
|
title: Scrapy
|
||||||
|
authors:
|
||||||
|
- name: Scrapy contributors
|
||||||
|
url: https://scrapy.org
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
|
|
||||||
| Version | Supported |
|
| Version | Supported |
|
||||||
| ------- | ------------------ |
|
| ------- | ------------------ |
|
||||||
| 2.15.x | :white_check_mark: |
|
| 2.17.x | :white_check_mark: |
|
||||||
| < 2.15.x | :x: |
|
| < 2.17.x | :x: |
|
||||||
|
|
||||||
## Reporting a Vulnerability
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
|
|
||||||
34
conftest.py
34
conftest.py
|
|
@ -11,6 +11,7 @@ from scrapy.utils.reactor import set_asyncio_event_loop_policy
|
||||||
from scrapy.utils.reactorless import install_reactor_import_hook
|
from scrapy.utils.reactorless import install_reactor_import_hook
|
||||||
from tests.keys import generate_keys
|
from tests.keys import generate_keys
|
||||||
from tests.mockserver.http import MockServer
|
from tests.mockserver.http import MockServer
|
||||||
|
from tests.mockserver.mitm_proxy import MitmProxy, mitmdump_cmd
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
|
|
@ -23,16 +24,13 @@ def _py_files(folder):
|
||||||
collect_ignore = [
|
collect_ignore = [
|
||||||
# may need extra deps
|
# may need extra deps
|
||||||
"docs/_ext",
|
"docs/_ext",
|
||||||
# not a test, but looks like a test
|
# contains scripts to be run by tests/test_crawler_subprocess.py::AsyncCrawlerProcessSubprocess
|
||||||
"scrapy/utils/testproc.py",
|
|
||||||
"scrapy/utils/testsite.py",
|
|
||||||
# contains scripts to be run by tests/test_crawler.py::AsyncCrawlerProcessSubprocess
|
|
||||||
*_py_files("tests/AsyncCrawlerProcess"),
|
*_py_files("tests/AsyncCrawlerProcess"),
|
||||||
# contains scripts to be run by tests/test_crawler.py::AsyncCrawlerRunnerSubprocess
|
# contains scripts to be run by tests/test_crawler_subprocess.py::AsyncCrawlerRunnerSubprocess
|
||||||
*_py_files("tests/AsyncCrawlerRunner"),
|
*_py_files("tests/AsyncCrawlerRunner"),
|
||||||
# contains scripts to be run by tests/test_crawler.py::CrawlerProcessSubprocess
|
# contains scripts to be run by tests/test_crawler_subprocess.py::CrawlerProcessSubprocess
|
||||||
*_py_files("tests/CrawlerProcess"),
|
*_py_files("tests/CrawlerProcess"),
|
||||||
# contains scripts to be run by tests/test_crawler.py::CrawlerRunnerSubprocess
|
# contains scripts to be run by tests/test_crawler_subprocess.py::CrawlerRunnerSubprocess
|
||||||
*_py_files("tests/CrawlerRunner"),
|
*_py_files("tests/CrawlerRunner"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -75,6 +73,24 @@ def mockserver() -> Generator[MockServer]:
|
||||||
yield mockserver
|
yield mockserver
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture # function scope because it modifies os.environ
|
||||||
|
def proxy_server(
|
||||||
|
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
|
||||||
|
) -> Generator[str]:
|
||||||
|
kind = request.param
|
||||||
|
proxy = MitmProxy(mode="socks5" if kind == "socks5" else None)
|
||||||
|
url = proxy.start()
|
||||||
|
if kind == "https":
|
||||||
|
url = url.replace("http://", "https://")
|
||||||
|
monkeypatch.setenv("http_proxy", url)
|
||||||
|
monkeypatch.setenv("https_proxy", url)
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield kind
|
||||||
|
finally:
|
||||||
|
proxy.stop()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def reactor_pytest(request) -> str:
|
def reactor_pytest(request) -> str:
|
||||||
return request.config.getoption("--reactor")
|
return request.config.getoption("--reactor")
|
||||||
|
|
@ -111,7 +127,6 @@ def pytest_runtest_setup(item):
|
||||||
"uvloop",
|
"uvloop",
|
||||||
"botocore",
|
"botocore",
|
||||||
"boto3",
|
"boto3",
|
||||||
"mitmproxy",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
for module in optional_deps:
|
for module in optional_deps:
|
||||||
|
|
@ -121,6 +136,9 @@ def pytest_runtest_setup(item):
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pytest.skip(f"{module} is not installed")
|
pytest.skip(f"{module} is not installed")
|
||||||
|
|
||||||
|
if item.get_closest_marker("requires_mitmproxy") and mitmdump_cmd() is None:
|
||||||
|
pytest.skip("mitmdump is not available")
|
||||||
|
|
||||||
|
|
||||||
# Generate localhost certificate files, needed by some tests
|
# Generate localhost certificate files, needed by some tests
|
||||||
generate_keys()
|
generate_keys()
|
||||||
|
|
|
||||||
|
|
@ -1,68 +0,0 @@
|
||||||
:orphan:
|
|
||||||
|
|
||||||
======================================
|
|
||||||
Scrapy documentation quick start guide
|
|
||||||
======================================
|
|
||||||
|
|
||||||
This file provides a quick guide on how to compile the Scrapy documentation.
|
|
||||||
|
|
||||||
|
|
||||||
Setup the environment
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
To compile the documentation you need Sphinx Python library. To install it
|
|
||||||
and all its dependencies run the following command from this dir
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
pip install -r requirements.txt
|
|
||||||
|
|
||||||
|
|
||||||
Compile the documentation
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
To compile the documentation (to classic HTML output) run the following command
|
|
||||||
from this dir::
|
|
||||||
|
|
||||||
make html
|
|
||||||
|
|
||||||
Documentation will be generated (in HTML format) inside the ``build/html`` dir.
|
|
||||||
|
|
||||||
|
|
||||||
View the documentation
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
To view the documentation run the following command::
|
|
||||||
|
|
||||||
make htmlview
|
|
||||||
|
|
||||||
This command will fire up your default browser and open the main page of your
|
|
||||||
(previously generated) HTML documentation.
|
|
||||||
|
|
||||||
|
|
||||||
Start over
|
|
||||||
----------
|
|
||||||
|
|
||||||
To clean up all generated documentation files and start from scratch run::
|
|
||||||
|
|
||||||
make clean
|
|
||||||
|
|
||||||
Keep in mind that this command won't touch any documentation source files.
|
|
||||||
|
|
||||||
|
|
||||||
Recreating documentation on the fly
|
|
||||||
-----------------------------------
|
|
||||||
|
|
||||||
There is a way to recreate the doc automatically when you make changes, you
|
|
||||||
need to install watchdog (``pip install watchdog``) and then use::
|
|
||||||
|
|
||||||
make watch
|
|
||||||
|
|
||||||
Alternative method using tox
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
To compile the documentation to HTML run the following command::
|
|
||||||
|
|
||||||
tox -e docs
|
|
||||||
|
|
||||||
Documentation will be generated inside the ``docs/_build/all`` dir.
|
|
||||||
|
|
@ -11,7 +11,7 @@ from sphinx.application import Sphinx
|
||||||
|
|
||||||
def maybe_skip_member(app: Sphinx, what, name: str, obj, skip: bool, options) -> bool:
|
def maybe_skip_member(app: Sphinx, what, name: str, obj, skip: bool, options) -> bool:
|
||||||
if not skip:
|
if not skip:
|
||||||
# autodocs was generating a text "alias of" for the following members
|
# autodoc was generating the text "alias of" for the following members
|
||||||
return name in {"default_item_class", "default_selector_class"}
|
return name in {"default_item_class", "default_selector_class"}
|
||||||
return skip
|
return skip
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "!layout.html" %}
|
{% extends "!layout.html" %}
|
||||||
|
|
||||||
{# Overriden to include a link to scrapy.org, not just to the docs root #}
|
{# Overridden to include a link to scrapy.org, not just to the docs root #}
|
||||||
{%- block sidebartitle %}
|
{%- block sidebartitle %}
|
||||||
|
|
||||||
{# the logo helper function was removed in Sphinx 6 and deprecated since Sphinx 4 #}
|
{# the logo helper function was removed in Sphinx 6 and deprecated since Sphinx 4 #}
|
||||||
|
|
|
||||||
|
|
@ -154,9 +154,11 @@ scrapy_intersphinx_enable = [
|
||||||
"coverage",
|
"coverage",
|
||||||
"cryptography",
|
"cryptography",
|
||||||
"cssselect",
|
"cssselect",
|
||||||
|
"form2request",
|
||||||
"itemloaders",
|
"itemloaders",
|
||||||
"parsel",
|
"parsel",
|
||||||
"pytest",
|
"pytest",
|
||||||
|
"scrapy-lint",
|
||||||
"sphinx",
|
"sphinx",
|
||||||
"tox",
|
"tox",
|
||||||
"twisted",
|
"twisted",
|
||||||
|
|
|
||||||
|
|
@ -323,9 +323,10 @@ deprecation removals are documented in the :ref:`release notes <news>`.
|
||||||
Tests
|
Tests
|
||||||
=====
|
=====
|
||||||
|
|
||||||
Tests are implemented using the :doc:`Twisted unit-testing framework
|
Tests are implemented using pytest_. Running tests requires :doc:`tox
|
||||||
<twisted:development/test-standard>`. Running tests requires
|
<tox:index>`.
|
||||||
:doc:`tox <tox:index>`.
|
|
||||||
|
.. _pytest: https://pytest.org
|
||||||
|
|
||||||
.. _running-tests:
|
.. _running-tests:
|
||||||
|
|
||||||
|
|
@ -371,6 +372,21 @@ To see coverage report install :doc:`coverage <coverage:index>`
|
||||||
|
|
||||||
see output of ``coverage --help`` for more options like html or xml report.
|
see output of ``coverage --help`` for more options like html or xml report.
|
||||||
|
|
||||||
|
Some tests need a ``mitmdump`` executable (from mitmproxy_) to test against a
|
||||||
|
fully featured proxy server; they are skipped when one cannot be found
|
||||||
|
(``mitmproxy`` is intentionally not a test dependency that would be installed
|
||||||
|
into test venvs, as that sometimes leads to various dependency conflicts).
|
||||||
|
To run these tests, make ``mitmdump`` available in one of these ways:
|
||||||
|
|
||||||
|
* install ``mitmproxy`` so that ``mitmdump`` is on your ``PATH``, e.g. with
|
||||||
|
pipx_ (``pipx install mitmproxy``) or uv_ (``uv tool install mitmproxy``);
|
||||||
|
|
||||||
|
* have uv_ installed, in which case the tests will run
|
||||||
|
``uvx --from mitmproxy mitmdump``;
|
||||||
|
|
||||||
|
* set the ``MITMDUMP`` environment variable to the path of a ``mitmdump``
|
||||||
|
executable.
|
||||||
|
|
||||||
Writing tests
|
Writing tests
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
|
@ -398,3 +414,6 @@ And their unit-tests are in::
|
||||||
.. _pytest-xdist: https://github.com/pytest-dev/pytest-xdist
|
.. _pytest-xdist: https://github.com/pytest-dev/pytest-xdist
|
||||||
.. _help wanted issues: https://github.com/scrapy/scrapy/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22
|
.. _help wanted issues: https://github.com/scrapy/scrapy/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22
|
||||||
.. _test coverage: https://app.codecov.io/gh/scrapy/scrapy
|
.. _test coverage: https://app.codecov.io/gh/scrapy/scrapy
|
||||||
|
.. _mitmproxy: https://mitmproxy.org/
|
||||||
|
.. _pipx: https://pipx.pypa.io/
|
||||||
|
.. _uv: https://docs.astral.sh/uv/
|
||||||
|
|
|
||||||
31
docs/faq.rst
31
docs/faq.rst
|
|
@ -82,10 +82,18 @@ to steal from us!
|
||||||
Does Scrapy work with HTTP proxies?
|
Does Scrapy work with HTTP proxies?
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
Yes. Support for HTTP proxies is provided (since Scrapy 0.8) through the HTTP
|
Yes. Support for HTTP proxies is provided through the HTTP Proxy downloader
|
||||||
Proxy downloader middleware. See
|
middleware. See
|
||||||
:class:`~scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware`.
|
:class:`~scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware`.
|
||||||
|
|
||||||
|
Does Scrapy work with SOCKS proxies?
|
||||||
|
------------------------------------
|
||||||
|
|
||||||
|
Yes, when using
|
||||||
|
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`. See
|
||||||
|
:class:`~scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware` and the
|
||||||
|
handler documentation.
|
||||||
|
|
||||||
How can I scrape an item with attributes in different pages?
|
How can I scrape an item with attributes in different pages?
|
||||||
------------------------------------------------------------
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -277,7 +285,8 @@ consume a lot of memory.
|
||||||
In order to avoid parsing all the entire feed at once in memory, you can use
|
In order to avoid parsing all the entire feed at once in memory, you can use
|
||||||
the :func:`~scrapy.utils.iterators.xmliter_lxml` and
|
the :func:`~scrapy.utils.iterators.xmliter_lxml` and
|
||||||
:func:`~scrapy.utils.iterators.csviter` functions. In fact, this is what
|
:func:`~scrapy.utils.iterators.csviter` functions. In fact, this is what
|
||||||
:class:`~scrapy.spiders.XMLFeedSpider` uses.
|
:class:`~scrapy.spiders.XMLFeedSpider` and
|
||||||
|
:class:`~scrapy.spiders.CSVFeedSpider` use.
|
||||||
|
|
||||||
.. autofunction:: scrapy.utils.iterators.xmliter_lxml
|
.. autofunction:: scrapy.utils.iterators.xmliter_lxml
|
||||||
|
|
||||||
|
|
@ -352,15 +361,19 @@ method for this purpose. For example:
|
||||||
def process_spider_output(self, response, result):
|
def process_spider_output(self, response, result):
|
||||||
for item_or_request in result:
|
for item_or_request in result:
|
||||||
if isinstance(item_or_request, Request):
|
if isinstance(item_or_request, Request):
|
||||||
|
yield item_or_request
|
||||||
continue
|
continue
|
||||||
adapter = ItemAdapter(item)
|
adapter = ItemAdapter(item_or_request)
|
||||||
for _ in range(adapter["multiply_by"]):
|
for _ in range(adapter["multiply_by"]):
|
||||||
yield deepcopy(item)
|
yield deepcopy(item_or_request)
|
||||||
|
|
||||||
Does Scrapy support IPv6 addresses?
|
Does Scrapy support IPv6 addresses?
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
Yes, by setting :setting:`TWISTED_DNS_RESOLVER` to ``scrapy.resolver.CachingHostnameResolver``.
|
Yes, but when using
|
||||||
|
:class:`~scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler` or
|
||||||
|
:class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler` you need to
|
||||||
|
set :setting:`TWISTED_DNS_RESOLVER` to ``scrapy.resolver.CachingHostnameResolver``.
|
||||||
Note that by doing so, you lose the ability to set a specific timeout for DNS requests
|
Note that by doing so, you lose the ability to set a specific timeout for DNS requests
|
||||||
(the value of the :setting:`DNS_TIMEOUT` setting is ignored).
|
(the value of the :setting:`DNS_TIMEOUT` setting is ignored).
|
||||||
|
|
||||||
|
|
@ -371,8 +384,9 @@ How to deal with ``<class 'ValueError'>: filedescriptor out of range in select()
|
||||||
----------------------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
This issue `has been reported`_ to appear when running broad crawls in macOS, where the default
|
This issue `has been reported`_ to appear when running broad crawls in macOS, where the default
|
||||||
Twisted reactor is :class:`twisted.internet.selectreactor.SelectReactor`. Switching to a
|
Twisted reactor was :class:`twisted.internet.selectreactor.SelectReactor` at that time.
|
||||||
different reactor is possible by using the :setting:`TWISTED_REACTOR` setting.
|
If you have switched to this reactor using the :setting:`TWISTED_REACTOR` setting you can switch
|
||||||
|
to a different one in the same way.
|
||||||
|
|
||||||
|
|
||||||
.. _faq-stop-response-download:
|
.. _faq-stop-response-download:
|
||||||
|
|
@ -398,7 +412,6 @@ How can I make a blank request?
|
||||||
|
|
||||||
from scrapy import Request
|
from scrapy import Request
|
||||||
|
|
||||||
|
|
||||||
blank_request = Request("data:,")
|
blank_request = Request("data:,")
|
||||||
|
|
||||||
In this case, the URL is set to a data URI scheme. Data URLs allow you to include data
|
In this case, the URL is set to a data URI scheme. Data URLs allow you to include data
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ Having trouble? We'd like to help!
|
||||||
* Ask or search questions in `StackOverflow using the scrapy tag`_.
|
* Ask or search questions in `StackOverflow using the scrapy tag`_.
|
||||||
* Ask or search questions in the `Scrapy subreddit`_.
|
* Ask or search questions in the `Scrapy subreddit`_.
|
||||||
* Search for questions on the archives of the `scrapy-users mailing list`_.
|
* Search for questions on the archives of the `scrapy-users mailing list`_.
|
||||||
* Ask a question in the `#scrapy IRC channel`_,
|
* Ask a question in the `#scrapy IRC channel`_.
|
||||||
* Report bugs with Scrapy in our `issue tracker`_.
|
* Report bugs with Scrapy in our `issue tracker`_.
|
||||||
* Join the Discord community `Scrapy Discord`_.
|
* Join the Discord community `Scrapy Discord`_.
|
||||||
|
|
||||||
|
|
@ -91,15 +91,15 @@ Basic concepts
|
||||||
:doc:`topics/selectors`
|
:doc:`topics/selectors`
|
||||||
Extract the data from web pages using XPath.
|
Extract the data from web pages using XPath.
|
||||||
|
|
||||||
:doc:`topics/shell`
|
|
||||||
Test your extraction code in an interactive environment.
|
|
||||||
|
|
||||||
:doc:`topics/items`
|
:doc:`topics/items`
|
||||||
Define the data you want to scrape.
|
Define the data you want to scrape.
|
||||||
|
|
||||||
:doc:`topics/loaders`
|
:doc:`topics/loaders`
|
||||||
Populate your items with the extracted data.
|
Populate your items with the extracted data.
|
||||||
|
|
||||||
|
:doc:`topics/shell`
|
||||||
|
Test your extraction code in an interactive environment.
|
||||||
|
|
||||||
:doc:`topics/item-pipeline`
|
:doc:`topics/item-pipeline`
|
||||||
Post-process and store your scraped data.
|
Post-process and store your scraped data.
|
||||||
|
|
||||||
|
|
@ -151,6 +151,7 @@ Solving specific problems
|
||||||
topics/debug
|
topics/debug
|
||||||
topics/contracts
|
topics/contracts
|
||||||
topics/practices
|
topics/practices
|
||||||
|
topics/security
|
||||||
topics/broad-crawls
|
topics/broad-crawls
|
||||||
topics/developer-tools
|
topics/developer-tools
|
||||||
topics/dynamic-content
|
topics/dynamic-content
|
||||||
|
|
@ -175,6 +176,10 @@ Solving specific problems
|
||||||
:doc:`topics/practices`
|
:doc:`topics/practices`
|
||||||
Get familiar with some Scrapy common practices.
|
Get familiar with some Scrapy common practices.
|
||||||
|
|
||||||
|
:doc:`topics/security`
|
||||||
|
Understand the security implications of Scrapy defaults and how to harden
|
||||||
|
them.
|
||||||
|
|
||||||
:doc:`topics/broad-crawls`
|
:doc:`topics/broad-crawls`
|
||||||
Tune Scrapy for crawling a lot domains in parallel.
|
Tune Scrapy for crawling a lot domains in parallel.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -230,8 +230,8 @@ Installing Scrapy with PyPy on Windows is not tested.
|
||||||
You can check that Scrapy is installed correctly by running ``scrapy bench``.
|
You can check that Scrapy is installed correctly by running ``scrapy bench``.
|
||||||
If this command gives errors such as
|
If this command gives errors such as
|
||||||
``TypeError: ... got 2 unexpected keyword arguments``, this means
|
``TypeError: ... got 2 unexpected keyword arguments``, this means
|
||||||
that setuptools was unable to pick up one PyPy-specific dependency.
|
that the ``PyPyDispatcher`` dependency wasn't installed. To fix this issue, run
|
||||||
To fix this issue, run ``pip install 'PyPyDispatcher>=2.1.0'``.
|
``pip install 'PyPyDispatcher>=2.1.0'``.
|
||||||
|
|
||||||
|
|
||||||
.. _intro-install-troubleshooting:
|
.. _intro-install-troubleshooting:
|
||||||
|
|
|
||||||
|
|
@ -83,16 +83,17 @@ While this enables you to do very fast crawls (sending multiple concurrent
|
||||||
requests at the same time, in a fault-tolerant way) Scrapy also gives you
|
requests at the same time, in a fault-tolerant way) Scrapy also gives you
|
||||||
control over the politeness of the crawl through :ref:`a few settings
|
control over the politeness of the crawl through :ref:`a few settings
|
||||||
<topics-settings-ref>`. You can do things like setting a download delay between
|
<topics-settings-ref>`. You can do things like setting a download delay between
|
||||||
each request, limiting the amount of concurrent requests per domain or per IP, and
|
each request, limiting the amount of concurrent requests per domain, and
|
||||||
even :ref:`using an auto-throttling extension <topics-autothrottle>` that tries
|
even :ref:`using an auto-throttling extension <topics-autothrottle>` that tries
|
||||||
to figure these settings out automatically.
|
to figure these settings out automatically.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
This is using :ref:`feed exports <topics-feed-exports>` to generate the
|
This is using :ref:`feed exports <topics-feed-exports>` to generate the
|
||||||
JSON file, you can easily change the export format (XML or CSV, for example) or the
|
JSON Lines file, you can easily change the export format (XML or CSV, for
|
||||||
storage backend (FTP or `Amazon S3`_, for example). You can also write an
|
example) or the storage backend (FTP or `Amazon S3`_, for example). You can
|
||||||
:ref:`item pipeline <topics-item-pipeline>` to store the items in a database.
|
also write an :ref:`item pipeline <topics-item-pipeline>` to store the
|
||||||
|
items in a database.
|
||||||
|
|
||||||
|
|
||||||
.. _topics-whatelse:
|
.. _topics-whatelse:
|
||||||
|
|
|
||||||
676
docs/news.rst
676
docs/news.rst
|
|
@ -3,6 +3,643 @@
|
||||||
Release notes
|
Release notes
|
||||||
=============
|
=============
|
||||||
|
|
||||||
|
.. _release-2.17.0:
|
||||||
|
|
||||||
|
Scrapy 2.17.0 (2026-07-07)
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Highlights:
|
||||||
|
|
||||||
|
- Security bug fixes
|
||||||
|
|
||||||
|
- HTTP/2 and SOCKS proxy support for ``HttpxDownloadHandler``
|
||||||
|
|
||||||
|
- Improved settings for changing allowed TLS versions
|
||||||
|
|
||||||
|
Security bug fixes
|
||||||
|
~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- ``s3://`` requests now use HTTPS by default, instead of plaintext HTTP.
|
||||||
|
|
||||||
|
Previously, :class:`~scrapy.core.downloader.handlers.s3.S3DownloadHandler`
|
||||||
|
sent signed S3 requests over plaintext HTTP unless
|
||||||
|
``request.meta["is_secure"]`` was set to a true value, exposing the request
|
||||||
|
path, the AWS ``Authorization`` header, the ``X-Amz-Security-Token`` header
|
||||||
|
(when using temporary credentials), and the response contents to network
|
||||||
|
attackers, who could also tamper with responses. See the `76g3-c3x4-crvx`_
|
||||||
|
security advisory for details.
|
||||||
|
|
||||||
|
To restore the previous behavior for a given request, set
|
||||||
|
``request.meta["is_secure"]`` to ``False``.
|
||||||
|
|
||||||
|
.. _76g3-c3x4-crvx: https://github.com/scrapy/scrapy/security/advisories/GHSA-76g3-c3x4-crvx
|
||||||
|
|
||||||
|
Deprecations
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- The ``DOWNLOADER_CLIENT_TLS_METHOD`` setting is deprecated. You should use
|
||||||
|
the :setting:`DOWNLOAD_TLS_MIN_VERSION` and/or
|
||||||
|
:setting:`DOWNLOAD_TLS_MAX_VERSION` settings instead if you want to change
|
||||||
|
the TLS method selection.
|
||||||
|
(:issue:`3288`, :issue:`6546`)
|
||||||
|
|
||||||
|
- The following spider attributes are deprecated in favor of settings:
|
||||||
|
|
||||||
|
- ``http_user`` (use :setting:`HTTPAUTH_USER`)
|
||||||
|
|
||||||
|
- ``http_pass`` (use :setting:`HTTPAUTH_PASS`)
|
||||||
|
|
||||||
|
- ``http_auth_domain`` (use :setting:`HTTPAUTH_DOMAIN`)
|
||||||
|
|
||||||
|
(:issue:`7590`)
|
||||||
|
|
||||||
|
- The ``scrapy.commands.ScrapyCommand.help()`` method is deprecated. It was
|
||||||
|
never called by Scrapy.
|
||||||
|
(:issue:`7626`, :issue:`7633`)
|
||||||
|
|
||||||
|
- The following TLS-related functions and constants, intended for internal
|
||||||
|
use, are deprecated:
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.tls.METHOD_TLS``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.tls.METHOD_TLSv10``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.tls.METHOD_TLSv11``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.tls.METHOD_TLSv12``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.tls.openssl_methods``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.tls.DEFAULT_CIPHERS``
|
||||||
|
|
||||||
|
- ``scrapy.utils.ssl.ffi_buf_to_string()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.ssl.get_temp_key_info()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.ssl.x509name_to_string()``
|
||||||
|
|
||||||
|
(:issue:`6546`, :issue:`7619`, :issue:`7665`)
|
||||||
|
|
||||||
|
- The ``CRAWLSPIDER_FOLLOW_LINKS`` setting is deprecated. You can set
|
||||||
|
``follow=False`` in your rules to achieve the same effect.
|
||||||
|
(:issue:`7592`)
|
||||||
|
|
||||||
|
- Instantiating
|
||||||
|
:class:`~scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware`
|
||||||
|
without a ``crawler`` argument is deprecated.
|
||||||
|
(:issue:`7655`)
|
||||||
|
|
||||||
|
- Instantiating
|
||||||
|
:class:`~scrapy.spidermiddlewares.referer.RefererMiddleware` without a
|
||||||
|
``settings`` argument is deprecated.
|
||||||
|
(:issue:`7664`)
|
||||||
|
|
||||||
|
New features
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Added support for HTTP/2 requests to
|
||||||
|
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`. It
|
||||||
|
requires setting the new :setting:`HTTPX_HTTP2_ENABLED` setting to
|
||||||
|
``True``.
|
||||||
|
(:issue:`7575`)
|
||||||
|
|
||||||
|
- Added support for SOCKS proxies to
|
||||||
|
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`.
|
||||||
|
(:issue:`747`, :issue:`7575`)
|
||||||
|
|
||||||
|
- Added :setting:`DOWNLOAD_TLS_MIN_VERSION` and
|
||||||
|
:setting:`DOWNLOAD_TLS_MAX_VERSION` settings as replacements for the
|
||||||
|
``DOWNLOADER_CLIENT_TLS_METHOD`` setting (which is now deprecated).
|
||||||
|
Compared to the old setting, they support specifying a range of allowed
|
||||||
|
versions and support newer TLS versions.
|
||||||
|
(:issue:`4821`, :issue:`6546`)
|
||||||
|
|
||||||
|
- Added :setting:`HTTPAUTH_USER`, :setting:`HTTPAUTH_PASS` and
|
||||||
|
:setting:`HTTPAUTH_DOMAIN` settings and :reqmeta:`http_user`,
|
||||||
|
:reqmeta:`http_pass` and :reqmeta:`http_auth_domain` meta keys as more
|
||||||
|
flexible ways to set HTTP authentication data.
|
||||||
|
(:issue:`7590`)
|
||||||
|
|
||||||
|
- Added a :reqmeta:`verbatim_url` meta key that can be set to ``True`` to
|
||||||
|
skip request URL canonicalization.
|
||||||
|
(:issue:`7473`)
|
||||||
|
|
||||||
|
- Added ``deny_tags`` and ``deny_attrs`` arguments to :class:`LinkExtractor
|
||||||
|
<scrapy.linkextractors.lxmlhtml.LxmlLinkExtractor>`.
|
||||||
|
(:issue:`6321`, :issue:`7679`)
|
||||||
|
|
||||||
|
- :attr:`scrapy.Item.fields` now returns the fields in the definition order
|
||||||
|
instead of the alphabetical one.
|
||||||
|
(:issue:`7015`, :issue:`7694`)
|
||||||
|
|
||||||
|
- Added a :setting:`RETRY_GIVE_UP_LOG_LEVEL` setting, a
|
||||||
|
:reqmeta:`give_up_log_level` meta key and a ``give_up_log_level`` argument
|
||||||
|
of the
|
||||||
|
:func:`~scrapy.downloadermiddlewares.retry.get_retry_request` function that
|
||||||
|
allow changing the log level of the message logged when the retry limit has
|
||||||
|
been reached.
|
||||||
|
(:issue:`4622`, :issue:`5297`, :issue:`7567`)
|
||||||
|
|
||||||
|
- It's now possible to set :setting:`DOWNLOADER_CLIENT_TLS_CIPHERS` to
|
||||||
|
``None`` to use the default ciphers of the underlying TLS implementation.
|
||||||
|
(:issue:`7499`, :issue:`7665`)
|
||||||
|
|
||||||
|
Improvements
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- :class:`~scrapy.FormRequest` is no longer deprecated, only its
|
||||||
|
``from_response()`` method is still deprecated.
|
||||||
|
(:issue:`7561`, :issue:`7671`)
|
||||||
|
|
||||||
|
- Switched the item definition in the default project template from a
|
||||||
|
:class:`scrapy.item.Item` to a dataclass.
|
||||||
|
(:issue:`7493`, :issue:`7513`)
|
||||||
|
|
||||||
|
- Fixed deprecation warnings with pyOpenSSL 26.3.0.
|
||||||
|
(:issue:`7619`)
|
||||||
|
|
||||||
|
- Removed the runtime warnings for :attr:`Spider.allowed_domains
|
||||||
|
<scrapy.Spider.allowed_domains>` containing URLs or domains with ports
|
||||||
|
instead of just domains and for spider classes having a ``start_url``
|
||||||
|
attribute instead of :class:`~scrapy.spiders.Spider.start_urls`. Please use
|
||||||
|
:doc:`scrapy-lint <scrapy-lint:index>` to find mistakes in your spider code
|
||||||
|
instead.
|
||||||
|
(:issue:`4421`, :issue:`7627`)
|
||||||
|
|
||||||
|
- :func:`scrapy.utils.test.get_crawler` now disables
|
||||||
|
:setting:`TELNETCONSOLE_ENABLED` by default.
|
||||||
|
(:issue:`7644`)
|
||||||
|
|
||||||
|
- Other code refactoring and improvements.
|
||||||
|
(:issue:`7409`, :issue:`7593`, :issue:`7594`, :issue:`7611`, :issue:`7649`)
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
- :class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler` no
|
||||||
|
longer ignores proxy credentials for redirected or retried requests.
|
||||||
|
(:issue:`7601`, :issue:`7630`)
|
||||||
|
|
||||||
|
- :class:`~scrapy.extensions.feedexport.GCSFeedStorage` now closes the
|
||||||
|
temporary file after the upload.
|
||||||
|
(:issue:`7546`)
|
||||||
|
|
||||||
|
- Fixed ``scrapy shell <URL>`` running a full spider crawl when there is a
|
||||||
|
spider for the requested URL. This bug was introduced in Scrapy 2.13.0.
|
||||||
|
(:issue:`7552`, :issue:`7557`)
|
||||||
|
|
||||||
|
- The :setting:`IMAGES_STORE_S3_ACL` and :setting:`IMAGES_STORE_GCS_ACL`
|
||||||
|
settings are no longer ignored. This bug was introduced in Scrapy 2.12.0.
|
||||||
|
(:issue:`7597`, :issue:`7614`)
|
||||||
|
|
||||||
|
- :class:`~scrapy.core.downloader.handlers.ftp.FTPDownloadHandler` now closes
|
||||||
|
the connection after making the request.
|
||||||
|
(:issue:`7602`, :issue:`7667`)
|
||||||
|
|
||||||
|
- Removed the deprecated ``spider`` argument from the pipeline defined in the
|
||||||
|
default project template.
|
||||||
|
(:issue:`7676`)
|
||||||
|
|
||||||
|
- Fixed ``scrapy genspider --edit`` not working.
|
||||||
|
(:issue:`7260`, :issue:`7683`)
|
||||||
|
|
||||||
|
- When a :class:`~scrapy.crawler.Crawler` instance is passed to
|
||||||
|
:meth:`AsyncCrawlerRunner.create_crawler()
|
||||||
|
<scrapy.crawler.AsyncCrawlerRunner.create_crawler>` or
|
||||||
|
:meth:`CrawlerRunner.create_crawler()
|
||||||
|
<scrapy.crawler.CrawlerRunner.create_crawler>`, settings from both classes
|
||||||
|
are now merged, previously only the settings from the
|
||||||
|
:class:`~scrapy.crawler.Crawler` instance were used.
|
||||||
|
(:issue:`1280`, :issue:`7647`)
|
||||||
|
|
||||||
|
- Fixed several issues with cookie handling in
|
||||||
|
:func:`scrapy.utils.request.request_to_curl`.
|
||||||
|
(:issue:`7603`, :issue:`7675`, :issue:`7684`)
|
||||||
|
|
||||||
|
- Fixed :class:`scrapy.resolver.CachingThreadedResolver` not disabling the
|
||||||
|
cache when :setting:`DNSCACHE_ENABLED` is set to ``False``.
|
||||||
|
(:issue:`7663`)
|
||||||
|
|
||||||
|
- Fixed :func:`scrapy.utils.response.open_in_browser` not removing comments
|
||||||
|
when looking for the ``<base>`` tag.
|
||||||
|
(:issue:`7506`)
|
||||||
|
|
||||||
|
- Fixed checking for deprecated methods in custom :setting:`ITEM_PROCESSOR`
|
||||||
|
implementations.
|
||||||
|
(:issue:`7589`)
|
||||||
|
|
||||||
|
- Fixed :func:`scrapy.utils.url.strip_url` corrupting some URLs with
|
||||||
|
credentials.
|
||||||
|
(:issue:`7604`, :issue:`7605`)
|
||||||
|
|
||||||
|
- :func:`scrapy.utils.misc.rel_has_nofollow` now ignores the case when
|
||||||
|
looking for "nofollow" strings.
|
||||||
|
(:issue:`7632`)
|
||||||
|
|
||||||
|
- Fixed an exception in :class:`scrapy.utils.sitemap.Sitemap` when parsing
|
||||||
|
some malformed sitemaps.
|
||||||
|
(:issue:`7686`, :issue:`7687`)
|
||||||
|
|
||||||
|
Documentation
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Mentioned :doc:`scrapy-lint <scrapy-lint:index>` in the docs.
|
||||||
|
(:issue:`4421`, :issue:`7627`)
|
||||||
|
|
||||||
|
- Added the docs about :ref:`security considerations <security>`.
|
||||||
|
(:issue:`7389`, :issue:`7678`)
|
||||||
|
|
||||||
|
- Improved the :ref:`item pipeline docs <topics-item-pipeline>`.
|
||||||
|
(:issue:`2350`, :issue:`7676`)
|
||||||
|
|
||||||
|
- Documented which stats are collected by
|
||||||
|
:class:`~scrapy.extensions.corestats.CoreStats`.
|
||||||
|
(:issue:`7421`)
|
||||||
|
|
||||||
|
- Switched documentation examples from using :class:`scrapy.item.Item` to
|
||||||
|
using dataclasses.
|
||||||
|
(:issue:`7493`, :issue:`7513`)
|
||||||
|
|
||||||
|
- Added feature comparison tables to the :ref:`download handler
|
||||||
|
<download-handlers-ref>` docs.
|
||||||
|
(:issue:`7575`)
|
||||||
|
|
||||||
|
- Improved the docs for :ref:`logging settings <logging-settings>`.
|
||||||
|
(:issue:`6909`, :issue:`7668`)
|
||||||
|
|
||||||
|
- Documented a way to :ref:`improve startup time and memory usage
|
||||||
|
<large-project-startup>` by using :setting:`SPIDER_MODULES`.
|
||||||
|
(:issue:`7576`, :issue:`7600`)
|
||||||
|
|
||||||
|
- Clarified handling of the ``type`` argument of :class:`~scrapy.Selector`.
|
||||||
|
(:issue:`7704`)
|
||||||
|
|
||||||
|
- Other documentation improvements and fixes.
|
||||||
|
(:issue:`4954`,
|
||||||
|
:issue:`6120`,
|
||||||
|
:issue:`7286`,
|
||||||
|
:issue:`7564`,
|
||||||
|
:issue:`7573`,
|
||||||
|
:issue:`7598`,
|
||||||
|
:issue:`7599`,
|
||||||
|
:issue:`7698`)
|
||||||
|
|
||||||
|
Quality assurance
|
||||||
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Fixed deprecation warnings with pytest 9.1.0.
|
||||||
|
(:issue:`7621`)
|
||||||
|
|
||||||
|
- Type hints improvements and fixes.
|
||||||
|
(:issue:`6958`, :issue:`7586`)
|
||||||
|
|
||||||
|
- CI and test improvements and fixes.
|
||||||
|
(:issue:`5954`,
|
||||||
|
:issue:`7002`,
|
||||||
|
:issue:`7017`,
|
||||||
|
:issue:`7247`,
|
||||||
|
:issue:`7508`,
|
||||||
|
:issue:`7545`,
|
||||||
|
:issue:`7566`,
|
||||||
|
:issue:`7574`,
|
||||||
|
:issue:`7585`,
|
||||||
|
:issue:`7595`,
|
||||||
|
:issue:`7608`,
|
||||||
|
:issue:`7610`,
|
||||||
|
:issue:`7612`,
|
||||||
|
:issue:`7616`,
|
||||||
|
:issue:`7625`,
|
||||||
|
:issue:`7637`,
|
||||||
|
:issue:`7639`,
|
||||||
|
:issue:`7640`,
|
||||||
|
:issue:`7641`,
|
||||||
|
:issue:`7642`,
|
||||||
|
:issue:`7643`,
|
||||||
|
:issue:`7644`,
|
||||||
|
:issue:`7645`,
|
||||||
|
:issue:`7646`,
|
||||||
|
:issue:`7654`,
|
||||||
|
:issue:`7655`,
|
||||||
|
:issue:`7664`,
|
||||||
|
:issue:`7672`,
|
||||||
|
:issue:`7677`,
|
||||||
|
:issue:`7680`,
|
||||||
|
:issue:`7682`,
|
||||||
|
:issue:`7692`)
|
||||||
|
|
||||||
|
.. _release-2.16.0:
|
||||||
|
|
||||||
|
Scrapy 2.16.0 (2026-05-19)
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Highlights:
|
||||||
|
|
||||||
|
- Official support for Python 3.14
|
||||||
|
|
||||||
|
- Support for Twisted 26.4.0+
|
||||||
|
|
||||||
|
Modified requirements
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Increased the minimum versions of the following dependencies:
|
||||||
|
|
||||||
|
- service_identity_: 18.1.0 → 23.1.0
|
||||||
|
|
||||||
|
(:issue:`7347`)
|
||||||
|
|
||||||
|
- Added support for Twisted 26.4.0+.
|
||||||
|
(:issue:`7347`, :issue:`7505`, :issue:`7520`)
|
||||||
|
|
||||||
|
- Added support for Python 3.14.
|
||||||
|
(:issue:`6604`, :issue:`7460`)
|
||||||
|
|
||||||
|
Backward-incompatible changes
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- The following classes and functions, intended for internal use by
|
||||||
|
:class:`~scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler`
|
||||||
|
and :class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler`, have
|
||||||
|
been made private:
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http11.ScrapyAgent``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http11.ScrapyProxyAgent``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http11.TunnelingAgent``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http11.TunnelingTCP4ClientEndpoint``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http11.tunnel_request_data()``
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http2.ScrapyH2Agent``
|
||||||
|
|
||||||
|
(:issue:`7496`, :issue:`7510`)
|
||||||
|
|
||||||
|
Deprecations
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- ``scrapy.FormRequest`` is deprecated. You can use the :doc:`form2request
|
||||||
|
<form2request:index>` library instead, see :ref:`form`.
|
||||||
|
(:issue:`6438`)
|
||||||
|
|
||||||
|
- ``scrapy.utils.python.MutableChain`` is deprecated.
|
||||||
|
(:issue:`7504`)
|
||||||
|
|
||||||
|
Deprecation removals
|
||||||
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- The ``start_requests()`` method of :class:`~scrapy.Spider`, deprecated in
|
||||||
|
2.13.0, is removed and no longer called. Use :meth:`~scrapy.Spider.start`
|
||||||
|
instead, or both to maintain support for lower Scrapy versions.
|
||||||
|
(:issue:`7490`)
|
||||||
|
|
||||||
|
- Support for ``process_start_requests()`` methods of :ref:`spider middlewares
|
||||||
|
<topics-spider-middleware>`, deprecated in 2.13.0, is removed. Use
|
||||||
|
:meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_start` instead,
|
||||||
|
or both to maintain support for lower Scrapy versions.
|
||||||
|
(:issue:`7490`)
|
||||||
|
|
||||||
|
- Support for synchronous ``process_spider_output()`` methods of spider
|
||||||
|
middlewares, deprecated in Scrapy 2.13.0, is removed. You should upgrade
|
||||||
|
the affected middlewares to have asynchronous ``process_spider_output()``
|
||||||
|
methods.
|
||||||
|
(:issue:`7504`)
|
||||||
|
|
||||||
|
- The ``spider`` arguments of the following methods of
|
||||||
|
:class:`~scrapy.core.scraper.Scraper`, deprecated in Scrapy 2.13.0, are
|
||||||
|
removed:
|
||||||
|
|
||||||
|
- ``close_spider()``
|
||||||
|
|
||||||
|
- ``enqueue_scrape()``
|
||||||
|
|
||||||
|
- ``handle_spider_error()``
|
||||||
|
|
||||||
|
- ``handle_spider_output()``
|
||||||
|
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
- HTTP/1.0 support code, deprecated in Scrapy 2.13.0, is removed. This
|
||||||
|
includes:
|
||||||
|
|
||||||
|
- ``scrapy.core.downloader.handlers.http10.HTTP10DownloadHandler``
|
||||||
|
|
||||||
|
- The ``scrapy.core.downloader.webclient`` module.
|
||||||
|
|
||||||
|
- The ``DOWNLOADER_HTTPCLIENTFACTORY`` setting.
|
||||||
|
|
||||||
|
(:issue:`7486`)
|
||||||
|
|
||||||
|
- The following functions, deprecated in Scrapy 2.13.0, are removed, you
|
||||||
|
should import them from :mod:`w3lib.url` directly instead:
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.add_or_replace_parameter()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.add_or_replace_parameters()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.any_to_uri()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.canonicalize_url()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.file_uri_to_path()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.is_url()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.parse_data_uri()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.parse_url()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.path_to_file_uri()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.safe_download_url()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.safe_url_string()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.url_query_cleaner()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.url.url_query_parameter()``
|
||||||
|
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
- The following test-related code, deprecated in Scrapy 2.13.0, is removed:
|
||||||
|
|
||||||
|
- the ``scrapy.utils.testproc`` module
|
||||||
|
|
||||||
|
- the ``scrapy.utils.testsite`` module
|
||||||
|
|
||||||
|
- ``scrapy.utils.test.assert_gcs_environ()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.test.get_ftp_content_and_delete()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.test.get_gcs_content_and_delete()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.test.mock_google_cloud_storage()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.test.skip_if_no_boto()``
|
||||||
|
|
||||||
|
- ``scrapy.utils.test.TestSpider``
|
||||||
|
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
- ``scrapy.utils.versions.scrapy_components_versions()``, deprecated in
|
||||||
|
Scrapy 2.13.0, is removed, you can use
|
||||||
|
:func:`scrapy.utils.versions.get_versions` instead.
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
- ``scrapy.downloadermiddlewares.ajaxcrawl.AjaxCrawlMiddleware`` and
|
||||||
|
``scrapy.utils.url.escape_ajax()``, deprecated in Scrapy 2.13.0, are
|
||||||
|
removed.
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
- The ``__init__()`` method of priority queue classes (see
|
||||||
|
:setting:`SCHEDULER_PRIORITY_QUEUE`) now needs to support a keyword-only
|
||||||
|
``start_queue_cls`` parameter, not supporting it was deprecated in Scrapy
|
||||||
|
2.13.0.
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
- ``scrapy.spiders.init.InitSpider``, deprecated in Scrapy 2.13.0, is
|
||||||
|
removed.
|
||||||
|
(:issue:`7487`)
|
||||||
|
|
||||||
|
New features
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- New features and improvements for
|
||||||
|
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`:
|
||||||
|
|
||||||
|
- Support for proxies.
|
||||||
|
|
||||||
|
- Support for the :reqmeta:`download_latency` meta key.
|
||||||
|
|
||||||
|
- Support for :attr:`Response.certificate
|
||||||
|
<scrapy.http.Response.certificate>`.
|
||||||
|
|
||||||
|
- Default headers set by the ``httpx`` library are no longer added to
|
||||||
|
requests.
|
||||||
|
|
||||||
|
(:issue:`7441`, :issue:`7524`)
|
||||||
|
|
||||||
|
- :class:`~scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler` now
|
||||||
|
skips HTTPS proxy certificate verification when the
|
||||||
|
:setting:`DOWNLOAD_VERIFY_CERTIFICATES` setting is set to ``False``.
|
||||||
|
(:issue:`7496`)
|
||||||
|
|
||||||
|
Improvements
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- :func:`time.monotonic` is used instead of :func:`time.time` to calculate
|
||||||
|
elapsed time in various places.
|
||||||
|
(:issue:`7377`)
|
||||||
|
|
||||||
|
- Improved extraction of the file extension from the URL in
|
||||||
|
:class:`~scrapy.pipelines.files.FilesPipeline`.
|
||||||
|
(:issue:`4225`, :issue:`7414`)
|
||||||
|
|
||||||
|
- Other code refactoring and improvements.
|
||||||
|
(:issue:`7401`)
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
- :class:`~scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler` now
|
||||||
|
raises an exception when a request has an ``https://`` destination and an
|
||||||
|
``https://`` proxy, which is not supported by this handler. Previously it
|
||||||
|
tried to connect to the proxy via HTTP in this case.
|
||||||
|
(:issue:`7496`)
|
||||||
|
|
||||||
|
- :class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler` now
|
||||||
|
raises an exception for requests with ``http://`` URLs instead of trying to
|
||||||
|
connect, which is not supported by this handler.
|
||||||
|
(:issue:`7496`)
|
||||||
|
|
||||||
|
- :class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler` no longer
|
||||||
|
adds the ``:status`` pseudo-header to :attr:`Response.headers
|
||||||
|
<scrapy.http.Response.headers>`.
|
||||||
|
(:issue:`7441`)
|
||||||
|
|
||||||
|
- Fixed :func:`scrapy.utils.response.open_in_browser` removing the ``<head>``
|
||||||
|
tag when adding the ``<base>`` tag.
|
||||||
|
(:issue:`7459`)
|
||||||
|
|
||||||
|
Documentation
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Documented that
|
||||||
|
:class:`~scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler`
|
||||||
|
doesn't support HTTPS proxies for HTTPS destinations and that
|
||||||
|
:class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler` doesn't
|
||||||
|
support proxies at all.
|
||||||
|
(:issue:`7496`)
|
||||||
|
|
||||||
|
- Added an example of using
|
||||||
|
:class:`logging.handlers.TimedRotatingFileHandler` to rotate Scrapy logs.
|
||||||
|
(:issue:`3628`, :issue:`7501`)
|
||||||
|
|
||||||
|
- Added a ``CITATION.cff`` file.
|
||||||
|
(:issue:`7502`, :issue:`7519`)
|
||||||
|
|
||||||
|
- Mentioned ``DOWNLOADER_CLIENT_TLS_METHOD`` in :ref:`bans`.
|
||||||
|
(:issue:`5232`, :issue:`7518`)
|
||||||
|
|
||||||
|
- Other documentation improvements and fixes.
|
||||||
|
(:issue:`7417`,
|
||||||
|
:issue:`7463`,
|
||||||
|
:issue:`7472`,
|
||||||
|
:issue:`7480`,
|
||||||
|
:issue:`7489`,
|
||||||
|
:issue:`7503`,
|
||||||
|
:issue:`7507`)
|
||||||
|
|
||||||
|
Quality assurance
|
||||||
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Added tests that connect to https://books.toscrape.com/ to test the
|
||||||
|
behavior with a real website. These tests are marked with the
|
||||||
|
``requires_internet`` pytest mark and can be skipped with e.g.
|
||||||
|
``-m 'not requires_internet'`` if you cannot or don't want to run them.
|
||||||
|
(:issue:`7520`)
|
||||||
|
|
||||||
|
- Type hints improvements and fixes.
|
||||||
|
(:issue:`7492`, :issue:`7532`)
|
||||||
|
|
||||||
|
- CI and test improvements and fixes.
|
||||||
|
(:issue:`7441`, :issue:`7466`, :issue:`7491`, :issue:`7496`)
|
||||||
|
|
||||||
|
.. _release-2.15.2:
|
||||||
|
|
||||||
|
Scrapy 2.15.2 (2026-04-28)
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
- Fixed links in https://docs.scrapy.org/llms.txt (:issue:`7467`)
|
||||||
|
|
||||||
|
.. _release-2.15.1:
|
||||||
|
|
||||||
|
Scrapy 2.15.1 (2026-04-23)
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
- Sharing of the SSL context between multiple connections, introduced in
|
||||||
|
Scrapy 2.15.0, is reverted as it caused problems and wasn't actually
|
||||||
|
needed.
|
||||||
|
(:issue:`7445`, :issue:`7450`)
|
||||||
|
|
||||||
|
- Fixed :meth:`scrapy.settings.BaseSettings.getwithbase` failing on keys with
|
||||||
|
dots that aren't import names. It now works the way it worked before Scrapy
|
||||||
|
2.15.0, without trying to match class objects and import path. A separate
|
||||||
|
method,
|
||||||
|
:func:`~scrapy.settings.BaseSettings.get_component_priority_dict_with_base`,
|
||||||
|
was added that does that, and it is now used for :ref:`component priority
|
||||||
|
dictionaries <component-priority-dictionaries>`.
|
||||||
|
(:issue:`7426`, :issue:`7449`)
|
||||||
|
|
||||||
|
- Documentation rendering improvements.
|
||||||
|
(:issue:`7452`, :issue:`7454`)
|
||||||
|
|
||||||
.. _release-2.15.0:
|
.. _release-2.15.0:
|
||||||
|
|
||||||
Scrapy 2.15.0 (2026-04-09)
|
Scrapy 2.15.0 (2026-04-09)
|
||||||
|
|
@ -398,7 +1035,7 @@ Deprecations
|
||||||
New features
|
New features
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
- Added a new setting, :setting:`REFERER_POLICIES`, to allow customizing
|
- Added a new setting, :setting:`REFERRER_POLICIES`, to allow customizing
|
||||||
supported referrer policies.
|
supported referrer policies.
|
||||||
|
|
||||||
Bug fixes
|
Bug fixes
|
||||||
|
|
@ -1320,8 +1957,8 @@ Highlights:
|
||||||
|
|
||||||
- Added the :reqmeta:`allow_offsite` request meta key
|
- Added the :reqmeta:`allow_offsite` request meta key
|
||||||
|
|
||||||
- :ref:`Spider middlewares that don't support asynchronous spider output
|
- Spider middlewares that don't support asynchronous spider output are
|
||||||
<sync-async-spider-middleware>` are deprecated
|
deprecated
|
||||||
|
|
||||||
- Added a base class for :ref:`universal spider middlewares
|
- Added a base class for :ref:`universal spider middlewares
|
||||||
<universal-spider-middleware>`
|
<universal-spider-middleware>`
|
||||||
|
|
@ -1459,13 +2096,11 @@ Deprecations
|
||||||
``start_queue_cls`` parameter.
|
``start_queue_cls`` parameter.
|
||||||
(:issue:`6752`)
|
(:issue:`6752`)
|
||||||
|
|
||||||
- :ref:`Spider middlewares that don't support asynchronous spider output
|
- Spider middlewares that don't support asynchronous spider output are
|
||||||
<sync-async-spider-middleware>` are deprecated. The async iterable
|
deprecated. The async iterable downgrading feature, needed for using such
|
||||||
downgrading feature, needed for using such middlewares with asynchronous
|
middlewares with asynchronous callbacks and with other spider middlewares
|
||||||
callbacks and with other spider middlewares that produce asynchronous
|
that produce asynchronous iterables, is also deprecated. Please update all
|
||||||
iterables, is also deprecated. Please update all such middlewares to
|
such middlewares to support asynchronous spider output. (:issue:`6664`)
|
||||||
support asynchronous spider output.
|
|
||||||
(:issue:`6664`)
|
|
||||||
|
|
||||||
- Functions that were imported from :mod:`w3lib.url` and re-exported in
|
- Functions that were imported from :mod:`w3lib.url` and re-exported in
|
||||||
:mod:`scrapy.utils.url` are now deprecated, you should import them from
|
:mod:`scrapy.utils.url` are now deprecated, you should import them from
|
||||||
|
|
@ -1724,9 +2359,8 @@ Documentation
|
||||||
- Documented the setting values set in the default project template.
|
- Documented the setting values set in the default project template.
|
||||||
(:issue:`6762`, :issue:`6775`)
|
(:issue:`6762`, :issue:`6775`)
|
||||||
|
|
||||||
- Improved the :ref:`docs <sync-async-spider-middleware>` about asynchronous
|
- Improved the docs about asynchronous iterable support in spider
|
||||||
iterable support in spider middlewares.
|
middlewares. (:issue:`6688`)
|
||||||
(:issue:`6688`)
|
|
||||||
|
|
||||||
- Improved the :ref:`docs <coroutine-deferred-apis>` about using
|
- Improved the :ref:`docs <coroutine-deferred-apis>` about using
|
||||||
:class:`~twisted.internet.defer.Deferred`-based APIs in coroutine-based
|
:class:`~twisted.internet.defer.Deferred`-based APIs in coroutine-based
|
||||||
|
|
@ -1890,7 +2524,7 @@ Backward-incompatible changes
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
- User-defined cookies for HTTPS requests will have the ``secure`` flag set
|
- User-defined cookies for HTTPS requests will have the ``secure`` flag set
|
||||||
to ``True`` unless it's set to ``False`` explictly. This is important when
|
to ``True`` unless it's set to ``False`` explicitly. This is important when
|
||||||
these cookies are reused in HTTP requests, e.g. after a redirect to an HTTP
|
these cookies are reused in HTTP requests, e.g. after a redirect to an HTTP
|
||||||
URL.
|
URL.
|
||||||
(:issue:`6357`)
|
(:issue:`6357`)
|
||||||
|
|
@ -1925,7 +2559,7 @@ Backward-incompatible changes
|
||||||
``crawler.settings`` instead. When they call ``__init__()`` of the base
|
``crawler.settings`` instead. When they call ``__init__()`` of the base
|
||||||
class they should pass the ``crawler`` argument to it too.
|
class they should pass the ``crawler`` argument to it too.
|
||||||
- A ``from_settings()`` method shouldn't be defined. Class-specific
|
- A ``from_settings()`` method shouldn't be defined. Class-specific
|
||||||
initialization code should go into either an overriden ``from_crawler()``
|
initialization code should go into either an overridden ``from_crawler()``
|
||||||
method or into ``__init__()``.
|
method or into ``__init__()``.
|
||||||
- It's now possible to override ``from_crawler()`` and it's not necessary
|
- It's now possible to override ``from_crawler()`` and it's not necessary
|
||||||
to call ``MediaPipeline.from_crawler()`` in it if other recommendations
|
to call ``MediaPipeline.from_crawler()`` in it if other recommendations
|
||||||
|
|
@ -5412,7 +6046,7 @@ The following changes may impact custom priority queue classes:
|
||||||
|
|
||||||
* A new keyword parameter has been added: ``key``. It is a string
|
* A new keyword parameter has been added: ``key``. It is a string
|
||||||
that is always an empty string for memory queues and indicates the
|
that is always an empty string for memory queues and indicates the
|
||||||
:setting:`JOB_DIR` value for disk queues.
|
:setting:`JOBDIR` value for disk queues.
|
||||||
|
|
||||||
* The parameter for disk queues that contains data from the previous
|
* The parameter for disk queues that contains data from the previous
|
||||||
crawl, ``startprios`` or ``slot_startprios``, is now passed as a
|
crawl, ``startprios`` or ``slot_startprios``, is now passed as a
|
||||||
|
|
@ -5933,8 +6567,8 @@ Backward-incompatible changes
|
||||||
``429``, you must override :setting:`RETRY_HTTP_CODES` accordingly.
|
``429``, you must override :setting:`RETRY_HTTP_CODES` accordingly.
|
||||||
|
|
||||||
* :class:`~scrapy.crawler.Crawler`,
|
* :class:`~scrapy.crawler.Crawler`,
|
||||||
:class:`CrawlerRunner.crawl <scrapy.crawler.CrawlerRunner.crawl>` and
|
:meth:`CrawlerRunner.crawl <scrapy.crawler.CrawlerRunner.crawl>` and
|
||||||
:class:`CrawlerRunner.create_crawler <scrapy.crawler.CrawlerRunner.create_crawler>`
|
:meth:`CrawlerRunner.create_crawler <scrapy.crawler.CrawlerRunner.create_crawler>`
|
||||||
no longer accept a :class:`~scrapy.spiders.Spider` subclass instance, they
|
no longer accept a :class:`~scrapy.spiders.Spider` subclass instance, they
|
||||||
only accept a :class:`~scrapy.spiders.Spider` subclass now.
|
only accept a :class:`~scrapy.spiders.Spider` subclass now.
|
||||||
|
|
||||||
|
|
@ -5966,7 +6600,7 @@ New features
|
||||||
``scrapy.pqueues.DownloaderAwarePriorityQueue``, may be
|
``scrapy.pqueues.DownloaderAwarePriorityQueue``, may be
|
||||||
:ref:`enabled <broad-crawls-scheduler-priority-queue>` for a significant
|
:ref:`enabled <broad-crawls-scheduler-priority-queue>` for a significant
|
||||||
scheduling improvement on crawls targeting multiple web domains, at the
|
scheduling improvement on crawls targeting multiple web domains, at the
|
||||||
cost of no :setting:`CONCURRENT_REQUESTS_PER_IP` support (:issue:`3520`)
|
cost of no ``CONCURRENT_REQUESTS_PER_IP`` support (:issue:`3520`)
|
||||||
|
|
||||||
* A new :attr:`.Request.cb_kwargs` attribute
|
* A new :attr:`.Request.cb_kwargs` attribute
|
||||||
provides a cleaner way to pass keyword arguments to callback methods
|
provides a cleaner way to pass keyword arguments to callback methods
|
||||||
|
|
@ -7154,7 +7788,7 @@ This 1.1 release brings a lot of interesting features and bug fixes:
|
||||||
selectors engine without needing to upgrade Scrapy.
|
selectors engine without needing to upgrade Scrapy.
|
||||||
- HTTPS downloader now does TLS protocol negotiation by default,
|
- HTTPS downloader now does TLS protocol negotiation by default,
|
||||||
instead of forcing TLS 1.0. You can also set the SSL/TLS method
|
instead of forcing TLS 1.0. You can also set the SSL/TLS method
|
||||||
using the new :setting:`DOWNLOADER_CLIENT_TLS_METHOD`.
|
using the new ``DOWNLOADER_CLIENT_TLS_METHOD`` setting.
|
||||||
|
|
||||||
- These bug fixes may require your attention:
|
- These bug fixes may require your attention:
|
||||||
|
|
||||||
|
|
@ -8445,7 +9079,7 @@ New features and settings
|
||||||
- In request errbacks, offending requests are now received in ``failure.request`` attribute (:rev:`2738`)
|
- In request errbacks, offending requests are now received in ``failure.request`` attribute (:rev:`2738`)
|
||||||
- Big downloader refactoring to support per domain/ip concurrency limits (:rev:`2732`)
|
- Big downloader refactoring to support per domain/ip concurrency limits (:rev:`2732`)
|
||||||
- ``CONCURRENT_REQUESTS_PER_SPIDER`` setting has been deprecated and replaced by:
|
- ``CONCURRENT_REQUESTS_PER_SPIDER`` setting has been deprecated and replaced by:
|
||||||
- :setting:`CONCURRENT_REQUESTS`, :setting:`CONCURRENT_REQUESTS_PER_DOMAIN`, :setting:`CONCURRENT_REQUESTS_PER_IP`
|
- :setting:`CONCURRENT_REQUESTS`, :setting:`CONCURRENT_REQUESTS_PER_DOMAIN`, ``CONCURRENT_REQUESTS_PER_IP``
|
||||||
- check the documentation for more details
|
- check the documentation for more details
|
||||||
- Added builtin caching DNS resolver (:rev:`2728`)
|
- Added builtin caching DNS resolver (:rev:`2728`)
|
||||||
- Moved Amazon AWS-related components/extensions (SQS spider queue, SimpleDB stats collector) to a separate project: [scaws](https://github.com/scrapinghub/scaws) (:rev:`2706`, :rev:`2714`)
|
- Moved Amazon AWS-related components/extensions (SQS spider queue, SimpleDB stats collector) to a separate project: [scaws](https://github.com/scrapinghub/scaws) (:rev:`2706`, :rev:`2714`)
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,4 @@ sphinx
|
||||||
sphinx-notfound-page
|
sphinx-notfound-page
|
||||||
sphinx-rtd-theme
|
sphinx-rtd-theme
|
||||||
sphinx-rtd-dark-mode
|
sphinx-rtd-dark-mode
|
||||||
sphinx-scrapy @ git+https://github.com/scrapy/sphinx-scrapy.git@0.7.1
|
sphinx-scrapy @ git+https://github.com/scrapy/sphinx-scrapy.git@0.8.8
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
# This file was autogenerated by uv via the following command:
|
# This file was autogenerated by uv via the following command:
|
||||||
# uv pip compile requirements.in -o requirements.txt
|
# uv pip compile -p 3.13 docs/requirements.in -o docs/requirements.txt
|
||||||
alabaster==1.0.0
|
alabaster==1.0.0
|
||||||
# via sphinx
|
# via sphinx
|
||||||
annotated-types==0.7.0
|
annotated-types==0.7.0
|
||||||
|
|
@ -39,7 +39,7 @@ docutils==0.22.4
|
||||||
filelock==3.25.2
|
filelock==3.25.2
|
||||||
# via tldextract
|
# via tldextract
|
||||||
h2==4.3.0
|
h2==4.3.0
|
||||||
# via -r requirements.in
|
# via -r docs/requirements.in
|
||||||
hpack==4.1.0
|
hpack==4.1.0
|
||||||
# via h2
|
# via h2
|
||||||
hyperframe==6.1.0
|
hyperframe==6.1.0
|
||||||
|
|
@ -97,7 +97,7 @@ pycparser==3.0
|
||||||
# via cffi
|
# via cffi
|
||||||
pydantic==2.12.5
|
pydantic==2.12.5
|
||||||
# via
|
# via
|
||||||
# -r requirements.in
|
# -r docs/requirements.in
|
||||||
# scrapy-spider-metadata
|
# scrapy-spider-metadata
|
||||||
pydantic-core==2.41.5
|
pydantic-core==2.41.5
|
||||||
# via pydantic
|
# via pydantic
|
||||||
|
|
@ -121,15 +121,16 @@ roman-numerals==4.1.0
|
||||||
scrapy==2.14.2
|
scrapy==2.14.2
|
||||||
# via scrapy-spider-metadata
|
# via scrapy-spider-metadata
|
||||||
scrapy-spider-metadata==0.2.0
|
scrapy-spider-metadata==0.2.0
|
||||||
# via -r requirements.in
|
# via -r docs/requirements.in
|
||||||
service-identity==24.2.0
|
service-identity==24.2.0
|
||||||
# via scrapy
|
# via scrapy
|
||||||
snowballstemmer==3.0.1
|
snowballstemmer==3.0.1
|
||||||
# via sphinx
|
# via sphinx
|
||||||
sphinx==9.1.0
|
sphinx==9.1.0
|
||||||
# via
|
# via
|
||||||
# -r requirements.in
|
# -r docs/requirements.in
|
||||||
# sphinx-copybutton
|
# sphinx-copybutton
|
||||||
|
# sphinx-last-updated-by-git
|
||||||
# sphinx-llms-txt
|
# sphinx-llms-txt
|
||||||
# sphinx-markdown-builder
|
# sphinx-markdown-builder
|
||||||
# sphinx-notfound-page
|
# sphinx-notfound-page
|
||||||
|
|
@ -138,20 +139,24 @@ sphinx==9.1.0
|
||||||
# sphinxcontrib-jquery
|
# sphinxcontrib-jquery
|
||||||
sphinx-copybutton==0.5.2
|
sphinx-copybutton==0.5.2
|
||||||
# via sphinx-scrapy
|
# via sphinx-scrapy
|
||||||
|
sphinx-last-updated-by-git==0.3.8
|
||||||
|
# via sphinx-sitemap
|
||||||
sphinx-llms-txt @ git+https://github.com/zytedata/sphinx-llms-txt.git@5e8866cb0cc249aa2017ad9050b3b83a7ca16f69
|
sphinx-llms-txt @ git+https://github.com/zytedata/sphinx-llms-txt.git@5e8866cb0cc249aa2017ad9050b3b83a7ca16f69
|
||||||
# via sphinx-scrapy
|
# via sphinx-scrapy
|
||||||
sphinx-markdown-builder @ git+https://github.com/zytedata/sphinx-markdown-builder.git@ac9f8babfe622e4300099ab44b96d9d9228e742e
|
sphinx-markdown-builder @ git+https://github.com/zytedata/sphinx-markdown-builder.git@cfe4c0bfd7b4542f7e6b65a58cdf9ec765829940
|
||||||
# via sphinx-scrapy
|
# via sphinx-scrapy
|
||||||
sphinx-notfound-page==1.1.0
|
sphinx-notfound-page==1.1.0
|
||||||
# via -r requirements.in
|
# via -r docs/requirements.in
|
||||||
sphinx-rtd-dark-mode==1.3.0
|
sphinx-rtd-dark-mode==1.3.0
|
||||||
# via -r requirements.in
|
# via -r docs/requirements.in
|
||||||
sphinx-rtd-theme==3.1.0
|
sphinx-rtd-theme==3.1.0
|
||||||
# via
|
# via
|
||||||
# -r requirements.in
|
# -r docs/requirements.in
|
||||||
# sphinx-rtd-dark-mode
|
# sphinx-rtd-dark-mode
|
||||||
sphinx-scrapy @ git+https://github.com/scrapy/sphinx-scrapy.git@2b5f6c7de64c8317cb771fdeb2e5020d1c9c9dcf
|
sphinx-scrapy @ git+https://github.com/scrapy/sphinx-scrapy.git@c0b2ac815afc3cb8857d575cecb5d55c05e6b737
|
||||||
# via -r requirements.in
|
# via -r docs/requirements.in
|
||||||
|
sphinx-sitemap==2.9.0
|
||||||
|
# via sphinx-scrapy
|
||||||
sphinxcontrib-applehelp==2.0.0
|
sphinxcontrib-applehelp==2.0.0
|
||||||
# via sphinx
|
# via sphinx
|
||||||
sphinxcontrib-devhelp==2.0.0
|
sphinxcontrib-devhelp==2.0.0
|
||||||
|
|
|
||||||
|
|
@ -166,8 +166,7 @@ Use a fallback component:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from scrapy.utils.misc import build_from_crawler
|
from scrapy.utils.misc import build_from_crawler, load_object
|
||||||
|
|
||||||
|
|
||||||
FALLBACK_SETTING = "MY_FALLBACK_DOWNLOAD_HANDLER"
|
FALLBACK_SETTING = "MY_FALLBACK_DOWNLOAD_HANDLER"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ this:
|
||||||
:meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output`).
|
:meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output`).
|
||||||
|
|
||||||
8. The :ref:`Engine <component-engine>` sends processed items to
|
8. The :ref:`Engine <component-engine>` sends processed items to
|
||||||
:ref:`Item Pipelines <component-pipelines>`, then send processed Requests to
|
:ref:`Item Pipelines <component-pipelines>`, then sends processed Requests to
|
||||||
the :ref:`Scheduler <component-scheduler>` and asks for possible next Requests
|
the :ref:`Scheduler <component-scheduler>` and asks for possible next Requests
|
||||||
to crawl.
|
to crawl.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,19 +4,27 @@
|
||||||
asyncio
|
asyncio
|
||||||
=======
|
=======
|
||||||
|
|
||||||
Scrapy has partial support for :mod:`asyncio`. After you :ref:`install the
|
Scrapy supports :mod:`asyncio` natively. New projects created with
|
||||||
asyncio reactor <install-asyncio>`, you may use :mod:`asyncio` and
|
:command:`startproject` have asyncio enabled by default, and you can use
|
||||||
:mod:`asyncio`-powered libraries in any :doc:`coroutine <coroutines>`.
|
:mod:`asyncio` and :mod:`asyncio`-powered libraries in any :doc:`coroutine
|
||||||
|
<coroutines>`.
|
||||||
|
|
||||||
|
The rest of this page covers advanced topics. If you are starting a new project,
|
||||||
|
no additional setup is needed.
|
||||||
|
|
||||||
|
|
||||||
.. _install-asyncio:
|
.. _install-asyncio:
|
||||||
|
|
||||||
Installing the asyncio reactor
|
Configuring the asyncio reactor
|
||||||
==============================
|
===============================
|
||||||
|
|
||||||
To enable :mod:`asyncio` support, your :setting:`TWISTED_REACTOR` setting needs
|
New projects generated with :command:`startproject` have the asyncio
|
||||||
to be set to ``'twisted.internet.asyncioreactor.AsyncioSelectorReactor'``,
|
reactor configured by default. No manual setup is needed.
|
||||||
which is the default value.
|
|
||||||
|
The :setting:`TWISTED_REACTOR` setting controls which Twisted reactor Scrapy
|
||||||
|
uses. Its default value is
|
||||||
|
``'twisted.internet.asyncioreactor.AsyncioSelectorReactor'``, which enables
|
||||||
|
:mod:`asyncio` support.
|
||||||
|
|
||||||
If you are using :class:`~scrapy.crawler.AsyncCrawlerRunner` or
|
If you are using :class:`~scrapy.crawler.AsyncCrawlerRunner` or
|
||||||
:class:`~scrapy.crawler.CrawlerRunner`, you also need to
|
:class:`~scrapy.crawler.CrawlerRunner`, you also need to
|
||||||
|
|
@ -97,6 +105,9 @@ Scrapy API requires passing a Deferred to it) using the following helpers:
|
||||||
|
|
||||||
.. autofunction:: scrapy.utils.defer.deferred_from_coro
|
.. autofunction:: scrapy.utils.defer.deferred_from_coro
|
||||||
.. autofunction:: scrapy.utils.defer.deferred_f_from_coro_f
|
.. autofunction:: scrapy.utils.defer.deferred_f_from_coro_f
|
||||||
|
|
||||||
|
The following function helps with a reverse wrapping:
|
||||||
|
|
||||||
.. autofunction:: scrapy.utils.defer.ensure_awaitable
|
.. autofunction:: scrapy.utils.defer.ensure_awaitable
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -182,7 +193,7 @@ in future Scrapy versions. The following features are not available:
|
||||||
:class:`~scrapy.crawler.CrawlerProcess`
|
:class:`~scrapy.crawler.CrawlerProcess`
|
||||||
(:class:`~scrapy.crawler.AsyncCrawlerProcess` and
|
(:class:`~scrapy.crawler.AsyncCrawlerProcess` and
|
||||||
:class:`~scrapy.crawler.AsyncCrawlerRunner` are available)
|
:class:`~scrapy.crawler.AsyncCrawlerRunner` are available)
|
||||||
* Twisted-specific DNS resolvers (the :setting:`DNS_RESOLVER` setting)
|
* Twisted-specific DNS resolvers (the :setting:`TWISTED_DNS_RESOLVER` setting)
|
||||||
* User and 3rd-party code that requires a reactor (see :ref:`below
|
* User and 3rd-party code that requires a reactor (see :ref:`below
|
||||||
<asyncio-without-reactor-migrate>` for examples)
|
<asyncio-without-reactor-migrate>` for examples)
|
||||||
|
|
||||||
|
|
@ -307,8 +318,7 @@ implementations, :class:`~asyncio.ProactorEventLoop` (default) and
|
||||||
:class:`~asyncio.SelectorEventLoop` works with Twisted.
|
:class:`~asyncio.SelectorEventLoop` works with Twisted.
|
||||||
|
|
||||||
Scrapy changes the event loop class to :class:`~asyncio.SelectorEventLoop`
|
Scrapy changes the event loop class to :class:`~asyncio.SelectorEventLoop`
|
||||||
automatically when you change the :setting:`TWISTED_REACTOR` setting or call
|
automatically when installing the asyncio reactor.
|
||||||
:func:`~scrapy.utils.reactor.install_reactor`.
|
|
||||||
|
|
||||||
.. note:: Other libraries you use may require
|
.. note:: Other libraries you use may require
|
||||||
:class:`~asyncio.ProactorEventLoop`, e.g. because it supports
|
:class:`~asyncio.ProactorEventLoop`, e.g. because it supports
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ AutoThrottle algorithm adjusts download delays based on the following rules:
|
||||||
.. _download-latency:
|
.. _download-latency:
|
||||||
|
|
||||||
In Scrapy, the download latency is measured as the time elapsed between
|
In Scrapy, the download latency is measured as the time elapsed between
|
||||||
establishing the TCP connection and receiving the HTTP headers.
|
sending the request and receiving the HTTP headers.
|
||||||
|
|
||||||
Note that these latencies are very hard to measure accurately in a cooperative
|
Note that these latencies are very hard to measure accurately in a cooperative
|
||||||
multitasking environment because Scrapy may be busy processing a spider
|
multitasking environment because Scrapy may be busy processing a spider
|
||||||
|
|
@ -88,6 +88,8 @@ server) is, and this extension builds on that premise.
|
||||||
Prevent specific requests from triggering slot delay adjustments
|
Prevent specific requests from triggering slot delay adjustments
|
||||||
================================================================
|
================================================================
|
||||||
|
|
||||||
|
.. versionadded:: 2.12.0
|
||||||
|
|
||||||
AutoThrottle adjusts the delay of download slots based on the latencies of
|
AutoThrottle adjusts the delay of download slots based on the latencies of
|
||||||
responses that belong to that download slot. The only exceptions are non-200
|
responses that belong to that download slot. The only exceptions are non-200
|
||||||
responses, which are only taken into account to increase that delay, but
|
responses, which are only taken into account to increase that delay, but
|
||||||
|
|
|
||||||
|
|
@ -199,6 +199,7 @@ Global commands:
|
||||||
* :command:`fetch`
|
* :command:`fetch`
|
||||||
* :command:`view`
|
* :command:`view`
|
||||||
* :command:`version`
|
* :command:`version`
|
||||||
|
* :command:`bench`
|
||||||
|
|
||||||
Project-only commands:
|
Project-only commands:
|
||||||
|
|
||||||
|
|
@ -207,7 +208,6 @@ Project-only commands:
|
||||||
* :command:`list`
|
* :command:`list`
|
||||||
* :command:`edit`
|
* :command:`edit`
|
||||||
* :command:`parse`
|
* :command:`parse`
|
||||||
* :command:`bench`
|
|
||||||
|
|
||||||
.. command:: startproject
|
.. command:: startproject
|
||||||
|
|
||||||
|
|
@ -309,11 +309,25 @@ Usage examples::
|
||||||
* parse_item
|
* parse_item
|
||||||
|
|
||||||
$ scrapy check
|
$ scrapy check
|
||||||
[FAILED] first_spider:parse_item
|
F.F.
|
||||||
>>> 'RetailPricex' field is missing
|
======================================================================
|
||||||
|
FAIL: [first_spider] parse (@returns post-hook)
|
||||||
|
----------------------------------------------------------------------
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
scrapy.exceptions.ContractFail: Returned 92 requests, expected 0..4
|
||||||
|
|
||||||
[FAILED] first_spider:parse
|
======================================================================
|
||||||
>>> Returned 92 requests, expected 0..4
|
FAIL: [first_spider] parse_item (@scrapes post-hook)
|
||||||
|
----------------------------------------------------------------------
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
scrapy.exceptions.ContractFail: Missing fields: RetailPricex
|
||||||
|
|
||||||
|
----------------------------------------------------------------------
|
||||||
|
Ran 4 contracts in 0.174s
|
||||||
|
|
||||||
|
FAILED (failures=2)
|
||||||
|
|
||||||
.. skip: end
|
.. skip: end
|
||||||
|
|
||||||
|
|
@ -377,7 +391,7 @@ Supported options:
|
||||||
|
|
||||||
* ``--spider=SPIDER``: bypass spider autodetection and force use of specific spider
|
* ``--spider=SPIDER``: bypass spider autodetection and force use of specific spider
|
||||||
|
|
||||||
* ``--headers``: print the response's HTTP headers instead of the response's body
|
* ``--headers``: print the request's and response's HTTP headers instead of the response's body
|
||||||
|
|
||||||
* ``--no-redirect``: do not follow HTTP 3xx redirects (default is to follow them)
|
* ``--no-redirect``: do not follow HTTP 3xx redirects (default is to follow them)
|
||||||
|
|
||||||
|
|
@ -387,15 +401,19 @@ Usage examples::
|
||||||
[ ... html content here ... ]
|
[ ... html content here ... ]
|
||||||
|
|
||||||
$ scrapy fetch --nolog --headers http://www.example.com/
|
$ scrapy fetch --nolog --headers http://www.example.com/
|
||||||
{'Accept-Ranges': ['bytes'],
|
> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
|
||||||
'Age': ['1263 '],
|
> Accept-Language: en
|
||||||
'Connection': ['close '],
|
> User-Agent: Scrapy/2.16.0 (+https://scrapy.org)
|
||||||
'Content-Length': ['596'],
|
> Accept-Encoding: gzip, deflate, br
|
||||||
'Content-Type': ['text/html; charset=UTF-8'],
|
>
|
||||||
'Date': ['Wed, 18 Aug 2010 23:59:46 GMT'],
|
< Date: Wed, 08 Jul 2026 06:15:01 GMT
|
||||||
'Etag': ['"573c1-254-48c9c87349680"'],
|
< Content-Type: text/html
|
||||||
'Last-Modified': ['Fri, 30 Jul 2010 15:30:18 GMT'],
|
< Server: cloudflare
|
||||||
'Server': ['Apache/2.2.3 (CentOS)']}
|
< Last-Modified: Wed, 01 Jul 2026 17:50:18 GMT
|
||||||
|
< Allow: GET, HEAD
|
||||||
|
< Cf-Cache-Status: HIT
|
||||||
|
< Age: 8184
|
||||||
|
< Cf-Ray: a17cf3b80eddf141-DME
|
||||||
|
|
||||||
.. command:: view
|
.. command:: view
|
||||||
|
|
||||||
|
|
@ -476,7 +494,7 @@ Supported options:
|
||||||
|
|
||||||
* ``--spider=SPIDER``: bypass spider autodetection and force use of specific spider
|
* ``--spider=SPIDER``: bypass spider autodetection and force use of specific spider
|
||||||
|
|
||||||
* ``--a NAME=VALUE``: set spider argument (may be repeated)
|
* ``-a NAME=VALUE``: set spider argument (may be repeated)
|
||||||
|
|
||||||
* ``--callback`` or ``-c``: spider method to use as callback for parsing the
|
* ``--callback`` or ``-c``: spider method to use as callback for parsing the
|
||||||
response
|
response
|
||||||
|
|
@ -605,7 +623,10 @@ shouldn't matter to the user running the command, but when the user :ref:`needs
|
||||||
a non-default Twisted reactor <disable-asyncio>`, it may be important.
|
a non-default Twisted reactor <disable-asyncio>`, it may be important.
|
||||||
|
|
||||||
Scrapy decides which of these two classes to use based on the value of the
|
Scrapy decides which of these two classes to use based on the value of the
|
||||||
:setting:`TWISTED_REACTOR` setting. If the setting value is the default one
|
:setting:`TWISTED_REACTOR` and :setting:`TWISTED_REACTOR_ENABLED` settings.
|
||||||
|
With :setting:`TWISTED_REACTOR_ENABLED` set to ``False`` it will use
|
||||||
|
:class:`~scrapy.crawler.AsyncCrawlerProcess`. Otherwise, if the
|
||||||
|
:setting:`TWISTED_REACTOR` value is the default one
|
||||||
(``'twisted.internet.asyncioreactor.AsyncioSelectorReactor'``),
|
(``'twisted.internet.asyncioreactor.AsyncioSelectorReactor'``),
|
||||||
:class:`~scrapy.crawler.AsyncCrawlerProcess` will be used, otherwise
|
:class:`~scrapy.crawler.AsyncCrawlerProcess` will be used, otherwise
|
||||||
:class:`~scrapy.crawler.CrawlerProcess` will be used. The :ref:`spider settings
|
:class:`~scrapy.crawler.CrawlerProcess` will be used. The :ref:`spider settings
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,13 @@ Supported callables
|
||||||
The following callables may be defined as coroutines using ``async def``, and
|
The following callables may be defined as coroutines using ``async def``, and
|
||||||
hence use coroutine syntax (e.g. ``await``, ``async for``, ``async with``):
|
hence use coroutine syntax (e.g. ``await``, ``async for``, ``async with``):
|
||||||
|
|
||||||
- The :meth:`~scrapy.spiders.Spider.start` spider method, which *must* be
|
- The :meth:`~scrapy.Spider.start` spider method, which *must* be
|
||||||
defined as an :term:`asynchronous generator`.
|
defined as an :term:`asynchronous generator`.
|
||||||
|
|
||||||
.. versionadded:: 2.13
|
.. versionadded:: 2.13
|
||||||
|
|
||||||
- :class:`~scrapy.Request` callbacks.
|
- :class:`~scrapy.Request` callbacks.
|
||||||
|
|
||||||
If you are using any custom or third-party :ref:`spider middleware
|
|
||||||
<topics-spider-middleware>`, see :ref:`sync-async-spider-middleware`.
|
|
||||||
|
|
||||||
- The :meth:`process_item` method of
|
- The :meth:`process_item` method of
|
||||||
:ref:`item pipelines <topics-item-pipeline>`.
|
:ref:`item pipelines <topics-item-pipeline>`.
|
||||||
|
|
||||||
|
|
@ -39,13 +36,9 @@ hence use coroutine syntax (e.g. ``await``, ``async for``, ``async with``):
|
||||||
|
|
||||||
- The
|
- The
|
||||||
:meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output`
|
:meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output`
|
||||||
method of :ref:`spider middlewares <topics-spider-middleware>`.
|
method of :ref:`spider middlewares <topics-spider-middleware>`, which
|
||||||
|
*must* be defined as an :term:`asynchronous generator` except in
|
||||||
If defined as a coroutine, it must be an :term:`asynchronous generator`.
|
:ref:`universal spider middlewares <universal-spider-middleware>`.
|
||||||
The input ``result`` parameter is an :term:`asynchronous iterable`.
|
|
||||||
|
|
||||||
See also :ref:`sync-async-spider-middleware` and
|
|
||||||
:ref:`universal-spider-middleware`.
|
|
||||||
|
|
||||||
- The :meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_start` method
|
- The :meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_start` method
|
||||||
of :ref:`spider middlewares <custom-spider-middleware>`, which *must* be
|
of :ref:`spider middlewares <custom-spider-middleware>`, which *must* be
|
||||||
|
|
@ -211,13 +204,15 @@ This means you can use many useful Python libraries providing such code:
|
||||||
Common use cases for asynchronous code include:
|
Common use cases for asynchronous code include:
|
||||||
|
|
||||||
* requesting data from websites, databases and other services (in
|
* requesting data from websites, databases and other services (in
|
||||||
:meth:`~scrapy.spiders.Spider.start`, callbacks, pipelines and
|
:meth:`~scrapy.Spider.start`, callbacks, pipelines and
|
||||||
middlewares);
|
middlewares);
|
||||||
* storing data in databases (in pipelines and middlewares);
|
* storing data in databases (in pipelines and middlewares);
|
||||||
* delaying the spider initialization until some external event (in the
|
* delaying the spider initialization until some external event (in the
|
||||||
:signal:`spider_opened` handler);
|
:signal:`spider_opened` handler);
|
||||||
* calling asynchronous Scrapy methods like :meth:`ExecutionEngine.download`
|
* calling asynchronous Scrapy methods like
|
||||||
(see :ref:`the screenshot pipeline example<ScreenshotPipeline>`).
|
:meth:`ExecutionEngine.download_async()
|
||||||
|
<scrapy.core.engine.ExecutionEngine.download_async>` (see :ref:`the
|
||||||
|
screenshot pipeline example <ScreenshotPipeline>`).
|
||||||
|
|
||||||
.. _aio-libs: https://github.com/aio-libs
|
.. _aio-libs: https://github.com/aio-libs
|
||||||
|
|
||||||
|
|
@ -274,139 +269,6 @@ You can also send multiple requests in parallel:
|
||||||
responses = await asyncio.gather(*tasks)
|
responses = await asyncio.gather(*tasks)
|
||||||
yield {
|
yield {
|
||||||
"h1": response.css("h1::text").get(),
|
"h1": response.css("h1::text").get(),
|
||||||
"price": responses[0][1].css(".price::text").get(),
|
"price": responses[0].css(".price::text").get(),
|
||||||
"price2": responses[1][1].css(".color::text").get(),
|
"color": responses[1].css(".color::text").get(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.. _sync-async-spider-middleware:
|
|
||||||
|
|
||||||
Mixing synchronous and asynchronous spider middlewares
|
|
||||||
======================================================
|
|
||||||
|
|
||||||
The output of a :class:`~scrapy.Request` callback is passed as the ``result``
|
|
||||||
parameter to the
|
|
||||||
:meth:`~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output` method
|
|
||||||
of the first :ref:`spider middleware <topics-spider-middleware>` from the
|
|
||||||
:ref:`list of active spider middlewares <topics-spider-middleware-setting>`.
|
|
||||||
Then the output of that ``process_spider_output`` method is passed to the
|
|
||||||
``process_spider_output`` method of the next spider middleware, and so on for
|
|
||||||
every active spider middleware.
|
|
||||||
|
|
||||||
Scrapy supports mixing :ref:`coroutine methods <async>` and synchronous methods
|
|
||||||
in this chain of calls.
|
|
||||||
|
|
||||||
However, if any of the ``process_spider_output`` methods is defined as a
|
|
||||||
synchronous method, and the previous ``Request`` callback or
|
|
||||||
``process_spider_output`` method is a coroutine, there are some drawbacks to
|
|
||||||
the asynchronous-to-synchronous conversion that Scrapy does so that the
|
|
||||||
synchronous ``process_spider_output`` method gets a synchronous iterable as its
|
|
||||||
``result`` parameter:
|
|
||||||
|
|
||||||
- The whole output of the previous ``Request`` callback or
|
|
||||||
``process_spider_output`` method is awaited at this point.
|
|
||||||
|
|
||||||
- If an exception raises while awaiting the output of the previous
|
|
||||||
``Request`` callback or ``process_spider_output`` method, none of that
|
|
||||||
output will be processed.
|
|
||||||
|
|
||||||
This contrasts with the regular behavior, where all items yielded before
|
|
||||||
an exception raises are processed.
|
|
||||||
|
|
||||||
Asynchronous-to-synchronous conversions are supported for backward
|
|
||||||
compatibility, but they are deprecated and will stop working in a future
|
|
||||||
version of Scrapy.
|
|
||||||
|
|
||||||
To avoid asynchronous-to-synchronous conversions, when defining ``Request``
|
|
||||||
callbacks as coroutine methods or when using spider middlewares whose
|
|
||||||
``process_spider_output`` method is an :term:`asynchronous generator`, all
|
|
||||||
active spider middlewares must either have their ``process_spider_output``
|
|
||||||
method defined as an asynchronous generator or :ref:`define a
|
|
||||||
process_spider_output_async method <universal-spider-middleware>`.
|
|
||||||
|
|
||||||
.. _sync-async-spider-middleware-users:
|
|
||||||
|
|
||||||
For middleware users
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
If you have asynchronous callbacks or use asynchronous-only spider middlewares
|
|
||||||
you should make sure the asynchronous-to-synchronous conversions
|
|
||||||
:ref:`described above <sync-async-spider-middleware>` don't happen. To do this,
|
|
||||||
make sure all spider middlewares you use support asynchronous spider output.
|
|
||||||
Even if you don't have asynchronous callbacks and don't use asynchronous-only
|
|
||||||
spider middlewares in your project, it's still a good idea to make sure all
|
|
||||||
middlewares you use support asynchronous spider output, so that it will be easy
|
|
||||||
to start using asynchronous callbacks in the future. Because of this, Scrapy
|
|
||||||
logs a warning when it detects a synchronous-only spider middleware.
|
|
||||||
|
|
||||||
If you want to update middlewares you wrote, see the :ref:`following section
|
|
||||||
<sync-async-spider-middleware-authors>`. If you have 3rd-party middlewares that
|
|
||||||
aren't yet updated by their authors, you can :ref:`subclass <tut-inheritance>`
|
|
||||||
them to make them :ref:`universal <universal-spider-middleware>` and use the
|
|
||||||
subclasses in your projects.
|
|
||||||
|
|
||||||
.. _sync-async-spider-middleware-authors:
|
|
||||||
|
|
||||||
For middleware authors
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
If you have a spider middleware that defines a synchronous
|
|
||||||
``process_spider_output`` method, you should update it to support asynchronous
|
|
||||||
spider output for :ref:`better compatibility <sync-async-spider-middleware>`,
|
|
||||||
even if you don't yet use it with asynchronous callbacks, especially if you
|
|
||||||
publish this middleware for other people to use. You have two options for this:
|
|
||||||
|
|
||||||
1. Make the middleware asynchronous, by making the ``process_spider_output``
|
|
||||||
method an :term:`asynchronous generator`.
|
|
||||||
2. Make the middleware universal, as described in the :ref:`next section
|
|
||||||
<universal-spider-middleware>`.
|
|
||||||
|
|
||||||
If your middleware won't be used in projects with synchronous-only middlewares,
|
|
||||||
e.g. because it's an internal middleware and you know that all other
|
|
||||||
middlewares in your projects are already updated, it's safe to choose the first
|
|
||||||
option. Otherwise, it's better to choose the second option.
|
|
||||||
|
|
||||||
.. _universal-spider-middleware:
|
|
||||||
|
|
||||||
Universal spider middlewares
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
To allow writing a spider middleware that supports asynchronous execution of
|
|
||||||
its ``process_spider_output`` method in Scrapy 2.7 and later (avoiding
|
|
||||||
:ref:`asynchronous-to-synchronous conversions <sync-async-spider-middleware>`)
|
|
||||||
while maintaining support for older Scrapy versions, you may define
|
|
||||||
``process_spider_output`` as a synchronous method and define an
|
|
||||||
:term:`asynchronous generator` version of that method with an alternative name:
|
|
||||||
``process_spider_output_async``.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class UniversalSpiderMiddleware:
|
|
||||||
def process_spider_output(self, response, result):
|
|
||||||
for r in result:
|
|
||||||
# ... do something with r
|
|
||||||
yield r
|
|
||||||
|
|
||||||
async def process_spider_output_async(self, response, result):
|
|
||||||
async for r in result:
|
|
||||||
# ... do something with r
|
|
||||||
yield r
|
|
||||||
|
|
||||||
.. note:: This is an interim measure to allow, for a time, to write code that
|
|
||||||
works in Scrapy 2.7 and later without requiring
|
|
||||||
asynchronous-to-synchronous conversions, and works in earlier Scrapy
|
|
||||||
versions as well.
|
|
||||||
|
|
||||||
In some future version of Scrapy, however, this feature will be
|
|
||||||
deprecated and, eventually, in a later version of Scrapy, this
|
|
||||||
feature will be removed, and all spider middlewares will be expected
|
|
||||||
to define their ``process_spider_output`` method as an asynchronous
|
|
||||||
generator.
|
|
||||||
|
|
||||||
Since 2.13.0, Scrapy provides a base class,
|
|
||||||
:class:`~scrapy.spidermiddlewares.base.BaseSpiderMiddleware`, which implements
|
|
||||||
the ``process_spider_output()`` and ``process_spider_output_async()`` methods,
|
|
||||||
so instead of duplicating the processing code you can override the
|
|
||||||
``get_processed_request()`` and/or the ``get_processed_item()`` method.
|
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,10 @@ for additional schemes and to replace or disable default ones:
|
||||||
"sftp": "my.download_handlers.SftpHandler",
|
"sftp": "my.download_handlers.SftpHandler",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-unencrypted-protocols` and
|
||||||
|
:ref:`security-local-resources`, for the security implications of the
|
||||||
|
default ``http``, ``ftp``, ``file`` and ``data`` handlers.
|
||||||
|
|
||||||
Replacing HTTP(S) download handlers
|
Replacing HTTP(S) download handlers
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
|
|
@ -85,7 +89,7 @@ the following API:
|
||||||
If ``True``, the handler will only be instantiated when the first
|
If ``True``, the handler will only be instantiated when the first
|
||||||
request handled by it needs to be downloaded.
|
request handled by it needs to be downloaded.
|
||||||
|
|
||||||
.. method:: download_request(request: Request) -> Response:
|
.. method:: download_request(request: Request) -> Response
|
||||||
:async:
|
:async:
|
||||||
|
|
||||||
Download the given request and return a response.
|
Download the given request and return a response.
|
||||||
|
|
@ -130,44 +134,39 @@ these exceptions.
|
||||||
|
|
||||||
.. _download-handlers-ref:
|
.. _download-handlers-ref:
|
||||||
|
|
||||||
Built-in download handlers reference
|
Built-in HTTP download handlers reference
|
||||||
====================================
|
=========================================
|
||||||
|
|
||||||
DataURIDownloadHandler
|
Scrapy ships several handlers for HTTP and HTTPS requests. While all of them
|
||||||
----------------------
|
support basic features, they may differ in support of specific Scrapy features
|
||||||
|
and settings and HTTP protocol features. See the documentation of specific
|
||||||
|
handlers and specific settings for more information. Additionally, as the
|
||||||
|
underlying HTTP client implementations differ between handlers, the behavior of
|
||||||
|
specific websites may be different when doing the same Scrapy requests but
|
||||||
|
using different handlers.
|
||||||
|
|
||||||
.. autoclass:: scrapy.core.downloader.handlers.datauri.DataURIDownloadHandler
|
Here is a comparison of some features of the built-in HTTP handlers, see the
|
||||||
|
individual handler docs for more differences:
|
||||||
|
|
||||||
| Supported scheme: ``data``.
|
================== ================= ===================== ====================
|
||||||
| Lazy: no.
|
Feature H2DownloadHandler HTTP11DownloadHandler HttpxDownloadHandler
|
||||||
|
================== ================= ===================== ====================
|
||||||
|
Requires asyncio No No Yes
|
||||||
|
Requires a reactor Yes Yes No
|
||||||
|
HTTP/1.1 No Yes Yes
|
||||||
|
HTTP/2 Yes No Yes
|
||||||
|
TLS implementation ``cryptography`` ``cryptography`` Stdlib ``ssl``
|
||||||
|
HTTP proxies No Yes Yes
|
||||||
|
SOCKS proxies No No Yes
|
||||||
|
================== ================= ===================== ====================
|
||||||
|
|
||||||
This handler supports RFC 2397 ``data:content/type;base64,`` data URIs.
|
You can find additional HTTP download handlers in the
|
||||||
|
scrapy-download-handlers-incubator_ package. This package is made by the Scrapy
|
||||||
|
developers and contains experimental handlers that may be included in some
|
||||||
|
later Scrapy version but can already be used. Please refer to the documentation
|
||||||
|
of this package for more information.
|
||||||
|
|
||||||
FileDownloadHandler
|
.. _scrapy-download-handlers-incubator: https://github.com/scrapy-plugins/scrapy-download-handlers-incubator
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. autoclass:: scrapy.core.downloader.handlers.file.FileDownloadHandler
|
|
||||||
|
|
||||||
| Supported scheme: ``file``.
|
|
||||||
| Lazy: no.
|
|
||||||
|
|
||||||
This handler supports ``file:///path`` local file URIs. It doesn't
|
|
||||||
support remote files.
|
|
||||||
|
|
||||||
FTPDownloadHandler
|
|
||||||
------------------
|
|
||||||
|
|
||||||
.. autoclass:: scrapy.core.downloader.handlers.ftp.FTPDownloadHandler
|
|
||||||
|
|
||||||
| Supported scheme: ``ftp``.
|
|
||||||
| Lazy: no.
|
|
||||||
|
|
||||||
This handler supports ``ftp://host/path`` FTP URIs.
|
|
||||||
|
|
||||||
It's implemented using :mod:`twisted.protocols.ftp`.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
This handler is not supported when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
|
||||||
|
|
||||||
.. _twisted-http2-handler:
|
.. _twisted-http2-handler:
|
||||||
|
|
||||||
|
|
@ -177,7 +176,9 @@ H2DownloadHandler
|
||||||
.. autoclass:: scrapy.core.downloader.handlers.http2.H2DownloadHandler
|
.. autoclass:: scrapy.core.downloader.handlers.http2.H2DownloadHandler
|
||||||
|
|
||||||
| Supported scheme: ``https``.
|
| Supported scheme: ``https``.
|
||||||
| Lazy: yes.
|
| :ref:`Lazy <lazy-download-handlers>`: yes.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: no.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: yes.
|
||||||
|
|
||||||
This handler supports ``https://host/path`` URLs and uses the HTTP/2 protocol
|
This handler supports ``https://host/path`` URLs and uses the HTTP/2 protocol
|
||||||
for them.
|
for them.
|
||||||
|
|
@ -196,61 +197,96 @@ If you want to use this handler you need to replace the default one for the
|
||||||
"https": "scrapy.core.downloader.handlers.http2.H2DownloadHandler",
|
"https": "scrapy.core.downloader.handlers.http2.H2DownloadHandler",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Features and limitations
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
|
|
||||||
This handler is experimental, and not yet recommended for production
|
This handler is experimental, and not yet recommended for production
|
||||||
environments. Future Scrapy versions may introduce related changes without
|
environments. Future Scrapy versions may introduce related changes without
|
||||||
a deprecation period or warning.
|
a deprecation period or warning.
|
||||||
|
|
||||||
.. note::
|
=========================== ================================================
|
||||||
|
HTTP proxies No (not implemented)
|
||||||
|
SOCKS proxies No (not supported by the library)
|
||||||
|
HTTP/2 Yes
|
||||||
|
``response.certificate`` :class:`twisted.internet.ssl.Certificate` object
|
||||||
|
Per-request ``bindaddress`` Yes
|
||||||
|
TLS implementation ``pyOpenSSL``/``cryptography``
|
||||||
|
=========================== ================================================
|
||||||
|
|
||||||
Known limitations of the HTTP/2 implementation in this handler include:
|
Other limitations:
|
||||||
|
|
||||||
- No support for HTTP/2 Cleartext (h2c), since no major browser supports
|
- No support for HTTP/1.1.
|
||||||
HTTP/2 unencrypted (refer `http2 faq`_).
|
|
||||||
|
|
||||||
- No setting to specify a maximum `frame size`_ larger than the default
|
- IPv6 support requires setting :setting:`TWISTED_DNS_RESOLVER`
|
||||||
value, 16384. Connections to servers that send a larger frame will
|
to ``scrapy.resolver.CachingHostnameResolver``.
|
||||||
fail.
|
|
||||||
|
|
||||||
- No support for `server pushes`_, which are ignored.
|
- No support for the :signal:`bytes_received` and :signal:`headers_received`
|
||||||
|
signals.
|
||||||
|
|
||||||
- No support for the :signal:`bytes_received` and
|
Known limitations of the HTTP/2 support:
|
||||||
:signal:`headers_received` signals.
|
|
||||||
|
- No support for HTTP/2 Cleartext (h2c), since no major browser supports
|
||||||
|
HTTP/2 unencrypted (refer `http2 faq`_).
|
||||||
|
|
||||||
|
- No setting to specify a maximum `frame size`_ larger than the default
|
||||||
|
value, 16384. Connections to servers that send a larger frame will fail.
|
||||||
|
|
||||||
|
- No support for `server pushes`_, which are ignored.
|
||||||
|
|
||||||
.. _frame size: https://datatracker.ietf.org/doc/html/rfc7540#section-4.2
|
.. _frame size: https://datatracker.ietf.org/doc/html/rfc7540#section-4.2
|
||||||
.. _http2 faq: https://http2.github.io/faq/#does-http2-require-encryption
|
.. _http2 faq: https://http2.github.io/faq/#does-http2-require-encryption
|
||||||
.. _server pushes: https://datatracker.ietf.org/doc/html/rfc7540#section-8.2
|
.. _server pushes: https://datatracker.ietf.org/doc/html/rfc7540#section-8.2
|
||||||
|
|
||||||
.. note::
|
|
||||||
This handler is not supported when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
|
||||||
|
|
||||||
HTTP11DownloadHandler
|
HTTP11DownloadHandler
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
.. autoclass:: scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler
|
.. autoclass:: scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler
|
||||||
|
|
||||||
| Supported schemes: ``http``, ``https``.
|
| Supported schemes: ``http``, ``https``.
|
||||||
| Lazy: no.
|
| :ref:`Lazy <lazy-download-handlers>`: no.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: no.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: yes.
|
||||||
|
|
||||||
This handler supports ``http://host/path`` and ``https://host/path`` URLs and
|
This handler supports ``http://host/path`` and ``https://host/path`` URLs and
|
||||||
uses the HTTP/1.1 protocol for them.
|
uses the HTTP/1.1 protocol for them.
|
||||||
|
|
||||||
It's implemented using :mod:`twisted.web.client`.
|
It's implemented using :mod:`twisted.web.client`.
|
||||||
|
|
||||||
.. note::
|
Features and limitations
|
||||||
This handler is not supported when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
=========================== ================================================
|
||||||
|
HTTP proxies Yes
|
||||||
|
SOCKS proxies No (not supported by the library)
|
||||||
|
HTTP/2 No (implemented as a separate handler)
|
||||||
|
``response.certificate`` :class:`twisted.internet.ssl.Certificate` object
|
||||||
|
Per-request ``bindaddress`` Yes
|
||||||
|
TLS implementation ``pyOpenSSL``/``cryptography``
|
||||||
|
=========================== ================================================
|
||||||
|
|
||||||
|
Other limitations:
|
||||||
|
|
||||||
|
- IPv6 support requires setting :setting:`TWISTED_DNS_RESOLVER`
|
||||||
|
to ``scrapy.resolver.CachingHostnameResolver``.
|
||||||
|
|
||||||
|
- HTTPS proxies to HTTPS destinations are not supported.
|
||||||
|
|
||||||
HttpxDownloadHandler
|
HttpxDownloadHandler
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
.. versionadded:: 2.15.0
|
||||||
|
|
||||||
.. autoclass:: scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler
|
.. autoclass:: scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler
|
||||||
|
|
||||||
| Supported schemes: ``http``, ``https``.
|
| Supported schemes: ``http``, ``https``.
|
||||||
| Lazy: no.
|
| :ref:`Lazy <lazy-download-handlers>`: no.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: yes.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: no.
|
||||||
|
|
||||||
This handler supports ``http://host/path`` and ``https://host/path`` URLs and
|
This handler supports ``http://host/path`` and ``https://host/path`` URLs and
|
||||||
uses the HTTP/1.1 protocol for them.
|
uses the HTTP/1.1 or HTTP/2 protocol for them.
|
||||||
|
|
||||||
It's implemented using the ``httpx`` library and needs it to be installed.
|
It's implemented using the ``httpx`` library and needs it to be installed.
|
||||||
|
|
||||||
|
|
@ -264,33 +300,83 @@ If you want to use this handler you need to replace the default ones for the
|
||||||
"https": "scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler",
|
"https": "scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Features and limitations
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
|
|
||||||
This handler is experimental, and not yet recommended for production
|
This handler is experimental, and not yet recommended for production
|
||||||
environments. Future Scrapy versions may introduce related changes without
|
environments. Future Scrapy versions may introduce related changes without
|
||||||
a deprecation period or warning or even remove it altogether.
|
a deprecation period or warning or even remove it altogether.
|
||||||
|
|
||||||
.. note::
|
=========================== =======================================
|
||||||
|
HTTP proxies Yes
|
||||||
|
SOCKS proxies Yes (SOCKS5; requires ``httpx[socks]``)
|
||||||
|
HTTP/2 Yes (requires ``httpx[http2]``)
|
||||||
|
``response.certificate`` DER bytes
|
||||||
|
Per-request ``bindaddress`` No (not supported by the library)
|
||||||
|
TLS implementation Standard library ``ssl``
|
||||||
|
=========================== =======================================
|
||||||
|
|
||||||
As this handler is based on a different HTTP client implementation compared
|
Other limitations:
|
||||||
to :class:`~.HTTP11DownloadHandler`, it's expected that its behavior on
|
|
||||||
some websites may be different. Additionally, these are the Scrapy features
|
|
||||||
that are explicitly not supported when using it:
|
|
||||||
|
|
||||||
- Proxy support (the :reqmeta:`proxy` meta key).
|
- The handler creates a separate connection pool for each proxy URL (due to
|
||||||
|
limitations of ``httpx``) which may lead to higher resource usage when
|
||||||
|
using proxy rotation.
|
||||||
|
|
||||||
- Per-request bind address support (the :reqmeta:`bindaddress` meta key).
|
.. setting:: HTTPX_HTTP2_ENABLED
|
||||||
The global :setting:`DOWNLOAD_BIND_ADDRESS` setting is supported but the
|
|
||||||
port number, if specified, will be ignored.
|
|
||||||
|
|
||||||
- The :setting:`DOWNLOADER_CLIENT_TLS_CIPHERS` and
|
HTTPX_HTTP2_ENABLED
|
||||||
:setting:`DOWNLOADER_CLIENT_TLS_METHOD` settings.
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
- Settings specific to the Twisted networking or HTTP implementation, like
|
.. versionadded:: 2.17.0
|
||||||
:setting:`DNS_RESOLVER`.
|
|
||||||
|
|
||||||
- Using :ref:`non-asyncio reactors <disable-asyncio>` (``httpx`` requires
|
Default: ``False``
|
||||||
``asyncio``).
|
|
||||||
|
Whether to enable HTTP/2 support in this handler. The ``httpx[http2]`` extra
|
||||||
|
needs to be installed if you want to enable this setting.
|
||||||
|
|
||||||
|
Built-in non-HTTP download handlers reference
|
||||||
|
=============================================
|
||||||
|
|
||||||
|
DataURIDownloadHandler
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
.. autoclass:: scrapy.core.downloader.handlers.datauri.DataURIDownloadHandler
|
||||||
|
|
||||||
|
| Supported scheme: ``data``.
|
||||||
|
| :ref:`Lazy <lazy-download-handlers>`: no.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: no.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: no.
|
||||||
|
|
||||||
|
This handler supports RFC 2397 ``data:content/type;base64,`` data URIs.
|
||||||
|
|
||||||
|
FileDownloadHandler
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
.. autoclass:: scrapy.core.downloader.handlers.file.FileDownloadHandler
|
||||||
|
|
||||||
|
| Supported scheme: ``file``.
|
||||||
|
| :ref:`Lazy <lazy-download-handlers>`: no.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: no.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: no.
|
||||||
|
|
||||||
|
This handler supports ``file:///path`` local file URIs. It doesn't
|
||||||
|
support remote files.
|
||||||
|
|
||||||
|
FTPDownloadHandler
|
||||||
|
------------------
|
||||||
|
|
||||||
|
.. autoclass:: scrapy.core.downloader.handlers.ftp.FTPDownloadHandler
|
||||||
|
|
||||||
|
| Supported scheme: ``ftp``.
|
||||||
|
| :ref:`Lazy <lazy-download-handlers>`: no.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: no.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: yes.
|
||||||
|
|
||||||
|
This handler supports ``ftp://host/path`` FTP URIs.
|
||||||
|
|
||||||
|
It's implemented using :mod:`twisted.protocols.ftp`.
|
||||||
|
|
||||||
S3DownloadHandler
|
S3DownloadHandler
|
||||||
-----------------
|
-----------------
|
||||||
|
|
@ -298,7 +384,9 @@ S3DownloadHandler
|
||||||
.. autoclass:: scrapy.core.downloader.handlers.s3.S3DownloadHandler
|
.. autoclass:: scrapy.core.downloader.handlers.s3.S3DownloadHandler
|
||||||
|
|
||||||
| Supported scheme: ``s3``.
|
| Supported scheme: ``s3``.
|
||||||
| Lazy: yes.
|
| :ref:`Lazy <lazy-download-handlers>`: yes.
|
||||||
|
| :ref:`Requires asyncio support <using-asyncio>`: no.
|
||||||
|
| :ref:`Requires a Twisted reactor <asyncio-without-reactor>`: no.
|
||||||
|
|
||||||
This handler supports ``s3://bucket/path`` S3 URIs.
|
This handler supports ``s3://bucket/path`` S3 URIs.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -307,26 +307,15 @@ HttpAuthMiddleware
|
||||||
|
|
||||||
.. class:: HttpAuthMiddleware
|
.. class:: HttpAuthMiddleware
|
||||||
|
|
||||||
This middleware authenticates all requests generated from certain spiders
|
This middleware authenticates requests using `Basic access authentication`_
|
||||||
using `Basic access authentication`_ (aka. HTTP auth).
|
(aka. HTTP auth).
|
||||||
|
|
||||||
To enable HTTP authentication for a spider, set the ``http_user`` and
|
Use the :setting:`HTTPAUTH_USER`, :setting:`HTTPAUTH_PASS`, and
|
||||||
``http_pass`` spider attributes to the authentication data and the
|
:setting:`HTTPAUTH_DOMAIN` settings to configure it. You can also override
|
||||||
``http_auth_domain`` spider attribute to the domain which requires this
|
the credentials per request via :attr:`~scrapy.Request.meta` keys
|
||||||
authentication (its subdomains will be also handled in the same way).
|
:reqmeta:`http_user`, :reqmeta:`http_pass`, and :reqmeta:`http_auth_domain`.
|
||||||
You can set ``http_auth_domain`` to ``None`` to enable the
|
|
||||||
authentication for all requests but you risk leaking your authentication
|
|
||||||
credentials to unrelated domains.
|
|
||||||
|
|
||||||
.. warning::
|
Example using settings (e.g. in :attr:`~scrapy.Spider.custom_settings`):
|
||||||
In previous Scrapy versions HttpAuthMiddleware sent the authentication
|
|
||||||
data with all requests, which is a security problem if the spider
|
|
||||||
makes requests to several different domains. Currently if the
|
|
||||||
``http_auth_domain`` attribute is not set, the middleware will use the
|
|
||||||
domain of the first request, which will work for some spiders but not
|
|
||||||
for others. In the future the middleware will produce an error instead.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
|
@ -334,13 +323,70 @@ HttpAuthMiddleware
|
||||||
|
|
||||||
|
|
||||||
class SomeIntranetSiteSpider(CrawlSpider):
|
class SomeIntranetSiteSpider(CrawlSpider):
|
||||||
http_user = "someuser"
|
|
||||||
http_pass = "somepass"
|
|
||||||
http_auth_domain = "intranet.example.com"
|
|
||||||
name = "intranet.example.com"
|
name = "intranet.example.com"
|
||||||
|
custom_settings = {
|
||||||
|
"HTTPAUTH_USER": "someuser",
|
||||||
|
"HTTPAUTH_PASS": "somepass",
|
||||||
|
"HTTPAUTH_DOMAIN": "intranet.example.com",
|
||||||
|
}
|
||||||
|
|
||||||
# .. rest of the spider code omitted ...
|
# .. rest of the spider code omitted ...
|
||||||
|
|
||||||
|
Example using per-request meta:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
async def start(self):
|
||||||
|
yield Request(
|
||||||
|
"https://intranet.example.com/protected/",
|
||||||
|
meta={
|
||||||
|
"http_user": "someuser",
|
||||||
|
"http_pass": "somepass",
|
||||||
|
"http_auth_domain": "intranet.example.com",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
.. setting:: HTTPAUTH_USER
|
||||||
|
|
||||||
|
HTTPAUTH_USER
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Default: ``""``
|
||||||
|
|
||||||
|
The username to use for HTTP basic authentication, applied to all requests
|
||||||
|
whose URL matches :setting:`HTTPAUTH_DOMAIN`.
|
||||||
|
|
||||||
|
.. setting:: HTTPAUTH_PASS
|
||||||
|
|
||||||
|
HTTPAUTH_PASS
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Default: ``""``
|
||||||
|
|
||||||
|
The password to use for HTTP basic authentication.
|
||||||
|
|
||||||
|
.. setting:: HTTPAUTH_DOMAIN
|
||||||
|
|
||||||
|
HTTPAUTH_DOMAIN
|
||||||
|
~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Default: ``None``
|
||||||
|
|
||||||
|
The domain (and its subdomains) to which HTTP basic authentication credentials
|
||||||
|
are sent. Set to ``None`` to send credentials with all requests, but be aware
|
||||||
|
that this risks leaking credentials to unrelated domains.
|
||||||
|
|
||||||
|
This setting must be explicitly configured whenever :setting:`HTTPAUTH_USER`
|
||||||
|
or :setting:`HTTPAUTH_PASS` is set.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-credential-leakage`
|
||||||
|
|
||||||
.. _Basic access authentication: https://en.wikipedia.org/wiki/Basic_access_authentication
|
.. _Basic access authentication: https://en.wikipedia.org/wiki/Basic_access_authentication
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -459,7 +505,7 @@ Filesystem storage backend (default)
|
||||||
|
|
||||||
* ``response_body`` - the plain response body
|
* ``response_body`` - the plain response body
|
||||||
|
|
||||||
* ``response_headers`` - the request headers (in raw HTTP format)
|
* ``response_headers`` - the response headers (in raw HTTP format)
|
||||||
|
|
||||||
* ``meta`` - some metadata of this cache resource in Python ``repr()``
|
* ``meta`` - some metadata of this cache resource in Python ``repr()``
|
||||||
format (grep-friendly format)
|
format (grep-friendly format)
|
||||||
|
|
@ -501,7 +547,7 @@ defines the methods described below.
|
||||||
.. method:: open_spider(spider)
|
.. method:: open_spider(spider)
|
||||||
|
|
||||||
This method gets called after a spider has been opened for crawling. It handles
|
This method gets called after a spider has been opened for crawling. It handles
|
||||||
the :signal:`open_spider <spider_opened>` signal.
|
the :signal:`spider_opened` signal.
|
||||||
|
|
||||||
:param spider: the spider which has been opened
|
:param spider: the spider which has been opened
|
||||||
:type spider: :class:`~scrapy.Spider` object
|
:type spider: :class:`~scrapy.Spider` object
|
||||||
|
|
@ -509,7 +555,7 @@ defines the methods described below.
|
||||||
.. method:: close_spider(spider)
|
.. method:: close_spider(spider)
|
||||||
|
|
||||||
This method gets called after a spider has been closed. It handles
|
This method gets called after a spider has been closed. It handles
|
||||||
the :signal:`close_spider <spider_closed>` signal.
|
the :signal:`spider_closed` signal.
|
||||||
|
|
||||||
:param spider: the spider which has been closed
|
:param spider: the spider which has been closed
|
||||||
:type spider: :class:`~scrapy.Spider` object
|
:type spider: :class:`~scrapy.Spider` object
|
||||||
|
|
@ -545,8 +591,8 @@ In order to use your storage backend, set:
|
||||||
HTTPCache middleware settings
|
HTTPCache middleware settings
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
The :class:`HttpCacheMiddleware` can be configured through the following
|
:class:`~scrapy.downloadermiddlewares.httpcache.HttpCacheMiddleware` can be
|
||||||
settings:
|
configured through the following settings:
|
||||||
|
|
||||||
.. setting:: HTTPCACHE_ENABLED
|
.. setting:: HTTPCACHE_ENABLED
|
||||||
|
|
||||||
|
|
@ -745,13 +791,29 @@ HttpProxyMiddleware
|
||||||
Handling of this meta key needs to be implemented inside the :ref:`download
|
Handling of this meta key needs to be implemented inside the :ref:`download
|
||||||
handler <topics-download-handlers>`, so it's not guaranteed to be supported
|
handler <topics-download-handlers>`, so it's not guaranteed to be supported
|
||||||
by all 3rd-party handlers. It's currently unsupported by
|
by all 3rd-party handlers. It's currently unsupported by
|
||||||
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`.
|
:class:`~scrapy.core.downloader.handlers.http2.H2DownloadHandler`.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Usually a proxy URL uses the ``http://`` scheme. More rarely, it uses the
|
||||||
|
``https://`` one. While both kinds of proxy URLs can be used with both HTTP
|
||||||
|
and HTTPS destination URLs, the specifics of the network exchange are
|
||||||
|
different for all 4 cases and it's possible that HTTPS proxies are fully or
|
||||||
|
partially unsupported by a given download handler. Currently,
|
||||||
|
:class:`~scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler`
|
||||||
|
supports HTTPS proxies only for HTTP destinations.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
If the download handler supports it, you can use a SOCKS proxy URL (e.g.
|
||||||
|
``socks5://username:password@some_proxy_server:port``).
|
||||||
|
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`
|
||||||
|
supports SOCKS proxies while other built-in handlers don't.
|
||||||
|
|
||||||
HttpProxyMiddleware settings
|
HttpProxyMiddleware settings
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. setting:: HTTPPROXY_ENABLED
|
.. setting:: HTTPPROXY_ENABLED
|
||||||
.. setting:: HTTPPROXY_AUTH_ENCODING
|
|
||||||
|
|
||||||
HTTPPROXY_ENABLED
|
HTTPPROXY_ENABLED
|
||||||
^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^
|
||||||
|
|
@ -760,6 +822,8 @@ Default: ``True``
|
||||||
|
|
||||||
Whether or not to enable the :class:`HttpProxyMiddleware`.
|
Whether or not to enable the :class:`HttpProxyMiddleware`.
|
||||||
|
|
||||||
|
.. setting:: HTTPPROXY_AUTH_ENCODING
|
||||||
|
|
||||||
HTTPPROXY_AUTH_ENCODING
|
HTTPPROXY_AUTH_ENCODING
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
@ -804,9 +868,9 @@ OffsiteMiddleware
|
||||||
.. reqmeta:: allow_offsite
|
.. reqmeta:: allow_offsite
|
||||||
|
|
||||||
If the request has the :attr:`~scrapy.Request.dont_filter` attribute set to
|
If the request has the :attr:`~scrapy.Request.dont_filter` attribute set to
|
||||||
``True`` or :attr:`Request.meta` has ``allow_offsite`` set to ``True``, then
|
``True`` or :attr:`Request.meta <scrapy.Request.meta>` has ``allow_offsite``
|
||||||
the OffsiteMiddleware will allow the request even if its domain is not listed
|
set to ``True``, then the OffsiteMiddleware will allow the request even if
|
||||||
in allowed domains.
|
its domain is not listed in allowed domains.
|
||||||
|
|
||||||
RedirectMiddleware
|
RedirectMiddleware
|
||||||
------------------
|
------------------
|
||||||
|
|
@ -921,7 +985,7 @@ Whether the Meta Refresh middleware will be enabled.
|
||||||
METAREFRESH_IGNORE_TAGS
|
METAREFRESH_IGNORE_TAGS
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
Default: ``[]``
|
Default: ``["noscript"]``
|
||||||
|
|
||||||
Meta tags within these tags are ignored.
|
Meta tags within these tags are ignored.
|
||||||
|
|
||||||
|
|
@ -951,17 +1015,6 @@ RetryMiddleware
|
||||||
A middleware to retry failed requests that are potentially caused by
|
A middleware to retry failed requests that are potentially caused by
|
||||||
temporary problems such as a connection timeout or HTTP 500 error.
|
temporary problems such as a connection timeout or HTTP 500 error.
|
||||||
|
|
||||||
Failed pages are collected on the scraping process and rescheduled at the
|
|
||||||
end, once the spider has finished crawling all regular (non failed) pages.
|
|
||||||
|
|
||||||
The :class:`RetryMiddleware` can be configured through the following
|
|
||||||
settings (see the settings documentation for more info):
|
|
||||||
|
|
||||||
* :setting:`RETRY_ENABLED`
|
|
||||||
* :setting:`RETRY_TIMES`
|
|
||||||
* :setting:`RETRY_HTTP_CODES`
|
|
||||||
* :setting:`RETRY_EXCEPTIONS`
|
|
||||||
|
|
||||||
.. reqmeta:: dont_retry
|
.. reqmeta:: dont_retry
|
||||||
|
|
||||||
If :attr:`Request.meta <scrapy.Request.meta>` has ``dont_retry`` key
|
If :attr:`Request.meta <scrapy.Request.meta>` has ``dont_retry`` key
|
||||||
|
|
@ -1028,7 +1081,7 @@ Default::
|
||||||
'twisted.internet.error.ConnectionDone',
|
'twisted.internet.error.ConnectionDone',
|
||||||
'twisted.internet.error.ConnectError',
|
'twisted.internet.error.ConnectError',
|
||||||
'twisted.internet.error.ConnectionLost',
|
'twisted.internet.error.ConnectionLost',
|
||||||
IOError,
|
OSError,
|
||||||
'scrapy.core.downloader.handlers.http11.TunnelError',
|
'scrapy.core.downloader.handlers.http11.TunnelError',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -1042,6 +1095,23 @@ has been exceeded (see :setting:`RETRY_TIMES`). To learn about uncaught
|
||||||
exception propagation, see
|
exception propagation, see
|
||||||
:meth:`~scrapy.downloadermiddlewares.DownloaderMiddleware.process_exception`.
|
:meth:`~scrapy.downloadermiddlewares.DownloaderMiddleware.process_exception`.
|
||||||
|
|
||||||
|
.. setting:: RETRY_GIVE_UP_LOG_LEVEL
|
||||||
|
|
||||||
|
RETRY_GIVE_UP_LOG_LEVEL
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Default: ``"ERROR"``
|
||||||
|
|
||||||
|
:ref:`Logging level <levels>` used for the message logged when a request
|
||||||
|
exceeds its retries.
|
||||||
|
|
||||||
|
Can be a level name (e.g. ``"WARNING"``) or a number (e.g. ``logging.WARNING``
|
||||||
|
or ``30``).
|
||||||
|
|
||||||
|
See also: :reqmeta:`give_up_log_level`, :func:`get_retry_request`.
|
||||||
|
|
||||||
.. setting:: RETRY_PRIORITY_ADJUST
|
.. setting:: RETRY_PRIORITY_ADJUST
|
||||||
|
|
||||||
RETRY_PRIORITY_ADJUST
|
RETRY_PRIORITY_ADJUST
|
||||||
|
|
@ -1141,9 +1211,9 @@ Based on :class:`~urllib.robotparser.RobotFileParser`:
|
||||||
* is compliant with `Martijn Koster's 1996 draft specification
|
* is compliant with `Martijn Koster's 1996 draft specification
|
||||||
<https://www.robotstxt.org/norobots-rfc.txt>`_
|
<https://www.robotstxt.org/norobots-rfc.txt>`_
|
||||||
|
|
||||||
* lacks support for wildcard matching
|
* lacks support for wildcard matching (before Python 3.14.5)
|
||||||
|
|
||||||
* doesn't use the length based rule
|
* doesn't use the length based rule (before Python 3.14.5)
|
||||||
|
|
||||||
It is faster than Protego and backward-compatible with versions of Scrapy before 1.8.0.
|
It is faster than Protego and backward-compatible with versions of Scrapy before 1.8.0.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ request with Scrapy.
|
||||||
|
|
||||||
It might be enough to yield a :class:`~scrapy.Request` with the same HTTP
|
It might be enough to yield a :class:`~scrapy.Request` with the same HTTP
|
||||||
method and URL. However, you may also need to reproduce the body, headers and
|
method and URL. However, you may also need to reproduce the body, headers and
|
||||||
form parameters (see :class:`~scrapy.FormRequest`) of that request.
|
form parameters (see :ref:`form`) of that request.
|
||||||
|
|
||||||
As all major browsers allow to export the requests in curl_ format, Scrapy
|
As all major browsers allow to export the requests in curl_ format, Scrapy
|
||||||
incorporates the method :meth:`~scrapy.Request.from_curl` to generate an equivalent
|
incorporates the method :meth:`~scrapy.Request.from_curl` to generate an equivalent
|
||||||
|
|
@ -133,7 +133,7 @@ data from it depends on the type of response:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
selector = Selector(data["html"])
|
selector = Selector(text=data["html"])
|
||||||
|
|
||||||
- If the response is JavaScript, or HTML with a ``<script/>`` element
|
- If the response is JavaScript, or HTML with a ``<script/>`` element
|
||||||
containing the desired data, see :ref:`topics-parsing-javascript`.
|
containing the desired data, see :ref:`topics-parsing-javascript`.
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ Exceptions
|
||||||
Built-in Exceptions reference
|
Built-in Exceptions reference
|
||||||
=============================
|
=============================
|
||||||
|
|
||||||
Here's a list of all exceptions included in Scrapy and their usage.
|
Here's a list of all exceptions included in Scrapy and their usage, except for
|
||||||
|
the :ref:`download handler exceptions <download-handlers-exceptions>`.
|
||||||
|
|
||||||
|
|
||||||
CloseSpider
|
CloseSpider
|
||||||
|
|
@ -31,7 +32,7 @@ For example:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
def parse_page(self, response):
|
def parse_page(self, response):
|
||||||
if "Bandwidth exceeded" in response.body:
|
if "Bandwidth exceeded" in response.text:
|
||||||
raise CloseSpider("bandwidth_exceeded")
|
raise CloseSpider("bandwidth_exceeded")
|
||||||
|
|
||||||
DontCloseSpider
|
DontCloseSpider
|
||||||
|
|
@ -71,7 +72,8 @@ remain disabled. Those components include:
|
||||||
- Downloader middlewares
|
- Downloader middlewares
|
||||||
- Spider middlewares
|
- Spider middlewares
|
||||||
|
|
||||||
The exception must be raised in the component's ``__init__`` method.
|
The exception must be raised in the component's ``__init__()`` or
|
||||||
|
``from_crawler()`` method.
|
||||||
|
|
||||||
NotSupported
|
NotSupported
|
||||||
------------
|
------------
|
||||||
|
|
|
||||||
|
|
@ -93,24 +93,25 @@ described next.
|
||||||
1. Declaring a serializer in the field
|
1. Declaring a serializer in the field
|
||||||
--------------------------------------
|
--------------------------------------
|
||||||
|
|
||||||
If you use :class:`~scrapy.Item` you can declare a serializer in the
|
Every :ref:`item type <item-types>` except :class:`dict` lets you declare a
|
||||||
:ref:`field metadata <topics-items-fields>`. The serializer must be
|
serializer in the :ref:`field metadata <topics-items-fields>`. The serializer
|
||||||
a callable which receives a value and returns its serialized form.
|
must be a callable which receives a value and returns its serialized form.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import scrapy
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
|
||||||
def serialize_price(value):
|
def serialize_price(value):
|
||||||
return f"$ {str(value)}"
|
return f"$ {str(value)}"
|
||||||
|
|
||||||
|
|
||||||
class Product(scrapy.Item):
|
@dataclass
|
||||||
name = scrapy.Field()
|
class Product:
|
||||||
price = scrapy.Field(serializer=serialize_price)
|
name: str
|
||||||
|
price: float = field(metadata={"serializer": serialize_price})
|
||||||
|
|
||||||
|
|
||||||
2. Overriding the serialize_field() method
|
2. Overriding the serialize_field() method
|
||||||
|
|
@ -152,7 +153,7 @@ output examples, which assume you're exporting these two items:
|
||||||
BaseItemExporter
|
BaseItemExporter
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
.. class:: BaseItemExporter(fields_to_export=None, export_empty_fields=False, encoding='utf-8', indent=0, dont_fail=False)
|
.. class:: BaseItemExporter(fields_to_export=None, export_empty_fields=False, encoding=None, indent=None, dont_fail=False)
|
||||||
|
|
||||||
This is the (abstract) base class for all Item Exporters. It provides
|
This is the (abstract) base class for all Item Exporters. It provides
|
||||||
support for common features used by all (concrete) Item Exporters, such as
|
support for common features used by all (concrete) Item Exporters, such as
|
||||||
|
|
@ -238,7 +239,7 @@ BaseItemExporter
|
||||||
|
|
||||||
.. attribute:: indent
|
.. attribute:: indent
|
||||||
|
|
||||||
Amount of spaces used to indent the output on each level. Defaults to ``0``.
|
Amount of spaces used to indent the output on each level. Defaults to ``None``.
|
||||||
|
|
||||||
* ``indent=None`` selects the most compact representation,
|
* ``indent=None`` selects the most compact representation,
|
||||||
all items in the same line with no indentation
|
all items in the same line with no indentation
|
||||||
|
|
@ -327,7 +328,7 @@ CsvItemExporter
|
||||||
|
|
||||||
:param join_multivalued: The char (or chars) that will be used for joining
|
:param join_multivalued: The char (or chars) that will be used for joining
|
||||||
multi-valued fields, if found.
|
multi-valued fields, if found.
|
||||||
:type include_headers_line: str
|
:type join_multivalued: str
|
||||||
|
|
||||||
:param errors: The optional string that specifies how encoding and decoding
|
:param errors: The optional string that specifies how encoding and decoding
|
||||||
errors are to be handled. For more information see
|
errors are to be handled. For more information see
|
||||||
|
|
@ -341,14 +342,14 @@ CsvItemExporter
|
||||||
|
|
||||||
A typical output of this exporter would be::
|
A typical output of this exporter would be::
|
||||||
|
|
||||||
product,price
|
name,price
|
||||||
Color TV,1200
|
Color TV,1200
|
||||||
DVD player,200
|
DVD player,200
|
||||||
|
|
||||||
PickleItemExporter
|
PickleItemExporter
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
.. class:: PickleItemExporter(file, protocol=0, **kwargs)
|
.. class:: PickleItemExporter(file, protocol=4, **kwargs)
|
||||||
|
|
||||||
Exports items in pickle format to the given file-like object.
|
Exports items in pickle format to the given file-like object.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,18 @@ Core Stats extension
|
||||||
Enable the collection of core statistics, provided the stats collection is
|
Enable the collection of core statistics, provided the stats collection is
|
||||||
enabled (see :ref:`topics-stats`).
|
enabled (see :ref:`topics-stats`).
|
||||||
|
|
||||||
.. _topics-extensions-ref-telnetconsole:
|
The following stats are collected:
|
||||||
|
|
||||||
|
* ``start_time``: start date/time of the crawl (:class:`~datetime.datetime`).
|
||||||
|
* ``finish_time``: end date/time of the crawl (:class:`~datetime.datetime`).
|
||||||
|
* ``elapsed_time_seconds``: total crawl duration in seconds (:class:`float`).
|
||||||
|
* ``finish_reason``: the closing reason string (e.g. ``"finished"``,
|
||||||
|
``"closespider_timeout"``).
|
||||||
|
* ``item_scraped_count``: total number of items that passed all pipelines.
|
||||||
|
* ``item_dropped_count``: total number of items dropped by a pipeline.
|
||||||
|
* ``item_dropped_reasons_count/<ExceptionName>``: per-exception drop count
|
||||||
|
(e.g. ``item_dropped_reasons_count/DropItem``).
|
||||||
|
* ``response_received_count``: total number of HTTP responses received.
|
||||||
|
|
||||||
Log Count extension
|
Log Count extension
|
||||||
~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
@ -146,6 +157,8 @@ Log Count extension
|
||||||
|
|
||||||
.. autoclass:: LogCount
|
.. autoclass:: LogCount
|
||||||
|
|
||||||
|
.. _topics-extensions-ref-telnetconsole:
|
||||||
|
|
||||||
Telnet console extension
|
Telnet console extension
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
@ -247,6 +260,7 @@ settings:
|
||||||
* :setting:`CLOSESPIDER_TIMEOUT_NO_ITEM`
|
* :setting:`CLOSESPIDER_TIMEOUT_NO_ITEM`
|
||||||
* :setting:`CLOSESPIDER_ITEMCOUNT`
|
* :setting:`CLOSESPIDER_ITEMCOUNT`
|
||||||
* :setting:`CLOSESPIDER_PAGECOUNT`
|
* :setting:`CLOSESPIDER_PAGECOUNT`
|
||||||
|
* :setting:`CLOSESPIDER_PAGECOUNT_NO_ITEM`
|
||||||
* :setting:`CLOSESPIDER_ERRORCOUNT`
|
* :setting:`CLOSESPIDER_ERRORCOUNT`
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
@ -260,12 +274,11 @@ settings:
|
||||||
CLOSESPIDER_TIMEOUT
|
CLOSESPIDER_TIMEOUT
|
||||||
"""""""""""""""""""
|
"""""""""""""""""""
|
||||||
|
|
||||||
Default: ``0``
|
Default: ``0.0``
|
||||||
|
|
||||||
An integer which specifies a number of seconds. If the spider remains open for
|
If the spider remains open for more than this number of seconds, it will be
|
||||||
more than that number of seconds, it will be automatically closed with the
|
automatically closed with the reason ``closespider_timeout``. If zero (or non
|
||||||
reason ``closespider_timeout``. If zero (or non set), spiders won't be closed by
|
set), spiders won't be closed by timeout.
|
||||||
timeout.
|
|
||||||
|
|
||||||
.. setting:: CLOSESPIDER_TIMEOUT_NO_ITEM
|
.. setting:: CLOSESPIDER_TIMEOUT_NO_ITEM
|
||||||
|
|
||||||
|
|
@ -328,9 +341,6 @@ closing the spider. If the spider generates more than that number of errors,
|
||||||
it will be closed with the reason ``closespider_errorcount``. If zero (or non
|
it will be closed with the reason ``closespider_errorcount``. If zero (or non
|
||||||
set), spiders won't be closed by number of errors.
|
set), spiders won't be closed by number of errors.
|
||||||
|
|
||||||
.. module:: scrapy.extensions.debug
|
|
||||||
:synopsis: Extensions for debugging Scrapy
|
|
||||||
|
|
||||||
.. module:: scrapy.extensions.periodic_log
|
.. module:: scrapy.extensions.periodic_log
|
||||||
:synopsis: Periodic stats logging
|
:synopsis: Periodic stats logging
|
||||||
|
|
||||||
|
|
@ -405,7 +415,7 @@ Example extension configuration:
|
||||||
custom_settings = {
|
custom_settings = {
|
||||||
"LOG_LEVEL": "INFO",
|
"LOG_LEVEL": "INFO",
|
||||||
"PERIODIC_LOG_STATS": {
|
"PERIODIC_LOG_STATS": {
|
||||||
"include": ["downloader/", "scheduler/", "log_count/", "item_scraped_count/"],
|
"include": ["downloader/", "scheduler/", "log_count/", "item_scraped_count"],
|
||||||
},
|
},
|
||||||
"PERIODIC_LOG_DELTA": {"include": ["downloader/"]},
|
"PERIODIC_LOG_DELTA": {"include": ["downloader/"]},
|
||||||
"PERIODIC_LOG_TIMING_ENABLED": True,
|
"PERIODIC_LOG_TIMING_ENABLED": True,
|
||||||
|
|
@ -450,6 +460,9 @@ Default: ``False``
|
||||||
Debugging extensions
|
Debugging extensions
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
.. module:: scrapy.extensions.debug
|
||||||
|
:synopsis: Extensions for debugging Scrapy
|
||||||
|
|
||||||
Stack trace dump extension
|
Stack trace dump extension
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,11 @@ Here are some examples to illustrate:
|
||||||
.. note:: :ref:`Spider arguments <spiderargs>` become spider attributes, hence
|
.. note:: :ref:`Spider arguments <spiderargs>` become spider attributes, hence
|
||||||
they can also be used as storage URI parameters.
|
they can also be used as storage URI parameters.
|
||||||
|
|
||||||
|
.. note:: Only ``%(...)s`` parameters are replaced. Any other percent
|
||||||
|
character is kept as-is, so percent-encoded URIs (e.g. ``%20`` for a
|
||||||
|
space or percent-encoded FTP credentials) and :class:`pathlib.Path`
|
||||||
|
keys containing ``%(...)s`` parameters both work as expected.
|
||||||
|
|
||||||
|
|
||||||
.. _topics-feed-storage-backends:
|
.. _topics-feed-storage-backends:
|
||||||
|
|
||||||
|
|
@ -161,7 +166,7 @@ The feeds are stored in the local filesystem.
|
||||||
- Required external libraries: none
|
- Required external libraries: none
|
||||||
|
|
||||||
Note that for the local filesystem storage (only) you can omit the scheme if
|
Note that for the local filesystem storage (only) you can omit the scheme if
|
||||||
you specify an absolute path like ``/tmp/export.csv`` (Unix systems only).
|
you specify a path (e.g. ``/tmp/export.csv``).
|
||||||
Alternatively you can also use a :class:`pathlib.Path` object.
|
Alternatively you can also use a :class:`pathlib.Path` object.
|
||||||
|
|
||||||
.. _topics-feed-storage-ftp:
|
.. _topics-feed-storage-ftp:
|
||||||
|
|
@ -528,10 +533,6 @@ safe numeric encoding (``\uXXXX`` sequences) for historic reasons.
|
||||||
|
|
||||||
Use ``"utf-8"`` if you want UTF-8 for JSON too.
|
Use ``"utf-8"`` if you want UTF-8 for JSON too.
|
||||||
|
|
||||||
.. versionchanged:: 2.8
|
|
||||||
The :command:`startproject` command now sets this setting to
|
|
||||||
``"utf-8"`` in the generated ``settings.py`` file.
|
|
||||||
|
|
||||||
.. setting:: FEED_EXPORT_FIELDS
|
.. setting:: FEED_EXPORT_FIELDS
|
||||||
|
|
||||||
FEED_EXPORT_FIELDS
|
FEED_EXPORT_FIELDS
|
||||||
|
|
@ -619,6 +620,7 @@ Default:
|
||||||
"file": "scrapy.extensions.feedexport.FileFeedStorage",
|
"file": "scrapy.extensions.feedexport.FileFeedStorage",
|
||||||
"stdout": "scrapy.extensions.feedexport.StdoutFeedStorage",
|
"stdout": "scrapy.extensions.feedexport.StdoutFeedStorage",
|
||||||
"s3": "scrapy.extensions.feedexport.S3FeedStorage",
|
"s3": "scrapy.extensions.feedexport.S3FeedStorage",
|
||||||
|
"gs": "scrapy.extensions.feedexport.GCSFeedStorage",
|
||||||
"ftp": "scrapy.extensions.feedexport.FTPFeedStorage",
|
"ftp": "scrapy.extensions.feedexport.FTPFeedStorage",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -760,8 +762,8 @@ The function signature should be as follows:
|
||||||
:param spider: source spider of the feed items
|
:param spider: source spider of the feed items
|
||||||
:type spider: scrapy.Spider
|
:type spider: scrapy.Spider
|
||||||
|
|
||||||
.. caution:: The function should return a new dictionary, modifying
|
.. caution:: The function must return a new dictionary instead of modifying
|
||||||
the received ``params`` in-place is deprecated.
|
the received ``params`` in-place.
|
||||||
|
|
||||||
For example, to include the :attr:`name <scrapy.Spider.name>` of the
|
For example, to include the :attr:`name <scrapy.Spider.name>` of the
|
||||||
source spider in the feed URI:
|
source spider in the feed URI:
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ Any of these methods may be defined as a coroutine function (``async def``).
|
||||||
Item pipeline example
|
Item pipeline example
|
||||||
=====================
|
=====================
|
||||||
|
|
||||||
|
.. _price-pipeline-example:
|
||||||
|
|
||||||
Price validation and dropping items with no prices
|
Price validation and dropping items with no prices
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -119,7 +121,7 @@ Write items to MongoDB
|
||||||
|
|
||||||
In this example we'll write items to MongoDB_ using pymongo_.
|
In this example we'll write items to MongoDB_ using pymongo_.
|
||||||
MongoDB address and database name are specified in Scrapy settings;
|
MongoDB address and database name are specified in Scrapy settings;
|
||||||
MongoDB collection is named after item class.
|
MongoDB collection is specified in a class attribute.
|
||||||
|
|
||||||
The main point of this example is to show how to :ref:`get the crawler
|
The main point of this example is to show how to :ref:`get the crawler
|
||||||
<from-crawler>` and how to clean up the resources properly.
|
<from-crawler>` and how to clean up the resources properly.
|
||||||
|
|
@ -246,6 +248,8 @@ returns multiples items with the same id:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
.. _activating-item-pipeline:
|
||||||
|
|
||||||
Activating an Item Pipeline component
|
Activating an Item Pipeline component
|
||||||
=====================================
|
=====================================
|
||||||
|
|
||||||
|
|
@ -262,3 +266,110 @@ To activate an Item Pipeline component you must add its class to the
|
||||||
The integer values you assign to classes in this setting determine the
|
The integer values you assign to classes in this setting determine the
|
||||||
order in which they run: items go through from lower valued to higher
|
order in which they run: items go through from lower valued to higher
|
||||||
valued classes. It's customary to define these numbers in the 0-1000 range.
|
valued classes. It's customary to define these numbers in the 0-1000 range.
|
||||||
|
|
||||||
|
A complete example
|
||||||
|
==================
|
||||||
|
|
||||||
|
The examples above show item pipeline components on their own. In a project, a
|
||||||
|
pipeline is one of four pieces that work together: the :ref:`item
|
||||||
|
<topics-items>` your spider produces, the :ref:`spider <topics-spiders>` that
|
||||||
|
yields it, the pipeline that processes it, and the :setting:`ITEM_PIPELINES`
|
||||||
|
setting that enables the pipeline.
|
||||||
|
|
||||||
|
The following example wires those pieces together to validate the price of
|
||||||
|
books scraped from `books.toscrape.com`_, reusing the ``PricePipeline`` from
|
||||||
|
:ref:`price-pipeline-example` above.
|
||||||
|
|
||||||
|
Define the item in ``myproject/items.py``:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BookItem:
|
||||||
|
title: str
|
||||||
|
price: float
|
||||||
|
|
||||||
|
Yield instances of that item from your spider, e.g. in
|
||||||
|
``myproject/spiders/books.py``:
|
||||||
|
|
||||||
|
.. skip: next
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
import scrapy
|
||||||
|
|
||||||
|
from myproject.items import BookItem
|
||||||
|
|
||||||
|
|
||||||
|
class BooksSpider(scrapy.Spider):
|
||||||
|
name = "books"
|
||||||
|
start_urls = ["https://books.toscrape.com/"]
|
||||||
|
|
||||||
|
def parse(self, response):
|
||||||
|
for book in response.css("article.product_pod"):
|
||||||
|
yield BookItem(
|
||||||
|
title=book.css("h3 a::attr(title)").get(),
|
||||||
|
price=float(book.css("p.price_color::text").re_first(r"[\d.]+")),
|
||||||
|
)
|
||||||
|
|
||||||
|
Put the ``PricePipeline`` shown earlier in ``myproject/pipelines.py``, and
|
||||||
|
enable it in ``myproject/settings.py``:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
ITEM_PIPELINES = {
|
||||||
|
"myproject.pipelines.PricePipeline": 300,
|
||||||
|
}
|
||||||
|
|
||||||
|
With these pieces in place, every ``BookItem`` that ``BooksSpider`` yields
|
||||||
|
passes through ``PricePipeline`` before it reaches the :ref:`feed exports
|
||||||
|
<topics-feed-exports>` or any other output.
|
||||||
|
|
||||||
|
.. _books.toscrape.com: https://books.toscrape.com/
|
||||||
|
|
||||||
|
|
||||||
|
Common pitfalls
|
||||||
|
===============
|
||||||
|
|
||||||
|
The pipeline does not run
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
A pipeline component only runs if its class is listed in the
|
||||||
|
:setting:`ITEM_PIPELINES` setting, normally in your project's
|
||||||
|
:file:`settings.py` file (see :ref:`activating-item-pipeline`). Adding it to
|
||||||
|
the spider or elsewhere has no effect.
|
||||||
|
|
||||||
|
To confirm that Scrapy loaded your pipeline, look for a line like this near the
|
||||||
|
start of the crawl log::
|
||||||
|
|
||||||
|
[scrapy.middleware] INFO: Enabled item pipelines:
|
||||||
|
['myproject.pipelines.PricePipeline']
|
||||||
|
|
||||||
|
If your pipeline is missing from that list, check that its import path matches
|
||||||
|
the :setting:`ITEM_PIPELINES` entry, and that the setting is not being
|
||||||
|
overridden, for example by :attr:`~scrapy.Spider.custom_settings` or by a
|
||||||
|
redefinition of :setting:`ITEM_PIPELINES` in :file:`settings.py`.
|
||||||
|
|
||||||
|
The item is not returned
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
:meth:`process_item` must return the item (or raise
|
||||||
|
:exc:`~scrapy.exceptions.DropItem`). A common mistake is to modify the item but
|
||||||
|
forget to return it:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
def process_item(self, item):
|
||||||
|
ItemAdapter(item)["price"] *= 1.15
|
||||||
|
# Bug: returns None, so the next component gets None instead of the item.
|
||||||
|
|
||||||
|
Return the item so that the next component, and the rest of Scrapy, can keep
|
||||||
|
processing it:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
def process_item(self, item):
|
||||||
|
ItemAdapter(item)["price"] *= 1.15
|
||||||
|
return item
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,8 @@ Item Types
|
||||||
|
|
||||||
Scrapy supports the following types of items, via the `itemadapter`_ library:
|
Scrapy supports the following types of items, via the `itemadapter`_ library:
|
||||||
:ref:`dictionaries <dict-items>`, :ref:`Item objects <item-objects>`,
|
:ref:`dictionaries <dict-items>`, :ref:`Item objects <item-objects>`,
|
||||||
:ref:`dataclass objects <dataclass-items>`, and :ref:`attrs objects <attrs-items>`.
|
:ref:`dataclass objects <dataclass-items>`, :ref:`attrs objects <attrs-items>`
|
||||||
|
and :ref:`Pydantic models <pydantic-items>`.
|
||||||
|
|
||||||
.. _itemadapter: https://github.com/scrapy/itemadapter
|
.. _itemadapter: https://github.com/scrapy/itemadapter
|
||||||
|
|
||||||
|
|
@ -61,8 +62,8 @@ its ``__init__`` method.
|
||||||
:class:`Item` also allows the defining of field metadata, which can be used to
|
:class:`Item` also allows the defining of field metadata, which can be used to
|
||||||
:ref:`customize serialization <topics-exporters-field-serialization>`.
|
:ref:`customize serialization <topics-exporters-field-serialization>`.
|
||||||
|
|
||||||
:mod:`trackref` tracks :class:`Item` objects to help find memory leaks
|
:mod:`scrapy.utils.trackref` tracks :class:`Item` objects to help find memory
|
||||||
(see :ref:`topics-leaks-trackrefs`).
|
leaks (see :ref:`topics-leaks-trackrefs`).
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
|
@ -262,7 +263,7 @@ Creating items
|
||||||
|
|
||||||
>>> product = Product(name="Desktop PC", price=1000)
|
>>> product = Product(name="Desktop PC", price=1000)
|
||||||
>>> print(product)
|
>>> print(product)
|
||||||
Product(name='Desktop PC', price=1000)
|
{'name': 'Desktop PC', 'price': 1000}
|
||||||
|
|
||||||
|
|
||||||
Getting field values
|
Getting field values
|
||||||
|
|
@ -376,10 +377,12 @@ Creating dicts from items:
|
||||||
>>> dict(product) # create a dict from all populated values
|
>>> dict(product) # create a dict from all populated values
|
||||||
{'price': 1000, 'name': 'Desktop PC'}
|
{'price': 1000, 'name': 'Desktop PC'}
|
||||||
|
|
||||||
Creating items from dicts:
|
Creating items from dicts:
|
||||||
|
|
||||||
|
.. code-block:: pycon
|
||||||
|
|
||||||
>>> Product({"name": "Laptop PC", "price": 1500})
|
>>> Product({"name": "Laptop PC", "price": 1500})
|
||||||
Product(price=1500, name='Laptop PC')
|
{'name': 'Laptop PC', 'price': 1500}
|
||||||
|
|
||||||
>>> Product({"name": "Laptop PC", "lala": 1500}) # warning: unknown field in dict
|
>>> Product({"name": "Laptop PC", "lala": 1500}) # warning: unknown field in dict
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,15 @@ If you wish to log the requests that couldn't be serialized, you can set the
|
||||||
:setting:`SCHEDULER_DEBUG` setting to ``True`` in the project's settings page.
|
:setting:`SCHEDULER_DEBUG` setting to ``True`` in the project's settings page.
|
||||||
It is ``False`` by default.
|
It is ``False`` by default.
|
||||||
|
|
||||||
|
.. note:: Because requests are serialized with :mod:`pickle`, the objects you
|
||||||
|
store on a request, such as the values of its
|
||||||
|
:attr:`~scrapy.Request.cb_kwargs` and :attr:`~scrapy.Request.meta`
|
||||||
|
dictionaries, are deep-copied when the request is written to and later read
|
||||||
|
back from the job directory. As a result, the callback receives a *copy* of
|
||||||
|
those objects rather than the original ones, and changes made to the copy are
|
||||||
|
not reflected in the original object. Keep this in mind if you rely on
|
||||||
|
sharing mutable state through ``cb_kwargs`` or ``meta``.
|
||||||
|
|
||||||
.. _job-dir-contents:
|
.. _job-dir-contents:
|
||||||
|
|
||||||
Job directory contents
|
Job directory contents
|
||||||
|
|
@ -142,8 +151,8 @@ Where:
|
||||||
- :class:`~scrapy.pqueues.ScrapyPriorityQueue` creates the ``{priority}{s?}``
|
- :class:`~scrapy.pqueues.ScrapyPriorityQueue` creates the ``{priority}{s?}``
|
||||||
directories.
|
directories.
|
||||||
|
|
||||||
- :class:`scrapy.squeues.PickleLifoDiskQueue`, a subclass of
|
- :class:`scrapy.squeues.PickleFifoDiskQueue`, a subclass of
|
||||||
:class:`queuelib.LifoDiskQueue` that uses :mod:`pickle` to serialize
|
:class:`queuelib.FifoDiskQueue` that uses :mod:`pickle` to serialize
|
||||||
:class:`dict` representations of :class:`scrapy.Request` objects, creates
|
:class:`dict` representations of :class:`scrapy.Request` objects, creates
|
||||||
the ``info.json`` and ``q{00000}`` files.
|
the ``info.json`` and ``q{00000}`` files.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,25 +62,27 @@ Debugging memory leaks with ``trackref``
|
||||||
|
|
||||||
.. skip: start
|
.. skip: start
|
||||||
|
|
||||||
:mod:`trackref` is a module provided by Scrapy to debug the most common cases of
|
:mod:`scrapy.utils.trackref` is a module provided by Scrapy to debug the most
|
||||||
memory leaks. It basically tracks the references to all live Request,
|
common cases of memory leaks. It basically tracks the references to all live
|
||||||
Response, Item, Spider and Selector objects.
|
Request, Response, Item, Spider and Selector objects.
|
||||||
|
|
||||||
You can enter the telnet console and inspect how many objects (of the classes
|
You can enter the telnet console and inspect how many objects (of the classes
|
||||||
mentioned above) are currently alive using the ``prefs()`` function which is an
|
mentioned above) are currently alive using the ``prefs()`` function which is an
|
||||||
alias to the :func:`~scrapy.utils.trackref.print_live_refs` function::
|
alias to the :func:`~scrapy.utils.trackref.print_live_refs` function:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
telnet localhost 6023
|
telnet localhost 6023
|
||||||
|
|
||||||
.. code-block:: pycon
|
.. code-block:: pycon
|
||||||
|
|
||||||
>>> prefs()
|
>>> prefs()
|
||||||
Live References
|
Live References
|
||||||
|
|
||||||
ExampleSpider 1 oldest: 15s ago
|
ExampleSpider 1 oldest: 15s ago
|
||||||
HtmlResponse 10 oldest: 1s ago
|
HtmlResponse 10 oldest: 1s ago
|
||||||
Selector 2 oldest: 0s ago
|
Selector 2 oldest: 0s ago
|
||||||
FormRequest 878 oldest: 7s ago
|
Request 878 oldest: 7s ago
|
||||||
|
|
||||||
As you can see, that report also shows the "age" of the oldest object in each
|
As you can see, that report also shows the "age" of the oldest object in each
|
||||||
class. If you're running multiple spiders per process chances are you can
|
class. If you're running multiple spiders per process chances are you can
|
||||||
|
|
@ -91,7 +93,7 @@ You can get the oldest object of each class using the
|
||||||
Which objects are tracked?
|
Which objects are tracked?
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
The objects tracked by ``trackrefs`` are all from these classes (and all its
|
The objects tracked by ``trackref`` are all from these classes (and all its
|
||||||
subclasses):
|
subclasses):
|
||||||
|
|
||||||
* :class:`scrapy.Request`
|
* :class:`scrapy.Request`
|
||||||
|
|
@ -162,7 +164,7 @@ Too many spiders?
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
If your project has too many spiders executed in parallel,
|
If your project has too many spiders executed in parallel,
|
||||||
the output of :func:`prefs` can be difficult to read.
|
the output of ``prefs()`` can be difficult to read.
|
||||||
For this reason, that function has a ``ignore`` argument which can be used to
|
For this reason, that function has a ``ignore`` argument which can be used to
|
||||||
ignore a particular class (and all its subclasses). For
|
ignore a particular class (and all its subclasses). For
|
||||||
example, this won't show any live references to spiders:
|
example, this won't show any live references to spiders:
|
||||||
|
|
@ -185,7 +187,7 @@ Here are the functions available in the :mod:`~scrapy.utils.trackref` module.
|
||||||
Inherit from this class if you want to track live
|
Inherit from this class if you want to track live
|
||||||
instances with the ``trackref`` module.
|
instances with the ``trackref`` module.
|
||||||
|
|
||||||
.. function:: print_live_refs(class_name, ignore=NoneType)
|
.. function:: print_live_refs(ignore=NoneType)
|
||||||
|
|
||||||
Print a report of live references, grouped by class name.
|
Print a report of live references, grouped by class name.
|
||||||
|
|
||||||
|
|
@ -201,9 +203,9 @@ Here are the functions available in the :mod:`~scrapy.utils.trackref` module.
|
||||||
|
|
||||||
.. function:: iter_all(class_name)
|
.. function:: iter_all(class_name)
|
||||||
|
|
||||||
Return an iterator over all objects alive with the given class name, or
|
Return an iterator over all objects alive with the given class name. Use
|
||||||
``None`` if none is found. Use :func:`print_live_refs` first to get a list
|
:func:`print_live_refs` first to get a list of all tracked live objects
|
||||||
of all tracked live objects per class name.
|
per class name.
|
||||||
|
|
||||||
.. skip: end
|
.. skip: end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,108 +47,7 @@ LxmlLinkExtractor
|
||||||
:synopsis: lxml's HTMLParser-based link extractors
|
:synopsis: lxml's HTMLParser-based link extractors
|
||||||
|
|
||||||
|
|
||||||
.. class:: LxmlLinkExtractor(allow=(), deny=(), allow_domains=(), deny_domains=(), deny_extensions=None, restrict_xpaths=(), restrict_css=(), tags=('a', 'area'), attrs=('href',), canonicalize=False, unique=True, process_value=None, strip=True)
|
.. autoclass:: LxmlLinkExtractor
|
||||||
|
|
||||||
LxmlLinkExtractor is the recommended link extractor with handy filtering
|
|
||||||
options. It is implemented using lxml's robust HTMLParser.
|
|
||||||
|
|
||||||
:param allow: a single regular expression (or list of regular expressions)
|
|
||||||
that the (absolute) urls must match in order to be extracted. If not
|
|
||||||
given (or empty), it will match all links.
|
|
||||||
:type allow: str or list
|
|
||||||
|
|
||||||
:param deny: a single regular expression (or list of regular expressions)
|
|
||||||
that the (absolute) urls must match in order to be excluded (i.e. not
|
|
||||||
extracted). It has precedence over the ``allow`` parameter. If not
|
|
||||||
given (or empty) it won't exclude any links.
|
|
||||||
:type deny: str or list
|
|
||||||
|
|
||||||
:param allow_domains: a single value or a list of string containing
|
|
||||||
domains which will be considered for extracting the links
|
|
||||||
:type allow_domains: str or list
|
|
||||||
|
|
||||||
:param deny_domains: a single value or a list of strings containing
|
|
||||||
domains which won't be considered for extracting the links
|
|
||||||
:type deny_domains: str or list
|
|
||||||
|
|
||||||
:param deny_extensions: a single value or list of strings containing
|
|
||||||
extensions that should be ignored when extracting links.
|
|
||||||
If not given, it will default to
|
|
||||||
:data:`scrapy.linkextractors.IGNORED_EXTENSIONS`.
|
|
||||||
|
|
||||||
:type deny_extensions: list
|
|
||||||
|
|
||||||
:param restrict_xpaths: is an XPath (or list of XPath's) which defines
|
|
||||||
regions inside the response where links should be extracted from.
|
|
||||||
If given, only the text selected by those XPath will be scanned for
|
|
||||||
links.
|
|
||||||
:type restrict_xpaths: str or list
|
|
||||||
|
|
||||||
:param restrict_css: a CSS selector (or list of selectors) which defines
|
|
||||||
regions inside the response where links should be extracted from.
|
|
||||||
Has the same behaviour as ``restrict_xpaths``.
|
|
||||||
:type restrict_css: str or list
|
|
||||||
|
|
||||||
:param restrict_text: a single regular expression (or list of regular expressions)
|
|
||||||
that the link's text must match in order to be extracted. If not
|
|
||||||
given (or empty), it will match all links. If a list of regular expressions is
|
|
||||||
given, the link will be extracted if it matches at least one.
|
|
||||||
:type restrict_text: str or list
|
|
||||||
|
|
||||||
:param tags: a tag or a list of tags to consider when extracting links.
|
|
||||||
Defaults to ``('a', 'area')``.
|
|
||||||
:type tags: str or list
|
|
||||||
|
|
||||||
:param attrs: an attribute or list of attributes which should be considered when looking
|
|
||||||
for links to extract (only for those tags specified in the ``tags``
|
|
||||||
parameter). Defaults to ``('href',)``
|
|
||||||
:type attrs: list
|
|
||||||
|
|
||||||
:param canonicalize: canonicalize each extracted url (using
|
|
||||||
w3lib.url.canonicalize_url). Defaults to ``False``.
|
|
||||||
Note that canonicalize_url is meant for duplicate checking;
|
|
||||||
it can change the URL visible at server side, so the response can be
|
|
||||||
different for requests with canonicalized and raw URLs. If you're
|
|
||||||
using LinkExtractor to follow links it is more robust to
|
|
||||||
keep the default ``canonicalize=False``.
|
|
||||||
:type canonicalize: bool
|
|
||||||
|
|
||||||
:param unique: whether duplicate filtering should be applied to extracted
|
|
||||||
links.
|
|
||||||
:type unique: bool
|
|
||||||
|
|
||||||
:param process_value: a function which receives each value extracted from
|
|
||||||
the tag and attributes scanned and can modify the value and return a
|
|
||||||
new one, or return ``None`` to ignore the link altogether. If not
|
|
||||||
given, ``process_value`` defaults to ``lambda x: x``.
|
|
||||||
|
|
||||||
.. highlight:: html
|
|
||||||
|
|
||||||
For example, to extract links from this code::
|
|
||||||
|
|
||||||
<a href="javascript:goToPage('../other/page.html'); return false">Link text</a>
|
|
||||||
|
|
||||||
.. highlight:: python
|
|
||||||
|
|
||||||
You can use the following function in ``process_value``:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
def process_value(value):
|
|
||||||
m = re.search(r"javascript:goToPage\('(.*?)'", value)
|
|
||||||
if m:
|
|
||||||
return m.group(1)
|
|
||||||
|
|
||||||
:type process_value: collections.abc.Callable
|
|
||||||
|
|
||||||
:param strip: whether to strip whitespaces from extracted attributes.
|
|
||||||
According to HTML5 standard, leading and trailing whitespaces
|
|
||||||
must be stripped from ``href`` attributes of ``<a>``, ``<area>``
|
|
||||||
and many other elements, ``src`` attribute of ``<img>``, ``<iframe>``
|
|
||||||
elements, etc., so LinkExtractor strips space chars by default.
|
|
||||||
Set ``strip=False`` to turn it off (e.g. if you're extracting urls
|
|
||||||
from elements or attributes which allow leading/trailing whitespaces).
|
|
||||||
:type strip: bool
|
|
||||||
|
|
||||||
.. automethod:: extract_links
|
.. automethod:: extract_links
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ data that will be assigned to the ``name`` field later.
|
||||||
|
|
||||||
Afterwards, similar calls are used for ``price`` and ``stock`` fields
|
Afterwards, similar calls are used for ``price`` and ``stock`` fields
|
||||||
(the latter using a CSS selector with the :meth:`~ItemLoader.add_css` method),
|
(the latter using a CSS selector with the :meth:`~ItemLoader.add_css` method),
|
||||||
and finally the ``last_update`` field is populated directly with a literal value
|
and finally the ``last_updated`` field is populated directly with a literal value
|
||||||
(``today``) using a different method: :meth:`~ItemLoader.add_value`.
|
(``today``) using a different method: :meth:`~ItemLoader.add_value`.
|
||||||
|
|
||||||
Finally, when all data is collected, the :meth:`ItemLoader.load_item` method is
|
Finally, when all data is collected, the :meth:`ItemLoader.load_item` method is
|
||||||
|
|
@ -102,14 +102,13 @@ One approach to overcome this is to define items using the
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class InventoryItem:
|
class InventoryItem:
|
||||||
name: Optional[str] = field(default=None)
|
name: str | None = field(default=None)
|
||||||
price: Optional[float] = field(default=None)
|
price: float | None = field(default=None)
|
||||||
stock: Optional[int] = field(default=None)
|
stock: int | None = field(default=None)
|
||||||
|
|
||||||
|
|
||||||
.. _topics-loaders-processors:
|
.. _topics-loaders-processors:
|
||||||
|
|
@ -228,7 +227,8 @@ metadata. Here is an example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import scrapy
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
from itemloaders.processors import Join, MapCompose, TakeFirst
|
from itemloaders.processors import Join, MapCompose, TakeFirst
|
||||||
from w3lib.html import remove_tags
|
from w3lib.html import remove_tags
|
||||||
|
|
||||||
|
|
@ -238,14 +238,21 @@ metadata. Here is an example:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class Product(scrapy.Item):
|
@dataclass
|
||||||
name = scrapy.Field(
|
class Product:
|
||||||
input_processor=MapCompose(remove_tags),
|
name: str | None = field(
|
||||||
output_processor=Join(),
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"input_processor": MapCompose(remove_tags),
|
||||||
|
"output_processor": Join(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
price = scrapy.Field(
|
price: str | None = field(
|
||||||
input_processor=MapCompose(remove_tags, filter_price),
|
default=None,
|
||||||
output_processor=TakeFirst(),
|
metadata={
|
||||||
|
"input_processor": MapCompose(remove_tags, filter_price),
|
||||||
|
"output_processor": TakeFirst(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -257,7 +264,7 @@ metadata. Here is an example:
|
||||||
>>> il.add_value("name", ["Welcome to my", "<strong>website</strong>"])
|
>>> il.add_value("name", ["Welcome to my", "<strong>website</strong>"])
|
||||||
>>> il.add_value("price", ["€", "<span>1000</span>"])
|
>>> il.add_value("price", ["€", "<span>1000</span>"])
|
||||||
>>> il.load_item()
|
>>> il.load_item()
|
||||||
{'name': 'Welcome to my website', 'price': '1000'}
|
Product(name='Welcome to my website', price='1000')
|
||||||
|
|
||||||
.. skip: end
|
.. skip: end
|
||||||
|
|
||||||
|
|
@ -266,8 +273,8 @@ The precedence order, for both input and output processors, is as follows:
|
||||||
1. Item Loader field-specific attributes: ``field_in`` and ``field_out`` (most
|
1. Item Loader field-specific attributes: ``field_in`` and ``field_out`` (most
|
||||||
precedence)
|
precedence)
|
||||||
2. Field metadata (``input_processor`` and ``output_processor`` key)
|
2. Field metadata (``input_processor`` and ``output_processor`` key)
|
||||||
3. Item Loader defaults: :meth:`ItemLoader.default_input_processor` and
|
3. Item Loader defaults: :attr:`ItemLoader.default_input_processor` and
|
||||||
:meth:`ItemLoader.default_output_processor` (least precedence)
|
:attr:`ItemLoader.default_output_processor` (least precedence)
|
||||||
|
|
||||||
See also: :ref:`topics-loaders-extending`.
|
See also: :ref:`topics-loaders-extending`.
|
||||||
|
|
||||||
|
|
@ -316,8 +323,8 @@ There are several ways to modify Item Loader context values:
|
||||||
loader = ItemLoader(product, unit="cm")
|
loader = ItemLoader(product, unit="cm")
|
||||||
|
|
||||||
3. On Item Loader declaration, for those input/output processors that support
|
3. On Item Loader declaration, for those input/output processors that support
|
||||||
instantiating them with an Item Loader context. :class:`~processor.MapCompose` is one of
|
instantiating them with an Item Loader context.
|
||||||
them:
|
:class:`~itemloaders.processors.MapCompose` is one of them:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,6 @@
|
||||||
Logging
|
Logging
|
||||||
=======
|
=======
|
||||||
|
|
||||||
.. note::
|
|
||||||
:mod:`scrapy.log` has been deprecated alongside its functions in favor of
|
|
||||||
explicit calls to the Python standard logging. Keep reading to learn more
|
|
||||||
about the new logging system.
|
|
||||||
|
|
||||||
Scrapy uses :mod:`logging` for event logging. We'll
|
Scrapy uses :mod:`logging` for event logging. We'll
|
||||||
provide some simple examples to get you started, but for more advanced
|
provide some simple examples to get you started, but for more advanced
|
||||||
use-cases it's strongly suggested to read thoroughly its documentation.
|
use-cases it's strongly suggested to read thoroughly its documentation.
|
||||||
|
|
@ -194,6 +189,48 @@ If :setting:`LOG_SHORT_NAMES` is set, then the logs will not display the Scrapy
|
||||||
component that prints the log. It is unset by default, hence logs contain the
|
component that prints the log. It is unset by default, hence logs contain the
|
||||||
Scrapy component responsible for that log output.
|
Scrapy component responsible for that log output.
|
||||||
|
|
||||||
|
Rotating log files
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Scrapy's :setting:`LOG_FILE` setting writes logs to a single file. It does not
|
||||||
|
rotate log files automatically, but you can use Python's standard
|
||||||
|
:mod:`logging.handlers` module when running Scrapy from a script.
|
||||||
|
|
||||||
|
For example, to rotate the log file every day:
|
||||||
|
|
||||||
|
.. skip: next
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from logging.handlers import TimedRotatingFileHandler
|
||||||
|
|
||||||
|
from scrapy.crawler import CrawlerProcess
|
||||||
|
from scrapy.utils.project import get_project_settings
|
||||||
|
|
||||||
|
from myproject.spiders.myspider import MySpider
|
||||||
|
|
||||||
|
settings = get_project_settings()
|
||||||
|
process = CrawlerProcess(settings, install_root_handler=False)
|
||||||
|
|
||||||
|
handler = TimedRotatingFileHandler(
|
||||||
|
"scrapy.log",
|
||||||
|
when="midnight",
|
||||||
|
backupCount=7,
|
||||||
|
encoding=settings.get("LOG_ENCODING"),
|
||||||
|
)
|
||||||
|
handler.setFormatter(
|
||||||
|
logging.Formatter(settings.get("LOG_FORMAT"), settings.get("LOG_DATEFORMAT"))
|
||||||
|
)
|
||||||
|
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
root_logger.setLevel(settings.get("LOG_LEVEL"))
|
||||||
|
root_logger.addHandler(handler)
|
||||||
|
|
||||||
|
process.crawl(MySpider)
|
||||||
|
process.start()
|
||||||
|
|
||||||
|
|
||||||
Command-line options
|
Command-line options
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,10 @@ this:
|
||||||
2. The item is returned from the spider and goes to the item pipeline.
|
2. The item is returned from the spider and goes to the item pipeline.
|
||||||
|
|
||||||
3. When the item reaches the :class:`FilesPipeline`, the URLs in the
|
3. When the item reaches the :class:`FilesPipeline`, the URLs in the
|
||||||
``file_urls`` field are scheduled for download using the standard
|
``file_urls`` field are downloaded using the standard Scrapy downloader
|
||||||
Scrapy scheduler and downloader (which means the scheduler and downloader
|
(which means the downloader middlewares are used, but the spider middlewares
|
||||||
middlewares are reused), but with a higher priority, processing them before other
|
aren't). The item remains "locked" at that particular pipeline stage until
|
||||||
pages are scraped. The item remains "locked" at that particular pipeline stage
|
the files have finished downloading (or failed for some reason).
|
||||||
until the files have finish downloading (or fail for some reason).
|
|
||||||
|
|
||||||
4. When the files are downloaded, another field (``files``) will be populated
|
4. When the files are downloaded, another field (``files``) will be populated
|
||||||
with the results. This field will contain a list of dicts with information
|
with the results. This field will contain a list of dicts with information
|
||||||
|
|
@ -81,9 +80,6 @@ thumbnailing and normalizing images to JPEG/RGB format.
|
||||||
Enabling your Media Pipeline
|
Enabling your Media Pipeline
|
||||||
============================
|
============================
|
||||||
|
|
||||||
.. setting:: IMAGES_STORE
|
|
||||||
.. setting:: FILES_STORE
|
|
||||||
|
|
||||||
To enable your media pipeline you must first add it to your project
|
To enable your media pipeline you must first add it to your project
|
||||||
:setting:`ITEM_PIPELINES` setting.
|
:setting:`ITEM_PIPELINES` setting.
|
||||||
|
|
||||||
|
|
@ -102,6 +98,8 @@ For Files Pipeline, use:
|
||||||
.. note::
|
.. note::
|
||||||
You can also use both the Files and Images Pipeline at the same time.
|
You can also use both the Files and Images Pipeline at the same time.
|
||||||
|
|
||||||
|
.. setting:: IMAGES_STORE
|
||||||
|
.. setting:: FILES_STORE
|
||||||
|
|
||||||
Then, configure the target storage setting to a valid value that will be used
|
Then, configure the target storage setting to a valid value that will be used
|
||||||
for storing the downloaded images. Otherwise the pipeline will remain disabled,
|
for storing the downloaded images. Otherwise the pipeline will remain disabled,
|
||||||
|
|
@ -337,17 +335,18 @@ respectively), the pipeline will put the results under the respective field
|
||||||
When using :ref:`item types <item-types>` for which fields are defined beforehand,
|
When using :ref:`item types <item-types>` for which fields are defined beforehand,
|
||||||
you must define both the URLs field and the results field. For example, when
|
you must define both the URLs field and the results field. For example, when
|
||||||
using the images pipeline, items must define both the ``image_urls`` and the
|
using the images pipeline, items must define both the ``image_urls`` and the
|
||||||
``images`` field. For instance, using the :class:`~scrapy.Item` class:
|
``images`` field. For instance, using a dataclass:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import scrapy
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
|
||||||
class MyItem(scrapy.Item):
|
@dataclass
|
||||||
|
class MyItem:
|
||||||
# ... other item fields ...
|
# ... other item fields ...
|
||||||
image_urls = scrapy.Field()
|
image_urls: list[str] = field(default_factory=list)
|
||||||
images = scrapy.Field()
|
images: list[dict] = field(default_factory=list)
|
||||||
|
|
||||||
If you want to use another field name for the URLs key or for the results key,
|
If you want to use another field name for the URLs key or for the results key,
|
||||||
it is also possible to override it.
|
it is also possible to override it.
|
||||||
|
|
@ -371,11 +370,12 @@ For the Images Pipeline, set :setting:`IMAGES_URLS_FIELD` and/or
|
||||||
If you need something more complex and want to override the custom pipeline
|
If you need something more complex and want to override the custom pipeline
|
||||||
behaviour, see :ref:`topics-media-pipeline-override`.
|
behaviour, see :ref:`topics-media-pipeline-override`.
|
||||||
|
|
||||||
If you have multiple image pipelines inheriting from ImagePipeline and you want
|
If you have multiple image pipelines inheriting from :class:`ImagesPipeline`
|
||||||
to have different settings in different pipelines you can set setting keys
|
and you want to have different settings in different pipelines you can set
|
||||||
preceded with uppercase name of your pipeline class. E.g. if your pipeline is
|
setting keys preceded with uppercase name of your pipeline class. E.g. if your
|
||||||
called MyPipeline and you want to have custom IMAGES_URLS_FIELD you define
|
pipeline is called ``MyPipeline`` and you want to have custom
|
||||||
setting MYPIPELINE_IMAGES_URLS_FIELD and your custom settings will be used.
|
:setting:`IMAGES_URLS_FIELD` you define setting
|
||||||
|
``MYPIPELINE_IMAGES_URLS_FIELD`` and your custom settings will be used.
|
||||||
|
|
||||||
|
|
||||||
Additional features
|
Additional features
|
||||||
|
|
@ -547,10 +547,9 @@ See here the methods that you can override in your custom Files Pipeline:
|
||||||
|
|
||||||
.. method:: FilesPipeline.get_media_requests(item, info)
|
.. method:: FilesPipeline.get_media_requests(item, info)
|
||||||
|
|
||||||
As seen on the workflow, the pipeline will get the URLs of the images to
|
As seen on the workflow, the pipeline will get the requests for the files
|
||||||
download from the item. In order to do this, you can override the
|
to download from the item by calling this method. You can override it to
|
||||||
:meth:`~get_media_requests` method and return a Request for each
|
change what requests are returned:
|
||||||
file URL:
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
|
@ -590,8 +589,9 @@ See here the methods that you can override in your custom Files Pipeline:
|
||||||
* ``downloaded`` - file was downloaded.
|
* ``downloaded`` - file was downloaded.
|
||||||
* ``uptodate`` - file was not downloaded, as it was downloaded recently,
|
* ``uptodate`` - file was not downloaded, as it was downloaded recently,
|
||||||
according to the file expiration policy.
|
according to the file expiration policy.
|
||||||
* ``cached`` - file was already scheduled for download, by another item
|
* ``cached`` - file was taken from a cache (the response has a
|
||||||
sharing the same file.
|
``"cached"`` flag, e.g. from
|
||||||
|
:class:`~scrapy.downloadermiddlewares.httpcache.HttpCacheMiddleware`).
|
||||||
|
|
||||||
The list of tuples received by :meth:`~item_completed` is
|
The list of tuples received by :meth:`~item_completed` is
|
||||||
guaranteed to retain the same order of the requests returned from the
|
guaranteed to retain the same order of the requests returned from the
|
||||||
|
|
@ -618,9 +618,6 @@ See here the methods that you can override in your custom Files Pipeline:
|
||||||
(False, Failure(...)),
|
(False, Failure(...)),
|
||||||
]
|
]
|
||||||
|
|
||||||
By default the :meth:`get_media_requests` method returns ``None`` which
|
|
||||||
means there are no files to download for the item.
|
|
||||||
|
|
||||||
.. method:: FilesPipeline.item_completed(results, item, info)
|
.. method:: FilesPipeline.item_completed(results, item, info)
|
||||||
|
|
||||||
The :meth:`FilesPipeline.item_completed` method called when all file
|
The :meth:`FilesPipeline.item_completed` method called when all file
|
||||||
|
|
@ -774,4 +771,28 @@ To enable your custom media pipeline component you must add its class import pat
|
||||||
|
|
||||||
ITEM_PIPELINES = {"myproject.pipelines.MyImagesPipeline": 300}
|
ITEM_PIPELINES = {"myproject.pipelines.MyImagesPipeline": 300}
|
||||||
|
|
||||||
|
Content-based image filtering pipeline
|
||||||
|
--------------------------------------
|
||||||
|
|
||||||
|
This example overrides ``get_images()`` to filter images using a classifier,
|
||||||
|
such as a TensorFlow_ model. Override ``is_valid_image()`` with your
|
||||||
|
classification logic:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from scrapy.pipelines.images import ImagesPipeline, ImageException
|
||||||
|
|
||||||
|
|
||||||
|
class ImageClassifierPipeline(ImagesPipeline):
|
||||||
|
def is_valid_image(self, image):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def get_images(self, response, request, info, *, item=None):
|
||||||
|
for path, image, buf in super().get_images(response, request, info, item=item):
|
||||||
|
if not self.is_valid_image(image):
|
||||||
|
raise ImageException("Image does not match criteria")
|
||||||
|
yield path, image, buf
|
||||||
|
|
||||||
|
|
||||||
.. _MD5 hash: https://en.wikipedia.org/wiki/MD5
|
.. _MD5 hash: https://en.wikipedia.org/wiki/MD5
|
||||||
|
.. _TensorFlow: https://tensorflow.org
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,10 @@ Run Scrapy from a script
|
||||||
You can use the :ref:`API <topics-api>` to run Scrapy from a script, instead of
|
You can use the :ref:`API <topics-api>` to run Scrapy from a script, instead of
|
||||||
the typical way of running Scrapy via ``scrapy crawl``.
|
the typical way of running Scrapy via ``scrapy crawl``.
|
||||||
|
|
||||||
Remember that Scrapy is built on top of the Twisted
|
Remember that Scrapy requires a Twisted reactor or (with
|
||||||
asynchronous networking library, so you need to run it inside the Twisted reactor.
|
:setting:`TWISTED_REACTOR_ENABLED` set to ``False``) an asyncio event loop, so
|
||||||
|
you need to run one of those in your script for it to work (helpers described
|
||||||
|
below can do it for you).
|
||||||
|
|
||||||
The first utility you can use to run your spiders is
|
The first utility you can use to run your spiders is
|
||||||
:class:`scrapy.crawler.AsyncCrawlerProcess` or
|
:class:`scrapy.crawler.AsyncCrawlerProcess` or
|
||||||
|
|
@ -347,10 +349,10 @@ finishes before starting the next one:
|
||||||
install_reactor("twisted.internet.asyncioreactor.AsyncioSelectorReactor")
|
install_reactor("twisted.internet.asyncioreactor.AsyncioSelectorReactor")
|
||||||
react(deferred_f_from_coro_f(crawl))
|
react(deferred_f_from_coro_f(crawl))
|
||||||
|
|
||||||
.. note:: When running multiple spiders in the same process, :ref:`reactor
|
.. note:: When running multiple spiders in the same process, :ref:`logging
|
||||||
settings <reactor-settings>` should not have a different value per spider.
|
settings <logging-settings>` and :ref:`reactor settings <reactor-settings>`
|
||||||
Also, :ref:`pre-crawler settings <pre-crawler-settings>` cannot be defined
|
should not have a different value per spider, and :ref:`pre-crawler
|
||||||
per spider.
|
settings <pre-crawler-settings>` cannot be defined per spider.
|
||||||
|
|
||||||
.. seealso:: :ref:`run-from-script`.
|
.. seealso:: :ref:`run-from-script`.
|
||||||
|
|
||||||
|
|
@ -387,6 +389,26 @@ crawl::
|
||||||
curl http://scrapy2.mycompany.com:6800/schedule.json -d project=myproject -d spider=spider1 -d part=2
|
curl http://scrapy2.mycompany.com:6800/schedule.json -d project=myproject -d spider=spider1 -d part=2
|
||||||
curl http://scrapy3.mycompany.com:6800/schedule.json -d project=myproject -d spider=spider1 -d part=3
|
curl http://scrapy3.mycompany.com:6800/schedule.json -d project=myproject -d spider=spider1 -d part=3
|
||||||
|
|
||||||
|
.. _large-project-startup:
|
||||||
|
|
||||||
|
Reducing startup time in large projects
|
||||||
|
=======================================
|
||||||
|
|
||||||
|
When running a spider with ``scrapy crawl``, Scrapy loads all modules listed in
|
||||||
|
:setting:`SPIDER_MODULES` to find the target spider. In large projects with
|
||||||
|
many spiders, this can noticeably increase startup time and memory usage.
|
||||||
|
|
||||||
|
To avoid loading every spider module, override :setting:`SPIDER_MODULES` on the
|
||||||
|
command line to point only to the module that contains the spider you want to
|
||||||
|
run:
|
||||||
|
|
||||||
|
.. code-block:: shell
|
||||||
|
|
||||||
|
scrapy crawl myspider -s SPIDER_MODULES=myproject.spiders.myspider
|
||||||
|
|
||||||
|
Because :setting:`SPIDER_MODULES` is a list setting, you can include multiple
|
||||||
|
modules by separating them with commas.
|
||||||
|
|
||||||
.. _bans:
|
.. _bans:
|
||||||
|
|
||||||
Avoiding getting banned
|
Avoiding getting banned
|
||||||
|
|
@ -409,6 +431,10 @@ Here are some tips to keep in mind when dealing with these kinds of sites:
|
||||||
* use a pool of rotating IPs. For example, the free `Tor project`_ or paid
|
* use a pool of rotating IPs. For example, the free `Tor project`_ or paid
|
||||||
services like `ProxyMesh`_. An open source alternative is `scrapoxy`_, a
|
services like `ProxyMesh`_. An open source alternative is `scrapoxy`_, a
|
||||||
super proxy that you can attach your own proxies to.
|
super proxy that you can attach your own proxies to.
|
||||||
|
* for HTTPS websites, if blocking appears related to TLS behavior, consider
|
||||||
|
adjusting the :setting:`DOWNLOAD_TLS_MIN_VERSION` and
|
||||||
|
:setting:`DOWNLOAD_TLS_MAX_VERSION` settings, since some websites may respond
|
||||||
|
differently depending on the TLS method used by the client.
|
||||||
* use a ban avoidance service, such as `Zyte API`_, which provides a `Scrapy
|
* use a ban avoidance service, such as `Zyte API`_, which provides a `Scrapy
|
||||||
plugin <https://github.com/scrapy-plugins/scrapy-zyte-api>`__ and additional
|
plugin <https://github.com/scrapy-plugins/scrapy-zyte-api>`__ and additional
|
||||||
features, like `AI web scraping <https://www.zyte.com/ai-web-scraping/>`__
|
features, like `AI web scraping <https://www.zyte.com/ai-web-scraping/>`__
|
||||||
|
|
@ -416,6 +442,14 @@ Here are some tips to keep in mind when dealing with these kinds of sites:
|
||||||
If you are still unable to prevent your bot getting banned, consider contacting
|
If you are still unable to prevent your bot getting banned, consider contacting
|
||||||
`commercial support`_.
|
`commercial support`_.
|
||||||
|
|
||||||
|
.. _static-analysis:
|
||||||
|
|
||||||
|
Static analysis
|
||||||
|
===============
|
||||||
|
|
||||||
|
Consider using :doc:`scrapy-lint <scrapy-lint:index>`, a linter for Scrapy
|
||||||
|
projects that detects common mistakes and anti-patterns.
|
||||||
|
|
||||||
.. _Tor project: https://www.torproject.org/
|
.. _Tor project: https://www.torproject.org/
|
||||||
.. _commercial support: https://www.scrapy.org/companies
|
.. _commercial support: https://www.scrapy.org/companies
|
||||||
.. _ProxyMesh: https://proxymesh.com/
|
.. _ProxyMesh: https://proxymesh.com/
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ Request objects
|
||||||
|
|
||||||
.. invisible-code-block: python
|
.. invisible-code-block: python
|
||||||
|
|
||||||
from scrapy.http import Request
|
from scrapy import Request
|
||||||
|
|
||||||
1. Using a dict:
|
1. Using a dict:
|
||||||
|
|
||||||
|
|
@ -117,6 +117,9 @@ Request objects
|
||||||
:param encoding: the encoding of this request (defaults to ``'utf-8'``).
|
:param encoding: the encoding of this request (defaults to ``'utf-8'``).
|
||||||
This encoding will be used to percent-encode the URL and to convert the
|
This encoding will be used to percent-encode the URL and to convert the
|
||||||
body to bytes (if given as a string).
|
body to bytes (if given as a string).
|
||||||
|
|
||||||
|
To disable URL percent-encoding for a request, use the
|
||||||
|
:reqmeta:`verbatim_url` request meta key.
|
||||||
:type encoding: str
|
:type encoding: str
|
||||||
|
|
||||||
:param priority: sets :attr:`priority`, defaults to ``0``.
|
:param priority: sets :attr:`priority`, defaults to ``0``.
|
||||||
|
|
@ -136,9 +139,13 @@ Request objects
|
||||||
|
|
||||||
.. attribute:: Request.url
|
.. attribute:: Request.url
|
||||||
|
|
||||||
A string containing the URL of this request. Keep in mind that this
|
A string containing the URL of this request.
|
||||||
attribute contains the escaped URL, so it can differ from the URL passed in
|
|
||||||
the ``__init__()`` method.
|
Keep in mind that this attribute contains the escaped URL, so it can
|
||||||
|
differ from the URL passed in the ``__init__()`` method.
|
||||||
|
|
||||||
|
If :reqmeta:`verbatim_url` is set to ``True``, the URL is kept as
|
||||||
|
passed to ``__init__()``.
|
||||||
|
|
||||||
This attribute is read-only. To change the URL of a Request use
|
This attribute is read-only. To change the URL of a Request use
|
||||||
:meth:`replace`.
|
:meth:`replace`.
|
||||||
|
|
@ -181,6 +188,13 @@ Request objects
|
||||||
``failure.request.cb_kwargs`` in the request's errback. For more information,
|
``failure.request.cb_kwargs`` in the request's errback. For more information,
|
||||||
see :ref:`errback-cb_kwargs`.
|
see :ref:`errback-cb_kwargs`.
|
||||||
|
|
||||||
|
.. note:: When :setting:`JOBDIR` is set, requests are serialized to disk
|
||||||
|
with :mod:`pickle` (see :ref:`request-serialization`). As a result,
|
||||||
|
the callback receives a deep copy of any object stored in
|
||||||
|
``cb_kwargs``, so mutating such an object in the callback does not
|
||||||
|
affect the original. Avoid relying on shared mutable state passed
|
||||||
|
through ``cb_kwargs`` in that case.
|
||||||
|
|
||||||
.. attribute:: Request.meta
|
.. attribute:: Request.meta
|
||||||
:value: {}
|
:value: {}
|
||||||
|
|
||||||
|
|
@ -233,7 +247,7 @@ Request objects
|
||||||
Return a new Request which is a copy of this Request. See also:
|
Return a new Request which is a copy of this Request. See also:
|
||||||
:ref:`topics-request-response-ref-request-callback-arguments`.
|
:ref:`topics-request-response-ref-request-callback-arguments`.
|
||||||
|
|
||||||
.. method:: Request.replace([url, method, headers, body, cookies, meta, flags, encoding, priority, dont_filter, callback, errback, cb_kwargs])
|
.. method:: Request.replace([url, method, headers, body, cookies, meta, flags, encoding, priority, dont_filter, callback, errback, cb_kwargs, cls])
|
||||||
|
|
||||||
Return a Request object with the same members, except for those members
|
Return a Request object with the same members, except for those members
|
||||||
given new values by whichever keyword arguments are specified. The
|
given new values by whichever keyword arguments are specified. The
|
||||||
|
|
@ -246,6 +260,78 @@ Request objects
|
||||||
.. automethod:: to_dict
|
.. automethod:: to_dict
|
||||||
|
|
||||||
|
|
||||||
|
.. _form:
|
||||||
|
|
||||||
|
Creating requests that submit HTML forms
|
||||||
|
----------------------------------------
|
||||||
|
|
||||||
|
Use :doc:`form2request <form2request:index>` to build request data from an HTML
|
||||||
|
``<form>`` element and convert it to a :class:`~scrapy.Request`.
|
||||||
|
|
||||||
|
Install it with pip:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
pip install form2request
|
||||||
|
|
||||||
|
Select the desired form with CSS or XPath, then build and convert request
|
||||||
|
data:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from form2request import form2request
|
||||||
|
|
||||||
|
|
||||||
|
def parse(self, response):
|
||||||
|
form = response.css("form#search")
|
||||||
|
request_data = form2request(form, data={"q": "scrapy"})
|
||||||
|
yield request_data.to_scrapy(callback=self.parse_results)
|
||||||
|
|
||||||
|
Use ``data`` to override field values. To drop a field from the resulting
|
||||||
|
request, set its value to ``None``.
|
||||||
|
|
||||||
|
By default, form2request simulates clicking the first submit button. To submit
|
||||||
|
without clicking any button, pass ``click=False``. To click a specific submit
|
||||||
|
button, pass its element:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
def parse(self, response):
|
||||||
|
form = response.css("form#checkout")
|
||||||
|
submit = form.css('button[name="pay"]')
|
||||||
|
request_data = form2request(form, click=submit)
|
||||||
|
|
||||||
|
.. _topics-request-response-ref-request-userlogin:
|
||||||
|
|
||||||
|
Using form2request to simulate a user login
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
It is usual for web sites to provide pre-populated form fields through ``<input
|
||||||
|
type="hidden">`` elements, such as session related data or authentication
|
||||||
|
tokens (for login pages). Build the request from the form and only override the
|
||||||
|
credentials:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
import scrapy
|
||||||
|
from form2request import form2request
|
||||||
|
|
||||||
|
|
||||||
|
class LoginSpider(scrapy.Spider):
|
||||||
|
name = "example.com"
|
||||||
|
start_urls = ["http://www.example.com/users/login.php"]
|
||||||
|
|
||||||
|
def parse(self, response):
|
||||||
|
form = response.css("form")
|
||||||
|
request_data = form2request(
|
||||||
|
form,
|
||||||
|
data={"username": "john", "password": "secret"},
|
||||||
|
)
|
||||||
|
yield request_data.to_scrapy(callback=self.after_login)
|
||||||
|
|
||||||
|
def after_login(self, response): ...
|
||||||
|
|
||||||
|
|
||||||
Other functions related to requests
|
Other functions related to requests
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
|
|
@ -469,6 +555,11 @@ in your :meth:`fingerprint` method implementation:
|
||||||
|
|
||||||
.. autofunction:: scrapy.utils.request.fingerprint
|
.. autofunction:: scrapy.utils.request.fingerprint
|
||||||
|
|
||||||
|
By default, request fingerprinting canonicalizes the request URL. If
|
||||||
|
:reqmeta:`verbatim_url` is set to ``True``, fingerprinting does not
|
||||||
|
canonicalize the URL, and the ``keep_fragments`` parameter is ignored (it is
|
||||||
|
effectively true).
|
||||||
|
|
||||||
For example, to take the value of a request header named ``X-ID`` into
|
For example, to take the value of a request header named ``X-ID`` into
|
||||||
account:
|
account:
|
||||||
|
|
||||||
|
|
@ -626,18 +717,24 @@ Those are:
|
||||||
* :reqmeta:`download_fail_on_dataloss`
|
* :reqmeta:`download_fail_on_dataloss`
|
||||||
* :reqmeta:`download_latency`
|
* :reqmeta:`download_latency`
|
||||||
* :reqmeta:`download_maxsize`
|
* :reqmeta:`download_maxsize`
|
||||||
|
* :reqmeta:`download_slot`
|
||||||
* :reqmeta:`download_warnsize`
|
* :reqmeta:`download_warnsize`
|
||||||
* :reqmeta:`download_timeout`
|
* :reqmeta:`download_timeout`
|
||||||
* ``ftp_password`` (See :setting:`FTP_PASSWORD` for more info)
|
* ``ftp_password`` (See :setting:`FTP_PASSWORD` for more info)
|
||||||
* ``ftp_user`` (See :setting:`FTP_USER` for more info)
|
* ``ftp_user`` (See :setting:`FTP_USER` for more info)
|
||||||
|
* :reqmeta:`give_up_log_level`
|
||||||
* :reqmeta:`handle_httpstatus_all`
|
* :reqmeta:`handle_httpstatus_all`
|
||||||
* :reqmeta:`handle_httpstatus_list`
|
* :reqmeta:`handle_httpstatus_list`
|
||||||
|
* :reqmeta:`http_auth_domain`
|
||||||
|
* :reqmeta:`http_pass`
|
||||||
|
* :reqmeta:`http_user`
|
||||||
* :reqmeta:`is_start_request`
|
* :reqmeta:`is_start_request`
|
||||||
* :reqmeta:`max_retry_times`
|
* :reqmeta:`max_retry_times`
|
||||||
* :reqmeta:`proxy`
|
* :reqmeta:`proxy`
|
||||||
* :reqmeta:`redirect_reasons`
|
* :reqmeta:`redirect_reasons`
|
||||||
* :reqmeta:`redirect_urls`
|
* :reqmeta:`redirect_urls`
|
||||||
* :reqmeta:`referrer_policy`
|
* :reqmeta:`referrer_policy`
|
||||||
|
* :reqmeta:`verbatim_url`
|
||||||
|
|
||||||
.. reqmeta:: bindaddress
|
.. reqmeta:: bindaddress
|
||||||
|
|
||||||
|
|
@ -705,15 +802,68 @@ download_fail_on_dataloss
|
||||||
Whether or not to fail on broken responses. See:
|
Whether or not to fail on broken responses. See:
|
||||||
:setting:`DOWNLOAD_FAIL_ON_DATALOSS`.
|
:setting:`DOWNLOAD_FAIL_ON_DATALOSS`.
|
||||||
|
|
||||||
|
.. reqmeta:: give_up_log_level
|
||||||
|
|
||||||
|
give_up_log_level
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
:ref:`Logging level <levels>` used for the message logged when a request
|
||||||
|
exceeds its retries. See :setting:`RETRY_GIVE_UP_LOG_LEVEL` for details.
|
||||||
|
|
||||||
|
.. reqmeta:: http_auth_domain
|
||||||
|
|
||||||
|
http_auth_domain
|
||||||
|
----------------
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Overrides :setting:`HTTPAUTH_DOMAIN` for this request.
|
||||||
|
|
||||||
|
.. reqmeta:: http_pass
|
||||||
|
|
||||||
|
http_pass
|
||||||
|
---------
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Overrides :setting:`HTTPAUTH_PASS` for this request.
|
||||||
|
|
||||||
|
.. reqmeta:: http_user
|
||||||
|
|
||||||
|
http_user
|
||||||
|
---------
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Overrides :setting:`HTTPAUTH_USER` for this request.
|
||||||
|
|
||||||
.. reqmeta:: max_retry_times
|
.. reqmeta:: max_retry_times
|
||||||
|
|
||||||
max_retry_times
|
max_retry_times
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
The meta key is used set retry times per request. When initialized, the
|
The meta key is used set retry times per request. When set, the
|
||||||
:reqmeta:`max_retry_times` meta key takes higher precedence over the
|
:reqmeta:`max_retry_times` meta key takes higher precedence over the
|
||||||
:setting:`RETRY_TIMES` setting.
|
:setting:`RETRY_TIMES` setting.
|
||||||
|
|
||||||
|
.. reqmeta:: verbatim_url
|
||||||
|
|
||||||
|
verbatim_url
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Set this key to ``True`` to keep the request URL as passed to
|
||||||
|
:class:`~scrapy.Request`, without URL percent-encoding.
|
||||||
|
|
||||||
|
When this key is enabled, :func:`~scrapy.utils.request.fingerprint` does not
|
||||||
|
canonicalize the request URL, so requests whose URLs differ only in
|
||||||
|
characters that would otherwise be canonicalized get different fingerprints.
|
||||||
|
|
||||||
|
In this mode, the ``keep_fragments`` parameter is ignored, and it is
|
||||||
|
effectively true.
|
||||||
|
|
||||||
.. _topics-stop-response-download:
|
.. _topics-stop-response-download:
|
||||||
|
|
||||||
|
|
@ -771,158 +921,10 @@ Request subclasses
|
||||||
Here is the list of built-in :class:`~scrapy.Request` subclasses. You can also subclass
|
Here is the list of built-in :class:`~scrapy.Request` subclasses. You can also subclass
|
||||||
it to implement your own custom functionality.
|
it to implement your own custom functionality.
|
||||||
|
|
||||||
FormRequest objects
|
FormRequest
|
||||||
-------------------
|
-----------
|
||||||
|
|
||||||
The FormRequest class extends the base :class:`~scrapy.Request` with functionality for
|
.. autoclass:: scrapy.FormRequest
|
||||||
dealing with HTML forms. It uses `lxml.html forms`_ to pre-populate form
|
|
||||||
fields with form data from :class:`Response` objects.
|
|
||||||
|
|
||||||
.. _lxml.html forms: https://lxml.de/lxmlhtml.html#forms
|
|
||||||
|
|
||||||
.. currentmodule:: None
|
|
||||||
|
|
||||||
.. class:: scrapy.FormRequest(url, [formdata, ...])
|
|
||||||
:canonical: scrapy.http.request.form.FormRequest
|
|
||||||
|
|
||||||
The :class:`~scrapy.FormRequest` class adds a new keyword parameter to the ``__init__()`` method. The
|
|
||||||
remaining arguments are the same as for the :class:`~scrapy.Request` class and are
|
|
||||||
not documented here.
|
|
||||||
|
|
||||||
:param formdata: is a dictionary (or iterable of (key, value) tuples)
|
|
||||||
containing HTML Form data which will be url-encoded and assigned to the
|
|
||||||
body of the request.
|
|
||||||
:type formdata: dict or collections.abc.Iterable
|
|
||||||
|
|
||||||
The :class:`~scrapy.FormRequest` objects support the following class method in
|
|
||||||
addition to the standard :class:`~scrapy.Request` methods:
|
|
||||||
|
|
||||||
.. classmethod:: from_response(response, [formname=None, formid=None, formnumber=0, formdata=None, formxpath=None, formcss=None, clickdata=None, dont_click=False, ...])
|
|
||||||
|
|
||||||
Returns a new :class:`~scrapy.FormRequest` object with its form field values
|
|
||||||
pre-populated with those found in the HTML ``<form>`` element contained
|
|
||||||
in the given response. For an example see
|
|
||||||
:ref:`topics-request-response-ref-request-userlogin`.
|
|
||||||
|
|
||||||
The policy is to automatically simulate a click, by default, on any form
|
|
||||||
control that looks clickable, like a ``<input type="submit">``. Even
|
|
||||||
though this is quite convenient, and often the desired behaviour,
|
|
||||||
sometimes it can cause problems which could be hard to debug. For
|
|
||||||
example, when working with forms that are filled and/or submitted using
|
|
||||||
javascript, the default :meth:`from_response` behaviour may not be the
|
|
||||||
most appropriate. To disable this behaviour you can set the
|
|
||||||
``dont_click`` argument to ``True``. Also, if you want to change the
|
|
||||||
control clicked (instead of disabling it) you can also use the
|
|
||||||
``clickdata`` argument.
|
|
||||||
|
|
||||||
.. caution:: Using this method with select elements which have leading
|
|
||||||
or trailing whitespace in the option values will not work due to a
|
|
||||||
`bug in lxml`_, which should be fixed in lxml 3.8 and above.
|
|
||||||
|
|
||||||
:param response: the response containing a HTML form which will be used
|
|
||||||
to pre-populate the form fields
|
|
||||||
:type response: :class:`~scrapy.http.Response` object
|
|
||||||
|
|
||||||
:param formname: if given, the form with name attribute set to this value will be used.
|
|
||||||
:type formname: str
|
|
||||||
|
|
||||||
:param formid: if given, the form with id attribute set to this value will be used.
|
|
||||||
:type formid: str
|
|
||||||
|
|
||||||
:param formxpath: if given, the first form that matches the xpath will be used.
|
|
||||||
:type formxpath: str
|
|
||||||
|
|
||||||
:param formcss: if given, the first form that matches the css selector will be used.
|
|
||||||
:type formcss: str
|
|
||||||
|
|
||||||
:param formnumber: the number of form to use, when the response contains
|
|
||||||
multiple forms. The first one (and also the default) is ``0``.
|
|
||||||
:type formnumber: int
|
|
||||||
|
|
||||||
:param formdata: fields to override in the form data. If a field was
|
|
||||||
already present in the response ``<form>`` element, its value is
|
|
||||||
overridden by the one passed in this parameter. If a value passed in
|
|
||||||
this parameter is ``None``, the field will not be included in the
|
|
||||||
request, even if it was present in the response ``<form>`` element.
|
|
||||||
:type formdata: dict
|
|
||||||
|
|
||||||
:param clickdata: attributes to lookup the control clicked. If it's not
|
|
||||||
given, the form data will be submitted simulating a click on the
|
|
||||||
first clickable element. In addition to html attributes, the control
|
|
||||||
can be identified by its zero-based index relative to other
|
|
||||||
submittable inputs inside the form, via the ``nr`` attribute.
|
|
||||||
:type clickdata: dict
|
|
||||||
|
|
||||||
:param dont_click: If True, the form data will be submitted without
|
|
||||||
clicking in any element.
|
|
||||||
:type dont_click: bool
|
|
||||||
|
|
||||||
The other parameters of this class method are passed directly to the
|
|
||||||
:class:`~scrapy.FormRequest` ``__init__()`` method.
|
|
||||||
|
|
||||||
.. currentmodule:: scrapy.http
|
|
||||||
|
|
||||||
Request usage examples
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
Using FormRequest to send data via HTTP POST
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
If you want to simulate a HTML Form POST in your spider and send a couple of
|
|
||||||
key-value fields, you can return a :class:`~scrapy.FormRequest` object (from your
|
|
||||||
spider) like this:
|
|
||||||
|
|
||||||
.. skip: next
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
return [
|
|
||||||
FormRequest(
|
|
||||||
url="http://www.example.com/post/action",
|
|
||||||
formdata={"name": "John Doe", "age": "27"},
|
|
||||||
callback=self.after_post,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
.. _topics-request-response-ref-request-userlogin:
|
|
||||||
|
|
||||||
Using FormRequest.from_response() to simulate a user login
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
It is usual for web sites to provide pre-populated form fields through ``<input
|
|
||||||
type="hidden">`` elements, such as session related data or authentication
|
|
||||||
tokens (for login pages). When scraping, you'll want these fields to be
|
|
||||||
automatically pre-populated and only override a couple of them, such as the
|
|
||||||
user name and password. You can use the :meth:`.FormRequest.from_response`
|
|
||||||
method for this job. Here's an example spider which uses it:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
import scrapy
|
|
||||||
|
|
||||||
|
|
||||||
def authentication_failed(response):
|
|
||||||
# TODO: Check the contents of the response and return True if it failed
|
|
||||||
# or False if it succeeded.
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class LoginSpider(scrapy.Spider):
|
|
||||||
name = "example.com"
|
|
||||||
start_urls = ["http://www.example.com/users/login.php"]
|
|
||||||
|
|
||||||
def parse(self, response):
|
|
||||||
return scrapy.FormRequest.from_response(
|
|
||||||
response,
|
|
||||||
formdata={"username": "john", "password": "secret"},
|
|
||||||
callback=self.after_login,
|
|
||||||
)
|
|
||||||
|
|
||||||
def after_login(self, response):
|
|
||||||
if authentication_failed(response):
|
|
||||||
self.logger.error("Login failed")
|
|
||||||
return
|
|
||||||
|
|
||||||
# continue scraping with authenticated session...
|
|
||||||
|
|
||||||
JsonRequest
|
JsonRequest
|
||||||
-----------
|
-----------
|
||||||
|
|
@ -998,7 +1000,7 @@ Response objects
|
||||||
:type request: scrapy.Request
|
:type request: scrapy.Request
|
||||||
|
|
||||||
:param certificate: an object representing the server's SSL certificate.
|
:param certificate: an object representing the server's SSL certificate.
|
||||||
:type certificate: twisted.internet.ssl.Certificate
|
:type certificate: typing.Any
|
||||||
|
|
||||||
:param ip_address: The IP address of the server from which the Response originated.
|
:param ip_address: The IP address of the server from which the Response originated.
|
||||||
:type ip_address: :class:`ipaddress.IPv4Address` or :class:`ipaddress.IPv6Address`
|
:type ip_address: :class:`ipaddress.IPv4Address` or :class:`ipaddress.IPv6Address`
|
||||||
|
|
@ -1023,7 +1025,7 @@ Response objects
|
||||||
|
|
||||||
A dictionary-like (:class:`scrapy.http.headers.Headers`) object which contains
|
A dictionary-like (:class:`scrapy.http.headers.Headers`) object which contains
|
||||||
the response headers. Values can be accessed using
|
the response headers. Values can be accessed using
|
||||||
:meth:`~scrapy.http.headers.Headers.get` to return the first header value with
|
:meth:`~scrapy.http.headers.Headers.get` to return the last header value with
|
||||||
the specified name or :meth:`~scrapy.http.headers.Headers.getlist` to return
|
the specified name or :meth:`~scrapy.http.headers.Headers.getlist` to return
|
||||||
all header values with the specified name. For example, this call will give you
|
all header values with the specified name. For example, this call will give you
|
||||||
all cookies in the headers::
|
all cookies in the headers::
|
||||||
|
|
@ -1084,14 +1086,14 @@ Response objects
|
||||||
.. attribute:: Response.flags
|
.. attribute:: Response.flags
|
||||||
|
|
||||||
A list that contains flags for this response. Flags are labels used for
|
A list that contains flags for this response. Flags are labels used for
|
||||||
tagging Responses. For example: ``'cached'``, ``'redirected``', etc. And
|
tagging Responses. For example: ``'cached'``, ``'redirected'``', etc. And
|
||||||
they're shown on the string representation of the Response (``__str__()``
|
they're shown on the string representation of the Response (``__str__()``
|
||||||
method) which is used by the engine for logging.
|
method) which is used by the engine for logging.
|
||||||
|
|
||||||
.. attribute:: Response.certificate
|
.. attribute:: Response.certificate
|
||||||
|
|
||||||
A :class:`twisted.internet.ssl.Certificate` object representing
|
An object representing the server's SSL certificate. Its type and
|
||||||
the server's SSL certificate.
|
contents depend on the download handler that produced the response.
|
||||||
|
|
||||||
Only populated for ``https`` responses, ``None`` otherwise.
|
Only populated for ``https`` responses, ``None`` otherwise.
|
||||||
|
|
||||||
|
|
@ -1099,8 +1101,8 @@ Response objects
|
||||||
|
|
||||||
The IP address of the server from which the Response originated.
|
The IP address of the server from which the Response originated.
|
||||||
|
|
||||||
This attribute is currently only populated by the HTTP 1.1 download
|
This attribute is currently only populated by the HTTP download
|
||||||
handler, i.e. for ``http(s)`` responses. For other handlers,
|
handlers, i.e. for ``http(s)`` responses. For other handlers,
|
||||||
:attr:`ip_address` is always ``None``.
|
:attr:`ip_address` is always ``None``.
|
||||||
|
|
||||||
.. attribute:: Response.protocol
|
.. attribute:: Response.protocol
|
||||||
|
|
@ -1118,7 +1120,7 @@ Response objects
|
||||||
|
|
||||||
Returns a new Response which is a copy of this Response.
|
Returns a new Response which is a copy of this Response.
|
||||||
|
|
||||||
.. method:: Response.replace([url, status, headers, body, request, flags, cls])
|
.. method:: Response.replace([url, status, headers, body, request, flags, certificate, ip_address, protocol, cls])
|
||||||
|
|
||||||
Returns a Response object with the same members, except for those members
|
Returns a Response object with the same members, except for those members
|
||||||
given new values by whichever keyword arguments are specified. The
|
given new values by whichever keyword arguments are specified. The
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,207 @@
|
||||||
|
.. _security:
|
||||||
|
|
||||||
|
========
|
||||||
|
Security
|
||||||
|
========
|
||||||
|
|
||||||
|
Scrapy defaults are optimized for web scraping, not for the security posture
|
||||||
|
that you might expect from software that handles untrusted input or runs in a
|
||||||
|
shared or exposed environment. Some common security practices are unnecessary
|
||||||
|
for many scraping use cases, and a few can even prevent valid ones (for
|
||||||
|
example, sites that you must scrape may use misconfigured TLS certificates or
|
||||||
|
serve content over unencrypted protocols).
|
||||||
|
|
||||||
|
This page highlights the Scrapy defaults that have security implications, so
|
||||||
|
that you can make an informed decision about whether to keep them, and explains
|
||||||
|
how to harden them along with the trade-offs involved.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
None of the options below are silver bullets. Which of them make sense
|
||||||
|
depends on your threat model: whether the URLs you crawl come from trusted
|
||||||
|
sources, whether the machine running Scrapy is exposed to a network you do
|
||||||
|
not control, whether the data you handle is sensitive, and so on.
|
||||||
|
|
||||||
|
.. _security-untrusted-responses:
|
||||||
|
|
||||||
|
Treat responses as untrusted input
|
||||||
|
==================================
|
||||||
|
|
||||||
|
Regardless of any setting, remember that response data comes from servers you
|
||||||
|
do not control, even when you trust the site you are crawling, as responses may
|
||||||
|
be tampered with in transit or the server itself may be compromised.
|
||||||
|
|
||||||
|
Never pass response data to functions that can execute code or otherwise act on
|
||||||
|
their input in an unsafe way, such as :func:`eval`, :func:`exec`, or
|
||||||
|
:func:`pickle.loads`, and be careful when writing response data to paths
|
||||||
|
derived from the response itself.
|
||||||
|
|
||||||
|
TLS connections
|
||||||
|
===============
|
||||||
|
|
||||||
|
.. _security-certificate-verification:
|
||||||
|
|
||||||
|
Certificate verification
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
By default Scrapy does **not** verify the TLS certificate of HTTPS servers, as
|
||||||
|
controlled by the :setting:`DOWNLOAD_VERIFY_CERTIFICATES` setting (default:
|
||||||
|
``False``).
|
||||||
|
|
||||||
|
This default favors reach over security: many sites that are otherwise fine to
|
||||||
|
scrape have expired, self-signed, or otherwise invalid certificates, and
|
||||||
|
verifying certificates would make requests to them fail.
|
||||||
|
|
||||||
|
If the integrity of the connection matters to you (for example, to detect
|
||||||
|
man-in-the-middle attacks), set:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
DOWNLOAD_VERIFY_CERTIFICATES = True
|
||||||
|
|
||||||
|
* **Pro:** requests to servers with invalid or untrusted certificates fail
|
||||||
|
instead of silently succeeding, protecting you from some man-in-the-middle
|
||||||
|
attacks.
|
||||||
|
|
||||||
|
* **Con:** you can no longer scrape sites with misconfigured certificates
|
||||||
|
without re-disabling verification for them.
|
||||||
|
|
||||||
|
.. _security-tls-protocols-ciphers:
|
||||||
|
|
||||||
|
Protocol versions and ciphers
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
You can restrict the TLS protocol versions that Scrapy accepts through the
|
||||||
|
:setting:`DOWNLOAD_TLS_MIN_VERSION` and :setting:`DOWNLOAD_TLS_MAX_VERSION`
|
||||||
|
settings, e.g. to reject obsolete protocol versions.
|
||||||
|
|
||||||
|
By default Scrapy uses the OpenSSL ``DEFAULT`` cipher list
|
||||||
|
(:setting:`DOWNLOADER_CLIENT_TLS_CIPHERS`), which favors compatibility and still
|
||||||
|
allows some older, weaker ciphers. Set it to ``None`` to instead use the curated
|
||||||
|
cipher list of the underlying TLS implementation (Twisted), which excludes weak
|
||||||
|
ciphers:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
DOWNLOADER_CLIENT_TLS_CIPHERS = None
|
||||||
|
|
||||||
|
* **Pro:** connections that would negotiate a weak cipher fail instead of
|
||||||
|
succeeding.
|
||||||
|
|
||||||
|
* **Con:** you can no longer connect to servers that only support the excluded
|
||||||
|
ciphers.
|
||||||
|
|
||||||
|
.. _security-unencrypted-protocols:
|
||||||
|
|
||||||
|
Unencrypted protocols
|
||||||
|
=====================
|
||||||
|
|
||||||
|
By default Scrapy enables download handlers for unencrypted protocols, namely
|
||||||
|
``http://`` and ``ftp://`` (see :setting:`DOWNLOAD_HANDLERS_BASE`). Data sent
|
||||||
|
and received over these protocols, including any credentials, travels in plain
|
||||||
|
text and can be read or modified by anyone on the network path.
|
||||||
|
|
||||||
|
If you only crawl over encrypted protocols, you can disable the unencrypted
|
||||||
|
ones so that no request can accidentally be sent unencrypted:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
DOWNLOAD_HANDLERS = {
|
||||||
|
"http": None,
|
||||||
|
"ftp": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
* **Pro:** a misconfigured or maliciously-redirected request cannot leak data
|
||||||
|
over an unencrypted connection, as such requests fail instead.
|
||||||
|
|
||||||
|
* **Con:** you can no longer crawl resources that are only available over those
|
||||||
|
protocols.
|
||||||
|
|
||||||
|
Note that disabling the ``http`` handler also prevents plain-HTTP requests that
|
||||||
|
result from following an ``http://`` redirect or link, which is often the point
|
||||||
|
of disabling it.
|
||||||
|
|
||||||
|
.. _security-local-resources:
|
||||||
|
|
||||||
|
Local and non-network resources
|
||||||
|
===============================
|
||||||
|
|
||||||
|
By default Scrapy enables download handlers for the ``file://`` and ``data:``
|
||||||
|
schemes (see :setting:`DOWNLOAD_HANDLERS_BASE`). The ``file://`` handler reads
|
||||||
|
arbitrary files from the local filesystem, limited only by the permissions of
|
||||||
|
the process running Scrapy.
|
||||||
|
|
||||||
|
This is convenient (for example, to parse a local HTML file), but it is a risk
|
||||||
|
if any of the URLs you schedule come from an untrusted source: a crafted
|
||||||
|
``file:///etc/passwd`` URL could read local files.
|
||||||
|
|
||||||
|
If you do not need them, disable these handlers:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
DOWNLOAD_HANDLERS = {
|
||||||
|
"file": None,
|
||||||
|
"data": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
* **Pro:** crawled URLs cannot be used to read local files or inline data.
|
||||||
|
|
||||||
|
* **Con:** you can no longer fetch ``file://`` or ``data:`` URLs.
|
||||||
|
|
||||||
|
More generally, if you crawl URLs from untrusted sources, consider validating
|
||||||
|
their schemes (and, where applicable, their hosts) before scheduling requests,
|
||||||
|
to avoid server-side request forgery (SSRF) and similar issues.
|
||||||
|
|
||||||
|
.. _security-telnet:
|
||||||
|
|
||||||
|
Telnet console
|
||||||
|
==============
|
||||||
|
|
||||||
|
Scrapy enables the :ref:`telnet console <topics-telnetconsole>` by default
|
||||||
|
(:setting:`TELNETCONSOLE_ENABLED`). The telnet console is a Python shell
|
||||||
|
running inside the Scrapy process, so anyone who can connect to it can run
|
||||||
|
arbitrary code in that process.
|
||||||
|
|
||||||
|
By default the console binds to ``127.0.0.1`` (:setting:`TELNETCONSOLE_HOST`)
|
||||||
|
and is protected by a username (:setting:`TELNETCONSOLE_USERNAME`, default
|
||||||
|
``scrapy``) and an automatically generated password
|
||||||
|
(:setting:`TELNETCONSOLE_PASSWORD`), so it is only reachable from the local
|
||||||
|
machine.
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
|
||||||
|
Telnet does not provide any transport-layer security, so the
|
||||||
|
username/password authentication does not protect the credentials or the
|
||||||
|
session from anyone able to observe the traffic. Never expose the telnet
|
||||||
|
console over an untrusted network by changing :setting:`TELNETCONSOLE_HOST`
|
||||||
|
to a non-local address.
|
||||||
|
|
||||||
|
If you do not use the telnet console, disable it entirely:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
TELNETCONSOLE_ENABLED = False
|
||||||
|
|
||||||
|
* **Pro:** removes a local code-execution surface and one less listening port.
|
||||||
|
|
||||||
|
* **Con:** you can no longer :ref:`inspect and control a running crawler
|
||||||
|
<topics-telnetconsole>` through it.
|
||||||
|
|
||||||
|
.. _security-credential-leakage:
|
||||||
|
|
||||||
|
Credential leakage across domains
|
||||||
|
=================================
|
||||||
|
|
||||||
|
Some Scrapy features attach credentials or other sensitive headers to requests,
|
||||||
|
and a crawl that spans multiple domains can leak them to unintended hosts:
|
||||||
|
|
||||||
|
* HTTP authentication credentials set through
|
||||||
|
:class:`~scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware` are only
|
||||||
|
sent to the domain set in :setting:`HTTPAUTH_DOMAIN`. Leave this set to the
|
||||||
|
intended domain rather than ``None`` so that credentials are not sent to
|
||||||
|
every domain you crawl.
|
||||||
|
|
||||||
|
* The ``Referer`` header may disclose the URLs you crawl to other sites. The
|
||||||
|
default :setting:`REFERRER_POLICY` already avoids sending the referrer from
|
||||||
|
HTTPS to HTTP, but you can tighten it further (for example, to
|
||||||
|
``same-origin`` or ``no-referrer``) if needed.
|
||||||
|
|
@ -308,7 +308,7 @@ Examples:
|
||||||
|
|
||||||
* ``*::text`` selects all descendant text nodes of the current selector context:
|
* ``*::text`` selects all descendant text nodes of the current selector context:
|
||||||
|
|
||||||
..skip: next
|
.. skip: next
|
||||||
.. code-block:: pycon
|
.. code-block:: pycon
|
||||||
|
|
||||||
>>> response.css("#images *::text").getall()
|
>>> response.css("#images *::text").getall()
|
||||||
|
|
@ -634,8 +634,7 @@ Example:
|
||||||
.. code-block:: pycon
|
.. code-block:: pycon
|
||||||
|
|
||||||
>>> from scrapy import Selector
|
>>> from scrapy import Selector
|
||||||
>>> sel = Selector(
|
>>> sel = Selector(text="""
|
||||||
... text="""
|
|
||||||
... <ul class="list">
|
... <ul class="list">
|
||||||
... <li>1</li>
|
... <li>1</li>
|
||||||
... <li>2</li>
|
... <li>2</li>
|
||||||
|
|
@ -645,8 +644,8 @@ Example:
|
||||||
... <li>4</li>
|
... <li>4</li>
|
||||||
... <li>5</li>
|
... <li>5</li>
|
||||||
... <li>6</li>
|
... <li>6</li>
|
||||||
... </ul>"""
|
... </ul>""")
|
||||||
... )
|
...
|
||||||
>>> xp = lambda x: sel.xpath(x).getall()
|
>>> xp = lambda x: sel.xpath(x).getall()
|
||||||
|
|
||||||
This gets all first ``<li>`` elements under whatever it is its parent:
|
This gets all first ``<li>`` elements under whatever it is its parent:
|
||||||
|
|
@ -948,11 +947,9 @@ with groups of itemscopes and corresponding itemprops:
|
||||||
>>> sel = Selector(text=doc, type="html")
|
>>> sel = Selector(text=doc, type="html")
|
||||||
>>> for scope in sel.xpath("//div[@itemscope]"):
|
>>> for scope in sel.xpath("//div[@itemscope]"):
|
||||||
... print("current scope:", scope.xpath("@itemtype").getall())
|
... print("current scope:", scope.xpath("@itemtype").getall())
|
||||||
... props = scope.xpath(
|
... props = scope.xpath("""
|
||||||
... """
|
|
||||||
... set:difference(./descendant::*/@itemprop,
|
... set:difference(./descendant::*/@itemprop,
|
||||||
... .//*[@itemscope]/*/@itemprop)"""
|
... .//*[@itemscope]/*/@itemprop)""")
|
||||||
... )
|
|
||||||
... print(f" properties: {props.getall()}")
|
... print(f" properties: {props.getall()}")
|
||||||
... print("")
|
... print("")
|
||||||
...
|
...
|
||||||
|
|
|
||||||
|
|
@ -361,6 +361,31 @@ All of these settings, except for :setting:`ASYNCIO_EVENT_LOOP`, are only used
|
||||||
when the Twisted reactor is used, i.e. when :setting:`TWISTED_REACTOR_ENABLED`
|
when the Twisted reactor is used, i.e. when :setting:`TWISTED_REACTOR_ENABLED`
|
||||||
is ``True``.
|
is ``True``.
|
||||||
|
|
||||||
|
.. _logging-settings:
|
||||||
|
|
||||||
|
Logging settings
|
||||||
|
----------------
|
||||||
|
|
||||||
|
**Logging settings** are settings that configure the global root logging
|
||||||
|
handler installed by :func:`~scrapy.utils.log.configure_logging`.
|
||||||
|
|
||||||
|
These settings can be defined from a spider. However, because only 1 root
|
||||||
|
logging handler is active per process, these settings cannot use a different
|
||||||
|
value per spider when :ref:`running multiple spiders in the same process
|
||||||
|
<run-multiple-spiders>`.
|
||||||
|
|
||||||
|
These settings are:
|
||||||
|
|
||||||
|
- :setting:`LOG_DATEFORMAT`
|
||||||
|
- :setting:`LOG_ENABLED`
|
||||||
|
- :setting:`LOG_ENCODING`
|
||||||
|
- :setting:`LOG_FILE`
|
||||||
|
- :setting:`LOG_FILE_APPEND`
|
||||||
|
- :setting:`LOG_FORMAT`
|
||||||
|
- :setting:`LOG_LEVEL`
|
||||||
|
- :setting:`LOG_SHORT_NAMES`
|
||||||
|
- :setting:`LOG_STDOUT`
|
||||||
|
|
||||||
.. _topics-settings-ref:
|
.. _topics-settings-ref:
|
||||||
|
|
||||||
Built-in settings reference
|
Built-in settings reference
|
||||||
|
|
@ -384,6 +409,36 @@ Default: ``{}``
|
||||||
A dict containing paths to the add-ons enabled in your project and their
|
A dict containing paths to the add-ons enabled in your project and their
|
||||||
priorities. For more information, see :ref:`topics-addons`.
|
priorities. For more information, see :ref:`topics-addons`.
|
||||||
|
|
||||||
|
.. setting:: ASYNCIO_EVENT_LOOP
|
||||||
|
|
||||||
|
ASYNCIO_EVENT_LOOP
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Default: ``None``
|
||||||
|
|
||||||
|
Import path of a given ``asyncio`` event loop class.
|
||||||
|
|
||||||
|
If the asyncio reactor is enabled (see :setting:`TWISTED_REACTOR`) or when
|
||||||
|
:ref:`running Scrapy without a reactor <asyncio-without-reactor>` this setting
|
||||||
|
can be used to specify the
|
||||||
|
asyncio event loop to be used with it. Set the setting to the import path of the
|
||||||
|
desired asyncio event loop class. If the setting is set to ``None`` the default asyncio
|
||||||
|
event loop will be used.
|
||||||
|
|
||||||
|
If you are installing the asyncio reactor manually using the :func:`~scrapy.utils.reactor.install_reactor`
|
||||||
|
function, you can use the ``event_loop_path`` parameter to indicate the import path of the event loop
|
||||||
|
class to be used.
|
||||||
|
|
||||||
|
Note that the event loop class must inherit from :class:`asyncio.AbstractEventLoop`.
|
||||||
|
|
||||||
|
.. caution:: Please be aware that, when using a non-default event loop
|
||||||
|
(either defined via :setting:`ASYNCIO_EVENT_LOOP` or installed with
|
||||||
|
:func:`~scrapy.utils.reactor.install_reactor`), Scrapy will call
|
||||||
|
:func:`asyncio.set_event_loop`, which will set the specified event loop
|
||||||
|
as the current loop for the current OS thread.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
.. setting:: AWS_ACCESS_KEY_ID
|
.. setting:: AWS_ACCESS_KEY_ID
|
||||||
|
|
||||||
AWS_ACCESS_KEY_ID
|
AWS_ACCESS_KEY_ID
|
||||||
|
|
@ -394,6 +449,24 @@ Default: ``None``
|
||||||
The AWS access key used by code that requires access to `Amazon Web services`_,
|
The AWS access key used by code that requires access to `Amazon Web services`_,
|
||||||
such as the :ref:`S3 feed storage backend <topics-feed-storage-s3>`.
|
such as the :ref:`S3 feed storage backend <topics-feed-storage-s3>`.
|
||||||
|
|
||||||
|
.. setting:: AWS_ENDPOINT_URL
|
||||||
|
|
||||||
|
AWS_ENDPOINT_URL
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Default: ``None``
|
||||||
|
|
||||||
|
Endpoint URL used for S3-like storage, for example Minio or s3.scality.
|
||||||
|
|
||||||
|
.. setting:: AWS_REGION_NAME
|
||||||
|
|
||||||
|
AWS_REGION_NAME
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Default: ``None``
|
||||||
|
|
||||||
|
The name of the region associated with the AWS client.
|
||||||
|
|
||||||
.. setting:: AWS_SECRET_ACCESS_KEY
|
.. setting:: AWS_SECRET_ACCESS_KEY
|
||||||
|
|
||||||
AWS_SECRET_ACCESS_KEY
|
AWS_SECRET_ACCESS_KEY
|
||||||
|
|
@ -417,15 +490,6 @@ such as the :ref:`S3 feed storage backend <topics-feed-storage-s3>`, when using
|
||||||
|
|
||||||
.. _temporary security credentials: https://docs.aws.amazon.com/IAM/latest/UserGuide/security-creds.html
|
.. _temporary security credentials: https://docs.aws.amazon.com/IAM/latest/UserGuide/security-creds.html
|
||||||
|
|
||||||
.. setting:: AWS_ENDPOINT_URL
|
|
||||||
|
|
||||||
AWS_ENDPOINT_URL
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Default: ``None``
|
|
||||||
|
|
||||||
Endpoint URL used for S3-like storage, for example Minio or s3.scality.
|
|
||||||
|
|
||||||
.. setting:: AWS_USE_SSL
|
.. setting:: AWS_USE_SSL
|
||||||
|
|
||||||
AWS_USE_SSL
|
AWS_USE_SSL
|
||||||
|
|
@ -446,41 +510,6 @@ Default: ``None``
|
||||||
Verify SSL connection between Scrapy and S3 or S3-like storage. By default
|
Verify SSL connection between Scrapy and S3 or S3-like storage. By default
|
||||||
SSL verification will occur.
|
SSL verification will occur.
|
||||||
|
|
||||||
.. setting:: AWS_REGION_NAME
|
|
||||||
|
|
||||||
AWS_REGION_NAME
|
|
||||||
---------------
|
|
||||||
|
|
||||||
Default: ``None``
|
|
||||||
|
|
||||||
The name of the region associated with the AWS client.
|
|
||||||
|
|
||||||
.. setting:: ASYNCIO_EVENT_LOOP
|
|
||||||
|
|
||||||
ASYNCIO_EVENT_LOOP
|
|
||||||
------------------
|
|
||||||
|
|
||||||
Default: ``None``
|
|
||||||
|
|
||||||
Import path of a given ``asyncio`` event loop class.
|
|
||||||
|
|
||||||
If the asyncio reactor is enabled (see :setting:`TWISTED_REACTOR`) this setting can be used to specify the
|
|
||||||
asyncio event loop to be used with it. Set the setting to the import path of the
|
|
||||||
desired asyncio event loop class. If the setting is set to ``None`` the default asyncio
|
|
||||||
event loop will be used.
|
|
||||||
|
|
||||||
If you are installing the asyncio reactor manually using the :func:`~scrapy.utils.reactor.install_reactor`
|
|
||||||
function, you can use the ``event_loop_path`` parameter to indicate the import path of the event loop
|
|
||||||
class to be used.
|
|
||||||
|
|
||||||
Note that the event loop class must inherit from :class:`asyncio.AbstractEventLoop`.
|
|
||||||
|
|
||||||
.. caution:: Please be aware that, when using a non-default event loop
|
|
||||||
(either defined via :setting:`ASYNCIO_EVENT_LOOP` or installed with
|
|
||||||
:func:`~scrapy.utils.reactor.install_reactor`), Scrapy will call
|
|
||||||
:func:`asyncio.set_event_loop`, which will set the specified event loop
|
|
||||||
as the current loop for the current OS thread.
|
|
||||||
|
|
||||||
.. setting:: BOT_NAME
|
.. setting:: BOT_NAME
|
||||||
|
|
||||||
BOT_NAME
|
BOT_NAME
|
||||||
|
|
@ -527,6 +556,8 @@ performed to any single domain.
|
||||||
See also: :ref:`topics-autothrottle` and its
|
See also: :ref:`topics-autothrottle` and its
|
||||||
:setting:`AUTOTHROTTLE_TARGET_CONCURRENCY` option.
|
:setting:`AUTOTHROTTLE_TARGET_CONCURRENCY` option.
|
||||||
|
|
||||||
|
It is possible to change this setting per domain by using
|
||||||
|
:setting:`DOWNLOAD_SLOTS`.
|
||||||
|
|
||||||
.. setting:: DEFAULT_DROPITEM_LOG_LEVEL
|
.. setting:: DEFAULT_DROPITEM_LOG_LEVEL
|
||||||
|
|
||||||
|
|
@ -566,7 +597,7 @@ When writing an item pipeline, you can force a different log level by setting
|
||||||
DEFAULT_ITEM_CLASS
|
DEFAULT_ITEM_CLASS
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
Default: ``'scrapy.Item'``
|
Default: ``'scrapy.item.Item'``
|
||||||
|
|
||||||
The default class that will be used for instantiating items in the :ref:`the
|
The default class that will be used for instantiating items in the :ref:`the
|
||||||
Scrapy shell <topics-shell>`.
|
Scrapy shell <topics-shell>`.
|
||||||
|
|
@ -660,7 +691,9 @@ Whether to enable DNS in-memory cache.
|
||||||
:class:`~scrapy.resolver.CachingThreadedResolver` and
|
:class:`~scrapy.resolver.CachingThreadedResolver` and
|
||||||
:class:`~scrapy.resolver.CachingHostnameResolver`. It has no effect when
|
:class:`~scrapy.resolver.CachingHostnameResolver`. It has no effect when
|
||||||
:setting:`TWISTED_REACTOR_ENABLED` is ``False``, and may have no effect
|
:setting:`TWISTED_REACTOR_ENABLED` is ``False``, and may have no effect
|
||||||
either when :setting:`DNS_RESOLVER` is set to a different resolver.
|
either when :setting:`TWISTED_DNS_RESOLVER` is set to a different resolver.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
.. setting:: DNSCACHE_SIZE
|
.. setting:: DNSCACHE_SIZE
|
||||||
|
|
||||||
|
|
@ -671,22 +704,7 @@ Default: ``10000``
|
||||||
|
|
||||||
DNS in-memory cache size, see :setting:`DNSCACHE_ENABLED`.
|
DNS in-memory cache size, see :setting:`DNSCACHE_ENABLED`.
|
||||||
|
|
||||||
.. setting:: TWISTED_DNS_RESOLVER
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
TWISTED_DNS_RESOLVER
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
Default: ``'scrapy.resolver.CachingThreadedResolver'``
|
|
||||||
|
|
||||||
The class to be used by Twisted to resolve DNS names. The default
|
|
||||||
``scrapy.resolver.CachingThreadedResolver`` supports specifying a timeout for
|
|
||||||
DNS requests via the :setting:`DNS_TIMEOUT` setting, but works only with IPv4
|
|
||||||
addresses. Scrapy provides an alternative resolver,
|
|
||||||
``scrapy.resolver.CachingHostnameResolver``, which supports IPv4/IPv6 addresses but does not
|
|
||||||
take the :setting:`DNS_TIMEOUT` setting into account.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
This setting has no effect when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
|
||||||
|
|
||||||
.. setting:: DNS_TIMEOUT
|
.. setting:: DNS_TIMEOUT
|
||||||
|
|
||||||
|
|
@ -701,7 +719,9 @@ Timeout for processing of DNS queries in seconds. Float is supported.
|
||||||
This setting is only used by
|
This setting is only used by
|
||||||
:class:`~scrapy.resolver.CachingThreadedResolver`. It has no effect when
|
:class:`~scrapy.resolver.CachingThreadedResolver`. It has no effect when
|
||||||
:setting:`TWISTED_REACTOR_ENABLED` is ``False``, and may have no effect
|
:setting:`TWISTED_REACTOR_ENABLED` is ``False``, and may have no effect
|
||||||
either when :setting:`DNS_RESOLVER` is set to a different resolver.
|
either when :setting:`TWISTED_DNS_RESOLVER` is set to a different resolver.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
.. setting:: DOWNLOADER
|
.. setting:: DOWNLOADER
|
||||||
|
|
||||||
|
|
@ -728,41 +748,74 @@ necessary to access certain HTTPS websites: for example, you may need to use
|
||||||
``'DEFAULT:!DH'`` for a website with weak DH parameters or enable a
|
``'DEFAULT:!DH'`` for a website with weak DH parameters or enable a
|
||||||
specific cipher that is not included in ``DEFAULT`` if a website requires it.
|
specific cipher that is not included in ``DEFAULT`` if a website requires it.
|
||||||
|
|
||||||
|
Set this setting to ``None`` to use the default ciphers of the underlying TLS
|
||||||
|
implementation.
|
||||||
|
|
||||||
|
.. versionchanged:: 2.17.0
|
||||||
|
Added support for setting this to ``None``.
|
||||||
|
|
||||||
.. _OpenSSL cipher list format: https://docs.openssl.org/master/man1/openssl-ciphers/#cipher-list-format
|
.. _OpenSSL cipher list format: https://docs.openssl.org/master/man1/openssl-ciphers/#cipher-list-format
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
Handling of this setting needs to be implemented inside the :ref:`download
|
Handling of this setting needs to be implemented inside the :ref:`download
|
||||||
handler <topics-download-handlers>`, so it's not guaranteed to be supported
|
handler <topics-download-handlers>`, so it's not guaranteed to be supported
|
||||||
by all 3rd-party handlers. It's currently unsupported by
|
by all 3rd-party handlers.
|
||||||
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`.
|
|
||||||
|
|
||||||
.. setting:: DOWNLOADER_CLIENT_TLS_METHOD
|
.. seealso:: :ref:`security-tls-protocols-ciphers`
|
||||||
|
|
||||||
DOWNLOADER_CLIENT_TLS_METHOD
|
.. setting:: DOWNLOAD_TLS_MAX_VERSION
|
||||||
----------------------------
|
|
||||||
|
|
||||||
Default: ``'TLS'``
|
DOWNLOAD_TLS_MAX_VERSION
|
||||||
|
------------------------
|
||||||
|
|
||||||
Use this setting to customize the TLS/SSL method used by the HTTPS download
|
.. versionadded:: 2.17.0
|
||||||
handler.
|
|
||||||
|
|
||||||
This setting must be one of these string values:
|
Default: ``None``
|
||||||
|
|
||||||
- ``'TLS'``: maps to OpenSSL's ``TLS_method()`` (a.k.a ``SSLv23_method()``),
|
Use this setting to change the maximum version of the TLS protocol allowed to
|
||||||
which allows protocol negotiation, starting from the highest supported
|
be used by Scrapy.
|
||||||
by the platform; **default, recommended**
|
|
||||||
- ``'TLSv1.0'``: this value forces HTTPS connections to use TLS version 1.0 ;
|
This setting must be either ``None``, in which case it doesn't affect the
|
||||||
set this if you want the behavior of Scrapy<1.1
|
version selection, or one of these string values:
|
||||||
- ``'TLSv1.1'``: forces TLS version 1.1
|
|
||||||
- ``'TLSv1.2'``: forces TLS version 1.2
|
- ``'TLSv1.0'``
|
||||||
|
- ``'TLSv1.1'``
|
||||||
|
- ``'TLSv1.2'``
|
||||||
|
- ``'TLSv1.3'``
|
||||||
|
|
||||||
|
The range of allowed TLS versions advertised by Scrapy when making TLS
|
||||||
|
connections will depend on the TLS implementation defaults and the values of
|
||||||
|
:setting:`DOWNLOAD_TLS_MIN_VERSION` and :setting:`DOWNLOAD_TLS_MAX_VERSION`.
|
||||||
|
It's possible to re-enable versions that are supported by the TLS
|
||||||
|
implementation but disabled by default by adjusting these settings, but it's
|
||||||
|
impossible to enable unsupported ones, such as any versions below 1.2 in many
|
||||||
|
modern environments.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
Handling of this setting needs to be implemented inside the :ref:`download
|
Handling of this setting needs to be implemented inside the :ref:`download
|
||||||
handler <topics-download-handlers>`, so it's not guaranteed to be supported
|
handler <topics-download-handlers>`, so it's not guaranteed to be supported
|
||||||
by all 3rd-party handlers. It's currently unsupported by
|
by all 3rd-party handlers. Additionally, the set of supported TLS versions
|
||||||
:class:`~scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler`.
|
depends on the TLS implementation being used by the handler.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-tls-protocols-ciphers`
|
||||||
|
|
||||||
|
.. setting:: DOWNLOAD_TLS_MIN_VERSION
|
||||||
|
|
||||||
|
DOWNLOAD_TLS_MIN_VERSION
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
|
||||||
|
Default: ``None``
|
||||||
|
|
||||||
|
Use this setting to change the minimum version of the TLS protocol allowed to
|
||||||
|
be used by Scrapy.
|
||||||
|
|
||||||
|
See :setting:`DOWNLOAD_TLS_MAX_VERSION` for the details and limitations.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-tls-protocols-ciphers`
|
||||||
|
|
||||||
.. setting:: DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING
|
.. setting:: DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING
|
||||||
|
|
||||||
|
|
@ -809,7 +862,6 @@ Default:
|
||||||
"scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware": 400,
|
"scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware": 400,
|
||||||
"scrapy.downloadermiddlewares.useragent.UserAgentMiddleware": 500,
|
"scrapy.downloadermiddlewares.useragent.UserAgentMiddleware": 500,
|
||||||
"scrapy.downloadermiddlewares.retry.RetryMiddleware": 550,
|
"scrapy.downloadermiddlewares.retry.RetryMiddleware": 550,
|
||||||
"scrapy.downloadermiddlewares.ajaxcrawl.AjaxCrawlMiddleware": 560,
|
|
||||||
"scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware": 580,
|
"scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware": 580,
|
||||||
"scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware": 590,
|
"scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware": 590,
|
||||||
"scrapy.downloadermiddlewares.redirect.RedirectMiddleware": 600,
|
"scrapy.downloadermiddlewares.redirect.RedirectMiddleware": 600,
|
||||||
|
|
@ -869,9 +921,8 @@ desired.
|
||||||
|
|
||||||
This delay can be set per spider using :attr:`download_delay` spider attribute.
|
This delay can be set per spider using :attr:`download_delay` spider attribute.
|
||||||
|
|
||||||
It is also possible to change this setting per domain, although it requires
|
It is possible to change this setting per domain by using
|
||||||
non-trivial code. See the implementation of the :ref:`AutoThrottle
|
:setting:`DOWNLOAD_SLOTS`.
|
||||||
<topics-autothrottle>` extension for an example.
|
|
||||||
|
|
||||||
.. setting:: DOWNLOAD_BIND_ADDRESS
|
.. setting:: DOWNLOAD_BIND_ADDRESS
|
||||||
|
|
||||||
|
|
@ -925,6 +976,9 @@ enabled in your project.
|
||||||
|
|
||||||
See :setting:`DOWNLOAD_HANDLERS_BASE` for example format.
|
See :setting:`DOWNLOAD_HANDLERS_BASE` for example format.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-unencrypted-protocols` and
|
||||||
|
:ref:`security-local-resources`
|
||||||
|
|
||||||
.. setting:: DOWNLOAD_HANDLERS_BASE
|
.. setting:: DOWNLOAD_HANDLERS_BASE
|
||||||
|
|
||||||
DOWNLOAD_HANDLERS_BASE
|
DOWNLOAD_HANDLERS_BASE
|
||||||
|
|
@ -972,6 +1026,9 @@ handler (without replacement), place this in your ``settings.py``:
|
||||||
"ftp": None,
|
"ftp": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-unencrypted-protocols` and
|
||||||
|
:ref:`security-local-resources`
|
||||||
|
|
||||||
|
|
||||||
.. setting:: DOWNLOAD_SLOTS
|
.. setting:: DOWNLOAD_SLOTS
|
||||||
|
|
||||||
|
|
@ -1130,6 +1187,8 @@ when making a request and abort the request if the verification fails.
|
||||||
certificate problems are logged when this setting is set to ``False``)
|
certificate problems are logged when this setting is set to ``False``)
|
||||||
depends on its implementation.
|
depends on its implementation.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-certificate-verification`
|
||||||
|
|
||||||
.. setting:: DUPEFILTER_CLASS
|
.. setting:: DUPEFILTER_CLASS
|
||||||
|
|
||||||
DUPEFILTER_CLASS
|
DUPEFILTER_CLASS
|
||||||
|
|
@ -1255,6 +1314,7 @@ Default:
|
||||||
|
|
||||||
{
|
{
|
||||||
"scrapy.extensions.corestats.CoreStats": 0,
|
"scrapy.extensions.corestats.CoreStats": 0,
|
||||||
|
"scrapy.extensions.logcount.LogCount": 0,
|
||||||
"scrapy.extensions.telnet.TelnetConsole": 0,
|
"scrapy.extensions.telnet.TelnetConsole": 0,
|
||||||
"scrapy.extensions.memusage.MemoryUsage": 0,
|
"scrapy.extensions.memusage.MemoryUsage": 0,
|
||||||
"scrapy.extensions.memdebug.MemoryDebugger": 0,
|
"scrapy.extensions.memdebug.MemoryDebugger": 0,
|
||||||
|
|
@ -1277,6 +1337,8 @@ and the :ref:`list of available extensions <topics-extensions-ref>`.
|
||||||
FEED_TEMPDIR
|
FEED_TEMPDIR
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
Default: ``None``
|
||||||
|
|
||||||
The Feed Temp dir allows you to set a custom folder to save crawler
|
The Feed Temp dir allows you to set a custom folder to save crawler
|
||||||
temporary files before uploading with :ref:`FTP feed storage <topics-feed-storage-ftp>` and
|
temporary files before uploading with :ref:`FTP feed storage <topics-feed-storage-ftp>` and
|
||||||
:ref:`Amazon S3 <topics-feed-storage-s3>`.
|
:ref:`Amazon S3 <topics-feed-storage-s3>`.
|
||||||
|
|
@ -1286,6 +1348,8 @@ temporary files before uploading with :ref:`FTP feed storage <topics-feed-storag
|
||||||
FEED_STORAGE_GCS_ACL
|
FEED_STORAGE_GCS_ACL
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
Default: ``""``
|
||||||
|
|
||||||
The Access Control List (ACL) used when storing items to :ref:`Google Cloud Storage <topics-feed-storage-gcs>`.
|
The Access Control List (ACL) used when storing items to :ref:`Google Cloud Storage <topics-feed-storage-gcs>`.
|
||||||
For more information on how to set this value, please refer to the column *JSON API* in `Google Cloud documentation <https://docs.cloud.google.com/storage/docs/access-control/lists>`_.
|
For more information on how to set this value, please refer to the column *JSON API* in `Google Cloud documentation <https://docs.cloud.google.com/storage/docs/access-control/lists>`_.
|
||||||
|
|
||||||
|
|
@ -1297,14 +1361,19 @@ FORCE_CRAWLER_PROCESS
|
||||||
Default: ``False``
|
Default: ``False``
|
||||||
|
|
||||||
If ``False``, :ref:`Scrapy commands that need a CrawlerProcess
|
If ``False``, :ref:`Scrapy commands that need a CrawlerProcess
|
||||||
<topics-commands-crawlerprocess>` will decide between using
|
<topics-commands-crawlerprocess>`, when :setting:`TWISTED_REACTOR_ENABLED`
|
||||||
|
is set to ``True``, will decide between using
|
||||||
:class:`scrapy.crawler.AsyncCrawlerProcess` and
|
:class:`scrapy.crawler.AsyncCrawlerProcess` and
|
||||||
:class:`scrapy.crawler.CrawlerProcess` based on the value of the
|
:class:`scrapy.crawler.CrawlerProcess` based on the value of the
|
||||||
:setting:`TWISTED_REACTOR` setting, but ignoring its value in :ref:`per-spider
|
:setting:`TWISTED_REACTOR` setting, but ignoring its value in :ref:`per-spider
|
||||||
settings <spider-settings>`.
|
settings <spider-settings>`.
|
||||||
|
|
||||||
If ``True``, these commands will always use
|
If ``True``, these commands will always use
|
||||||
:class:`~scrapy.crawler.CrawlerProcess`.
|
:class:`~scrapy.crawler.CrawlerProcess` when :setting:`TWISTED_REACTOR_ENABLED`
|
||||||
|
is set to ``True``.
|
||||||
|
|
||||||
|
When :setting:`TWISTED_REACTOR_ENABLED` is set to ``False``,
|
||||||
|
:class:`~scrapy.crawler.AsyncCrawlerProcess` will be used in all cases.
|
||||||
|
|
||||||
Set this to ``True`` if you want to set :setting:`TWISTED_REACTOR` to a
|
Set this to ``True`` if you want to set :setting:`TWISTED_REACTOR` to a
|
||||||
non-default value in :ref:`per-spider settings <spider-settings>`.
|
non-default value in :ref:`per-spider settings <spider-settings>`.
|
||||||
|
|
@ -1426,6 +1495,8 @@ Default: ``True``
|
||||||
|
|
||||||
Whether to enable logging.
|
Whether to enable logging.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_ENCODING
|
.. setting:: LOG_ENCODING
|
||||||
|
|
||||||
LOG_ENCODING
|
LOG_ENCODING
|
||||||
|
|
@ -1435,6 +1506,8 @@ Default: ``'utf-8'``
|
||||||
|
|
||||||
The encoding to use for logging.
|
The encoding to use for logging.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_FILE
|
.. setting:: LOG_FILE
|
||||||
|
|
||||||
LOG_FILE
|
LOG_FILE
|
||||||
|
|
@ -1444,6 +1517,8 @@ Default: ``None``
|
||||||
|
|
||||||
File name to use for logging output. If ``None``, standard error will be used.
|
File name to use for logging output. If ``None``, standard error will be used.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_FILE_APPEND
|
.. setting:: LOG_FILE_APPEND
|
||||||
|
|
||||||
LOG_FILE_APPEND
|
LOG_FILE_APPEND
|
||||||
|
|
@ -1454,6 +1529,8 @@ Default: ``True``
|
||||||
If ``False``, the log file specified with :setting:`LOG_FILE` will be
|
If ``False``, the log file specified with :setting:`LOG_FILE` will be
|
||||||
overwritten (discarding the output from previous runs, if any).
|
overwritten (discarding the output from previous runs, if any).
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_FORMAT
|
.. setting:: LOG_FORMAT
|
||||||
|
|
||||||
LOG_FORMAT
|
LOG_FORMAT
|
||||||
|
|
@ -1465,6 +1542,8 @@ String for formatting log messages. Refer to the
|
||||||
:ref:`Python logging documentation <logrecord-attributes>` for the whole
|
:ref:`Python logging documentation <logrecord-attributes>` for the whole
|
||||||
list of available placeholders.
|
list of available placeholders.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_DATEFORMAT
|
.. setting:: LOG_DATEFORMAT
|
||||||
|
|
||||||
LOG_DATEFORMAT
|
LOG_DATEFORMAT
|
||||||
|
|
@ -1477,6 +1556,8 @@ in :setting:`LOG_FORMAT`. Refer to the
|
||||||
:ref:`Python datetime documentation <strftime-strptime-behavior>` for the
|
:ref:`Python datetime documentation <strftime-strptime-behavior>` for the
|
||||||
whole list of available directives.
|
whole list of available directives.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_FORMATTER
|
.. setting:: LOG_FORMATTER
|
||||||
|
|
||||||
LOG_FORMATTER
|
LOG_FORMATTER
|
||||||
|
|
@ -1496,6 +1577,8 @@ Default: ``'DEBUG'``
|
||||||
Minimum level to log. Available levels are: CRITICAL, ERROR, WARNING,
|
Minimum level to log. Available levels are: CRITICAL, ERROR, WARNING,
|
||||||
INFO, DEBUG. For more info see :ref:`topics-logging`.
|
INFO, DEBUG. For more info see :ref:`topics-logging`.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_STDOUT
|
.. setting:: LOG_STDOUT
|
||||||
|
|
||||||
LOG_STDOUT
|
LOG_STDOUT
|
||||||
|
|
@ -1507,6 +1590,8 @@ If ``True``, all standard output (and error) of your process will be redirected
|
||||||
to the log. For example if you ``print('hello')`` it will appear in the Scrapy
|
to the log. For example if you ``print('hello')`` it will appear in the Scrapy
|
||||||
log.
|
log.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_SHORT_NAMES
|
.. setting:: LOG_SHORT_NAMES
|
||||||
|
|
||||||
LOG_SHORT_NAMES
|
LOG_SHORT_NAMES
|
||||||
|
|
@ -1517,6 +1602,8 @@ Default: ``False``
|
||||||
If ``True``, the logs will just contain the root path. If it is set to ``False``
|
If ``True``, the logs will just contain the root path. If it is set to ``False``
|
||||||
then it displays the component responsible for the log output
|
then it displays the component responsible for the log output
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`logging setting <logging-settings>`.
|
||||||
|
|
||||||
.. setting:: LOG_VERSIONS
|
.. setting:: LOG_VERSIONS
|
||||||
|
|
||||||
LOG_VERSIONS
|
LOG_VERSIONS
|
||||||
|
|
@ -1536,6 +1623,8 @@ The following special items are also supported:
|
||||||
|
|
||||||
- ``Python``
|
- ``Python``
|
||||||
|
|
||||||
|
- ``pyOpenSSL``
|
||||||
|
|
||||||
.. setting:: LOGSTATS_INTERVAL
|
.. setting:: LOGSTATS_INTERVAL
|
||||||
|
|
||||||
LOGSTATS_INTERVAL
|
LOGSTATS_INTERVAL
|
||||||
|
|
@ -1663,7 +1752,10 @@ significant similarities in the time between their requests.
|
||||||
|
|
||||||
The randomization policy is the same used by `wget`_ ``--random-wait`` option.
|
The randomization policy is the same used by `wget`_ ``--random-wait`` option.
|
||||||
|
|
||||||
If :setting:`DOWNLOAD_DELAY` is zero (default) this option has no effect.
|
If :setting:`DOWNLOAD_DELAY` is zero this option has no effect.
|
||||||
|
|
||||||
|
It is possible to change this setting per domain by using
|
||||||
|
:setting:`DOWNLOAD_SLOTS`.
|
||||||
|
|
||||||
.. _wget: https://www.gnu.org/software/wget/manual/wget.html
|
.. _wget: https://www.gnu.org/software/wget/manual/wget.html
|
||||||
|
|
||||||
|
|
@ -1679,6 +1771,8 @@ multi-purpose thread pool used by various Scrapy components. Threaded
|
||||||
DNS Resolver, BlockingFeedStorage, S3FilesStore just to name a few. Increase
|
DNS Resolver, BlockingFeedStorage, S3FilesStore just to name a few. Increase
|
||||||
this value if you're experiencing problems with insufficient blocking IO.
|
this value if you're experiencing problems with insufficient blocking IO.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
.. setting:: REDIRECT_PRIORITY_ADJUST
|
.. setting:: REDIRECT_PRIORITY_ADJUST
|
||||||
|
|
||||||
REDIRECT_PRIORITY_ADJUST
|
REDIRECT_PRIORITY_ADJUST
|
||||||
|
|
@ -1722,7 +1816,7 @@ The parser backend to use for parsing ``robots.txt`` files. For more information
|
||||||
.. setting:: ROBOTSTXT_USER_AGENT
|
.. setting:: ROBOTSTXT_USER_AGENT
|
||||||
|
|
||||||
ROBOTSTXT_USER_AGENT
|
ROBOTSTXT_USER_AGENT
|
||||||
^^^^^^^^^^^^^^^^^^^^
|
--------------------
|
||||||
|
|
||||||
Default: ``None``
|
Default: ``None``
|
||||||
|
|
||||||
|
|
@ -1877,6 +1971,8 @@ Default:
|
||||||
|
|
||||||
{
|
{
|
||||||
"scrapy.contracts.default.UrlContract": 1,
|
"scrapy.contracts.default.UrlContract": 1,
|
||||||
|
"scrapy.contracts.default.CallbackKeywordArgumentsContract": 1,
|
||||||
|
"scrapy.contracts.default.MetadataContract": 1,
|
||||||
"scrapy.contracts.default.ReturnsContract": 2,
|
"scrapy.contracts.default.ReturnsContract": 2,
|
||||||
"scrapy.contracts.default.ScrapesContract": 3,
|
"scrapy.contracts.default.ScrapesContract": 3,
|
||||||
}
|
}
|
||||||
|
|
@ -1905,6 +2001,8 @@ Default: ``'scrapy.spiderloader.SpiderLoader'``
|
||||||
The class that will be used for loading spiders, which must implement the
|
The class that will be used for loading spiders, which must implement the
|
||||||
:ref:`topics-api-spiderloader`.
|
:ref:`topics-api-spiderloader`.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`pre-crawler setting <pre-crawler-settings>`.
|
||||||
|
|
||||||
.. setting:: SPIDER_LOADER_WARN_ONLY
|
.. setting:: SPIDER_LOADER_WARN_ONLY
|
||||||
|
|
||||||
SPIDER_LOADER_WARN_ONLY
|
SPIDER_LOADER_WARN_ONLY
|
||||||
|
|
@ -1917,6 +2015,8 @@ it will fail loudly if there is any ``ImportError`` or ``SyntaxError`` exception
|
||||||
But you can choose to silence this exception and turn it into a simple
|
But you can choose to silence this exception and turn it into a simple
|
||||||
warning by setting ``SPIDER_LOADER_WARN_ONLY = True``.
|
warning by setting ``SPIDER_LOADER_WARN_ONLY = True``.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`pre-crawler setting <pre-crawler-settings>`.
|
||||||
|
|
||||||
.. setting:: SPIDER_MIDDLEWARES
|
.. setting:: SPIDER_MIDDLEWARES
|
||||||
|
|
||||||
SPIDER_MIDDLEWARES
|
SPIDER_MIDDLEWARES
|
||||||
|
|
@ -1937,6 +2037,7 @@ Default:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
{
|
{
|
||||||
|
"scrapy.spidermiddlewares.start.StartSpiderMiddleware": 25,
|
||||||
"scrapy.spidermiddlewares.httperror.HttpErrorMiddleware": 50,
|
"scrapy.spidermiddlewares.httperror.HttpErrorMiddleware": 50,
|
||||||
"scrapy.spidermiddlewares.referer.RefererMiddleware": 700,
|
"scrapy.spidermiddlewares.referer.RefererMiddleware": 700,
|
||||||
"scrapy.spidermiddlewares.urllength.UrlLengthMiddleware": 800,
|
"scrapy.spidermiddlewares.urllength.UrlLengthMiddleware": 800,
|
||||||
|
|
@ -1962,6 +2063,8 @@ Example:
|
||||||
|
|
||||||
SPIDER_MODULES = ["mybot.spiders_prod", "mybot.spiders_dev"]
|
SPIDER_MODULES = ["mybot.spiders_prod", "mybot.spiders_dev"]
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`pre-crawler setting <pre-crawler-settings>`.
|
||||||
|
|
||||||
.. setting:: STATS_CLASS
|
.. setting:: STATS_CLASS
|
||||||
|
|
||||||
STATS_CLASS
|
STATS_CLASS
|
||||||
|
|
@ -1994,6 +2097,8 @@ Default: ``True`` (``False`` when :setting:`TWISTED_REACTOR_ENABLED` is ``False`
|
||||||
A boolean which specifies if the :ref:`telnet console <topics-telnetconsole>`
|
A boolean which specifies if the :ref:`telnet console <topics-telnetconsole>`
|
||||||
will be enabled (provided its extension is also enabled).
|
will be enabled (provided its extension is also enabled).
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-telnet`
|
||||||
|
|
||||||
.. setting:: TEMPLATES_DIR
|
.. setting:: TEMPLATES_DIR
|
||||||
|
|
||||||
TEMPLATES_DIR
|
TEMPLATES_DIR
|
||||||
|
|
@ -2008,6 +2113,25 @@ command.
|
||||||
The project name must not conflict with the name of custom files or directories
|
The project name must not conflict with the name of custom files or directories
|
||||||
in the ``project`` subdirectory.
|
in the ``project`` subdirectory.
|
||||||
|
|
||||||
|
.. setting:: TWISTED_DNS_RESOLVER
|
||||||
|
|
||||||
|
TWISTED_DNS_RESOLVER
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
Default: ``'scrapy.resolver.CachingThreadedResolver'``
|
||||||
|
|
||||||
|
The class to be used by Twisted to resolve DNS names. The default
|
||||||
|
``scrapy.resolver.CachingThreadedResolver`` supports specifying a timeout for
|
||||||
|
DNS requests via the :setting:`DNS_TIMEOUT` setting, but works only with IPv4
|
||||||
|
addresses. Scrapy provides an alternative resolver,
|
||||||
|
``scrapy.resolver.CachingHostnameResolver``, which supports IPv4/IPv6 addresses but does not
|
||||||
|
take the :setting:`DNS_TIMEOUT` setting into account.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
This setting has no effect when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
.. setting:: TWISTED_REACTOR_ENABLED
|
.. setting:: TWISTED_REACTOR_ENABLED
|
||||||
|
|
||||||
TWISTED_REACTOR_ENABLED
|
TWISTED_REACTOR_ENABLED
|
||||||
|
|
@ -2033,7 +2157,7 @@ stopped) will not apply. This mode is currently experimental and may not be
|
||||||
suitable for production use. It may also not be supported by 3rd-party code.
|
suitable for production use. It may also not be supported by 3rd-party code.
|
||||||
See :ref:`asyncio-without-reactor` for more information about this mode.
|
See :ref:`asyncio-without-reactor` for more information about this mode.
|
||||||
|
|
||||||
.. note:: This setting can't be set :ref:`per-spider <spider-settings>`.
|
.. note:: This is a :ref:`pre-crawler setting <pre-crawler-settings>`.
|
||||||
|
|
||||||
.. versionadded:: 2.15.0
|
.. versionadded:: 2.15.0
|
||||||
|
|
||||||
|
|
@ -2046,6 +2170,9 @@ Default: ``"twisted.internet.asyncioreactor.AsyncioSelectorReactor"``
|
||||||
|
|
||||||
Import path of a given :mod:`~twisted.internet.reactor`.
|
Import path of a given :mod:`~twisted.internet.reactor`.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
This setting has no effect when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
||||||
|
|
||||||
Scrapy will install this reactor if no other reactor is installed yet, such as
|
Scrapy will install this reactor if no other reactor is installed yet, such as
|
||||||
when the ``scrapy`` CLI program is invoked or when using the
|
when the ``scrapy`` CLI program is invoked or when using the
|
||||||
:class:`~scrapy.crawler.AsyncCrawlerProcess` class or the
|
:class:`~scrapy.crawler.AsyncCrawlerProcess` class or the
|
||||||
|
|
@ -2140,6 +2267,7 @@ current platform.
|
||||||
|
|
||||||
For additional information, see :doc:`core/howto/choosing-reactor`.
|
For additional information, see :doc:`core/howto/choosing-reactor`.
|
||||||
|
|
||||||
|
.. note:: This is a :ref:`reactor setting <reactor-settings>`.
|
||||||
|
|
||||||
.. setting:: URLLENGTH_LIMIT
|
.. setting:: URLLENGTH_LIMIT
|
||||||
|
|
||||||
|
|
@ -2148,7 +2276,7 @@ URLLENGTH_LIMIT
|
||||||
|
|
||||||
Default: ``2083``
|
Default: ``2083``
|
||||||
|
|
||||||
Scope: ``spidermiddlewares.urllength``
|
Scope: ``scrapy.spidermiddlewares.urllength``
|
||||||
|
|
||||||
The maximum URL length to allow for crawled URLs.
|
The maximum URL length to allow for crawled URLs.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,8 @@ Let's take an example using :ref:`coroutines <topics-coroutines>`:
|
||||||
.. skip: next
|
.. skip: next
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
import scrapy
|
import scrapy
|
||||||
import treq
|
import treq
|
||||||
|
|
||||||
|
|
@ -452,7 +454,7 @@ bytes_received
|
||||||
.. signal:: bytes_received
|
.. signal:: bytes_received
|
||||||
.. function:: bytes_received(data, request, spider)
|
.. function:: bytes_received(data, request, spider)
|
||||||
|
|
||||||
Sent by the HTTP 1.1 and S3 download handlers when a group of bytes is
|
Sent by some download handlers when a group of bytes is
|
||||||
received for a specific request. This signal might be fired multiple
|
received for a specific request. This signal might be fired multiple
|
||||||
times for the same request, with partial data each time. For instance,
|
times for the same request, with partial data each time. For instance,
|
||||||
a possible scenario for a 25 kb response would be two signals fired
|
a possible scenario for a 25 kb response would be two signals fired
|
||||||
|
|
@ -480,7 +482,7 @@ headers_received
|
||||||
.. signal:: headers_received
|
.. signal:: headers_received
|
||||||
.. function:: headers_received(headers, body_length, request, spider)
|
.. function:: headers_received(headers, body_length, request, spider)
|
||||||
|
|
||||||
Sent by the HTTP 1.1 and S3 download handlers when the response headers are
|
Sent by some download handlers when the response headers are
|
||||||
available for a given request, before downloading any additional content.
|
available for a given request, before downloading any additional content.
|
||||||
|
|
||||||
Handlers for this signal can stop the download of a response while it
|
Handlers for this signal can stop the download of a response while it
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ previous (or subsequent) middleware being applied.
|
||||||
If you want to disable a builtin middleware (the ones defined in
|
If you want to disable a builtin middleware (the ones defined in
|
||||||
:setting:`SPIDER_MIDDLEWARES_BASE`, and enabled by default) you must define it
|
:setting:`SPIDER_MIDDLEWARES_BASE`, and enabled by default) you must define it
|
||||||
in your project :setting:`SPIDER_MIDDLEWARES` setting and assign ``None`` as its
|
in your project :setting:`SPIDER_MIDDLEWARES` setting and assign ``None`` as its
|
||||||
value. For example, if you want to disable the off-site middleware:
|
value. For example, if you want to disable the referer middleware:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
|
@ -117,36 +117,28 @@ one or more of these methods:
|
||||||
:type response: :class:`~scrapy.http.Response` object
|
:type response: :class:`~scrapy.http.Response` object
|
||||||
|
|
||||||
.. method:: process_spider_output(response, result)
|
.. method:: process_spider_output(response, result)
|
||||||
|
:async:
|
||||||
|
|
||||||
This method is called with the results returned from the Spider, after
|
This method is an :term:`asynchronous generator` called with the
|
||||||
it has processed the response.
|
results from the spider after the spider has processed the response.
|
||||||
|
|
||||||
:meth:`process_spider_output` must return an iterable of
|
.. seealso:: :ref:`universal-spider-middleware`.
|
||||||
:class:`~scrapy.Request` objects and :ref:`item objects
|
|
||||||
<topics-items>`.
|
|
||||||
|
|
||||||
Consider defining this method as an :term:`asynchronous generator`,
|
|
||||||
which will be a requirement in a future version of Scrapy. However, if
|
|
||||||
you plan on sharing your spider middleware with other people, consider
|
|
||||||
either :ref:`enforcing Scrapy 2.7 <enforce-component-requirements>`
|
|
||||||
as a minimum requirement of your spider middleware, or :ref:`making
|
|
||||||
your spider middleware universal <universal-spider-middleware>` so that
|
|
||||||
it works with Scrapy versions earlier than Scrapy 2.7.
|
|
||||||
|
|
||||||
:param response: the response which generated this output from the
|
:param response: the response which generated this output from the
|
||||||
spider
|
spider
|
||||||
:type response: :class:`~scrapy.http.Response` object
|
:type response: :class:`~scrapy.http.Response` object
|
||||||
|
|
||||||
:param result: the result returned by the spider
|
:param result: the results from the spider
|
||||||
:type result: an iterable of :class:`~scrapy.Request` objects and
|
:type result: an :term:`asynchronous iterable` of
|
||||||
:ref:`item objects <topics-items>`
|
:class:`~scrapy.Request` objects and :ref:`item objects
|
||||||
|
<topics-items>`
|
||||||
|
|
||||||
.. method:: process_spider_output_async(response, result)
|
.. method:: process_spider_output_async(response, result)
|
||||||
:async:
|
:async:
|
||||||
|
|
||||||
If defined, this method must be an :term:`asynchronous generator`,
|
Alternative name for :meth:`process_spider_output` used when
|
||||||
which will be called instead of :meth:`process_spider_output` if
|
implementing a :ref:`universal spider middleware
|
||||||
``result`` is an :term:`asynchronous iterable`.
|
<universal-spider-middleware>`.
|
||||||
|
|
||||||
.. method:: process_spider_exception(response, exception)
|
.. method:: process_spider_exception(response, exception)
|
||||||
|
|
||||||
|
|
@ -174,13 +166,40 @@ one or more of these methods:
|
||||||
:type exception: :exc:`Exception` object
|
:type exception: :exc:`Exception` object
|
||||||
|
|
||||||
|
|
||||||
|
.. _universal-spider-middleware:
|
||||||
|
|
||||||
|
Universal spider middlewares
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
In Scrapy 2.6.3 and lower, ``process_spider_output()`` must be a *synchronous*
|
||||||
|
generator.
|
||||||
|
|
||||||
|
To support those versions and higher Scrapy versions in the same middleware,
|
||||||
|
rename your asynchronous :meth:`~SpiderMiddleware.process_spider_output`
|
||||||
|
method to :meth:`~SpiderMiddleware.process_spider_output_async`, and define a
|
||||||
|
synchronous ``process_spider_output()`` method to be used by 2.6.3 and lower
|
||||||
|
versions.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
class UniversalSpiderMiddleware:
|
||||||
|
async def process_spider_output_async(self, response, result):
|
||||||
|
async for r in result:
|
||||||
|
# ... do something with r
|
||||||
|
yield r
|
||||||
|
|
||||||
|
def process_spider_output(self, response, result):
|
||||||
|
for r in result:
|
||||||
|
# ... do something with r
|
||||||
|
yield r
|
||||||
|
|
||||||
Base class for custom spider middlewares
|
Base class for custom spider middlewares
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
|
||||||
Scrapy provides a base class for custom spider middlewares. It's not required
|
Scrapy provides a base class for custom spider middlewares. It's not required
|
||||||
to use it but it can help with simplifying middleware implementations and
|
to use it but it can help with simplifying middleware implementations.
|
||||||
reducing the amount of boilerplate code in :ref:`universal middlewares
|
|
||||||
<universal-spider-middleware>`.
|
|
||||||
|
|
||||||
.. module:: scrapy.spidermiddlewares.base
|
.. module:: scrapy.spidermiddlewares.base
|
||||||
|
|
||||||
|
|
@ -336,6 +355,8 @@ Default: ``'scrapy.spidermiddlewares.referer.DefaultReferrerPolicy'``
|
||||||
using the special ``"referrer_policy"`` :ref:`Request.meta <topics-request-meta>` key,
|
using the special ``"referrer_policy"`` :ref:`Request.meta <topics-request-meta>` key,
|
||||||
with the same acceptable values as for the ``REFERRER_POLICY`` setting.
|
with the same acceptable values as for the ``REFERRER_POLICY`` setting.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-credential-leakage`
|
||||||
|
|
||||||
Acceptable values for REFERRER_POLICY
|
Acceptable values for REFERRER_POLICY
|
||||||
*************************************
|
*************************************
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -208,12 +208,6 @@ scrapy.Spider
|
||||||
:param response: the response to parse
|
:param response: the response to parse
|
||||||
:type response: :class:`~scrapy.http.Response`
|
:type response: :class:`~scrapy.http.Response`
|
||||||
|
|
||||||
.. method:: log(message, [level, component])
|
|
||||||
|
|
||||||
Wrapper that sends a log message through the Spider's :attr:`logger`,
|
|
||||||
kept for backward compatibility. For more information see
|
|
||||||
:ref:`topics-logging-from-spiders`.
|
|
||||||
|
|
||||||
.. method:: closed(reason)
|
.. method:: closed(reason)
|
||||||
|
|
||||||
Called when the spider closes. This method provides a shortcut to
|
Called when the spider closes. This method provides a shortcut to
|
||||||
|
|
@ -335,8 +329,8 @@ The above example can also be written as follows:
|
||||||
|
|
||||||
If you are :ref:`running Scrapy from a script <run-from-script>`, you can
|
If you are :ref:`running Scrapy from a script <run-from-script>`, you can
|
||||||
specify spider arguments when calling
|
specify spider arguments when calling
|
||||||
:class:`CrawlerProcess.crawl <scrapy.crawler.CrawlerProcess.crawl>` or
|
:meth:`CrawlerProcess.crawl <scrapy.crawler.CrawlerProcess.crawl>` or
|
||||||
:class:`CrawlerRunner.crawl <scrapy.crawler.CrawlerRunner.crawl>`:
|
:meth:`CrawlerRunner.crawl <scrapy.crawler.CrawlerRunner.crawl>`:
|
||||||
|
|
||||||
.. skip: next
|
.. skip: next
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
@ -354,11 +348,6 @@ Otherwise, you would cause iteration over a ``start_urls`` string
|
||||||
(a very common python pitfall)
|
(a very common python pitfall)
|
||||||
resulting in each character being seen as a separate url.
|
resulting in each character being seen as a separate url.
|
||||||
|
|
||||||
A valid use case is to set the http auth credentials
|
|
||||||
used by :class:`~scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware`::
|
|
||||||
|
|
||||||
scrapy crawl myspider -a http_user=myuser -a http_pass=mypassword
|
|
||||||
|
|
||||||
Spider arguments can also be passed through the Scrapyd ``schedule.json`` API.
|
Spider arguments can also be passed through the Scrapyd ``schedule.json`` API.
|
||||||
See `Scrapyd documentation`_.
|
See `Scrapyd documentation`_.
|
||||||
|
|
||||||
|
|
@ -457,13 +446,14 @@ with a ``TestItem`` declared in a ``myproject.items`` module:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import scrapy
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
class TestItem(scrapy.Item):
|
@dataclass
|
||||||
id = scrapy.Field()
|
class TestItem:
|
||||||
name = scrapy.Field()
|
id: str | None = None
|
||||||
description = scrapy.Field()
|
name: str | None = None
|
||||||
|
description: str | None = None
|
||||||
|
|
||||||
|
|
||||||
.. currentmodule:: scrapy.spiders
|
.. currentmodule:: scrapy.spiders
|
||||||
|
|
@ -556,7 +546,6 @@ Let's now take a look at an example CrawlSpider with rules:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import scrapy
|
|
||||||
from scrapy.spiders import CrawlSpider, Rule
|
from scrapy.spiders import CrawlSpider, Rule
|
||||||
from scrapy.linkextractors import LinkExtractor
|
from scrapy.linkextractors import LinkExtractor
|
||||||
|
|
||||||
|
|
@ -576,7 +565,7 @@ Let's now take a look at an example CrawlSpider with rules:
|
||||||
|
|
||||||
def parse_item(self, response):
|
def parse_item(self, response):
|
||||||
self.logger.info("Hi, this is an item page! %s", response.url)
|
self.logger.info("Hi, this is an item page! %s", response.url)
|
||||||
item = scrapy.Item()
|
item = {}
|
||||||
item["id"] = response.xpath('//td[@id="item_id"]/text()').re(r"ID: (\d+)")
|
item["id"] = response.xpath('//td[@id="item_id"]/text()').re(r"ID: (\d+)")
|
||||||
item["name"] = response.xpath('//td[@id="item_name"]/text()').get()
|
item["name"] = response.xpath('//td[@id="item_name"]/text()').get()
|
||||||
item["description"] = response.xpath(
|
item["description"] = response.xpath(
|
||||||
|
|
@ -598,7 +587,7 @@ Let's now take a look at an example CrawlSpider with rules:
|
||||||
This spider would start crawling example.com's home page, collecting category
|
This spider would start crawling example.com's home page, collecting category
|
||||||
links, and item links, parsing the latter with the ``parse_item`` method. For
|
links, and item links, parsing the latter with the ``parse_item`` method. For
|
||||||
each item response, some data will be extracted from the HTML using XPath, and
|
each item response, some data will be extracted from the HTML using XPath, and
|
||||||
an :class:`~scrapy.Item` will be filled with it.
|
a dictionary will be filled with it.
|
||||||
|
|
||||||
XMLFeedSpider
|
XMLFeedSpider
|
||||||
-------------
|
-------------
|
||||||
|
|
@ -619,7 +608,7 @@ XMLFeedSpider
|
||||||
|
|
||||||
A string which defines the iterator to use. It can be either:
|
A string which defines the iterator to use. It can be either:
|
||||||
|
|
||||||
- ``'iternodes'`` - a fast iterator based on regular expressions
|
- ``'iternodes'`` - a fast iterator based on ``lxml``
|
||||||
|
|
||||||
- ``'html'`` - an iterator which uses :class:`~scrapy.Selector`.
|
- ``'html'`` - an iterator which uses :class:`~scrapy.Selector`.
|
||||||
Keep in mind this uses DOM parsing and must load all DOM in memory
|
Keep in mind this uses DOM parsing and must load all DOM in memory
|
||||||
|
|
@ -714,9 +703,9 @@ These spiders are pretty easy to use, let's have a look at one example:
|
||||||
)
|
)
|
||||||
|
|
||||||
item = TestItem()
|
item = TestItem()
|
||||||
item["id"] = node.xpath("@id").get()
|
item.id = node.xpath("@id").get()
|
||||||
item["name"] = node.xpath("name").get()
|
item.name = node.xpath("name").get()
|
||||||
item["description"] = node.xpath("description").get()
|
item.description = node.xpath("description").get()
|
||||||
return item
|
return item
|
||||||
|
|
||||||
Basically what we did up there was to create a spider that downloads a feed from
|
Basically what we did up there was to create a spider that downloads a feed from
|
||||||
|
|
@ -778,9 +767,9 @@ Let's see an example similar to the previous one, but using a
|
||||||
self.logger.info("Hi, this is a row!: %r", row)
|
self.logger.info("Hi, this is a row!: %r", row)
|
||||||
|
|
||||||
item = TestItem()
|
item = TestItem()
|
||||||
item["id"] = row["id"]
|
item.id = row["id"]
|
||||||
item["name"] = row["name"]
|
item.name = row["name"]
|
||||||
item["description"] = row["description"]
|
item.description = row["description"]
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -958,6 +947,7 @@ Combine SitemapSpider with other sources of urls:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
from scrapy import Request
|
||||||
from scrapy.spiders import SitemapSpider
|
from scrapy.spiders import SitemapSpider
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ Collector, and can be accessed through the :attr:`~scrapy.crawler.Crawler.stats`
|
||||||
attribute of the :ref:`topics-api-crawler`, as illustrated by the examples in
|
attribute of the :ref:`topics-api-crawler`, as illustrated by the examples in
|
||||||
the :ref:`topics-stats-usecases` section below.
|
the :ref:`topics-stats-usecases` section below.
|
||||||
|
|
||||||
However, the Stats Collector is always available, so you can always import it
|
The Stats Collector API is always available, so you can always use it (to
|
||||||
in your module and use its API (to increment or set new stat keys), regardless
|
increment or set new stat keys), regardless
|
||||||
of whether the stats collection is enabled or not. If it's disabled, the API
|
of whether the stats collection is enabled or not. If it's disabled, the API
|
||||||
will still work but it won't collect anything. This is aimed at simplifying the
|
will still work but it won't collect anything. This is aimed at simplifying the
|
||||||
stats collector usage: you should spend no more than one line of code for
|
stats collector usage: you should spend no more than one line of code for
|
||||||
|
|
@ -21,9 +21,6 @@ using the Stats Collector from.
|
||||||
Another feature of the Stats Collector is that it's very efficient (when
|
Another feature of the Stats Collector is that it's very efficient (when
|
||||||
enabled) and extremely efficient (almost unnoticeable) when disabled.
|
enabled) and extremely efficient (almost unnoticeable) when disabled.
|
||||||
|
|
||||||
The Stats Collector keeps a stats table per open spider which is automatically
|
|
||||||
opened when the spider is opened, and closed when the spider is closed.
|
|
||||||
|
|
||||||
.. _topics-stats-usecases:
|
.. _topics-stats-usecases:
|
||||||
|
|
||||||
Common Stats Collector uses
|
Common Stats Collector uses
|
||||||
|
|
@ -87,13 +84,13 @@ Get all stats:
|
||||||
Available Stats Collectors
|
Available Stats Collectors
|
||||||
==========================
|
==========================
|
||||||
|
|
||||||
|
.. currentmodule:: scrapy.statscollectors
|
||||||
|
|
||||||
Besides the basic :class:`StatsCollector` there are other Stats Collectors
|
Besides the basic :class:`StatsCollector` there are other Stats Collectors
|
||||||
available in Scrapy which extend the basic Stats Collector. You can select
|
available in Scrapy which extend the basic Stats Collector. You can select
|
||||||
which Stats Collector to use through the :setting:`STATS_CLASS` setting. The
|
which Stats Collector to use through the :setting:`STATS_CLASS` setting. The
|
||||||
default Stats Collector used is the :class:`MemoryStatsCollector`.
|
default Stats Collector used is the :class:`MemoryStatsCollector`.
|
||||||
|
|
||||||
.. currentmodule:: scrapy.statscollectors
|
|
||||||
|
|
||||||
MemoryStatsCollector
|
MemoryStatsCollector
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
|
@ -102,7 +99,7 @@ MemoryStatsCollector
|
||||||
A simple stats collector that keeps the stats of the last scraping run (for
|
A simple stats collector that keeps the stats of the last scraping run (for
|
||||||
each spider) in memory, after they're closed. The stats can be accessed
|
each spider) in memory, after they're closed. The stats can be accessed
|
||||||
through the :attr:`spider_stats` attribute, which is a dict keyed by spider
|
through the :attr:`spider_stats` attribute, which is a dict keyed by spider
|
||||||
domain name.
|
name.
|
||||||
|
|
||||||
This is the default Stats Collector used in Scrapy.
|
This is the default Stats Collector used in Scrapy.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,14 +29,16 @@ disable it if you want. For more information about the extension itself see
|
||||||
.. note::
|
.. note::
|
||||||
This feature is not supported when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
This feature is not supported when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-telnet`
|
||||||
|
|
||||||
.. highlight:: none
|
.. highlight:: none
|
||||||
|
|
||||||
How to access the telnet console
|
How to access the telnet console
|
||||||
================================
|
================================
|
||||||
|
|
||||||
The telnet console listens in the TCP port defined in the
|
The telnet console listens on the first available TCP port from the range
|
||||||
:setting:`TELNETCONSOLE_PORT` setting, which defaults to ``6023``. To access
|
defined in the :setting:`TELNETCONSOLE_PORT` setting, which defaults to
|
||||||
the console you need to type::
|
``[6023, 6073]``. To access the console you need to type::
|
||||||
|
|
||||||
telnet localhost 6023
|
telnet localhost 6023
|
||||||
Trying localhost...
|
Trying localhost...
|
||||||
|
|
@ -46,12 +48,12 @@ the console you need to type::
|
||||||
Password:
|
Password:
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
By default Username is ``scrapy`` and Password is autogenerated. The
|
By default, the username is ``scrapy`` and the password is autogenerated. The
|
||||||
autogenerated Password can be seen on Scrapy logs like the example below::
|
autogenerated password can be seen on Scrapy logs like the example below::
|
||||||
|
|
||||||
2018-10-16 14:35:21 [scrapy.extensions.telnet] INFO: Telnet Password: 16f92501e8a59326
|
2018-10-16 14:35:21 [scrapy.extensions.telnet] INFO: Telnet Password: 16f92501e8a59326
|
||||||
|
|
||||||
Default Username and Password can be overridden by the settings
|
The default username and password can be overridden by the settings
|
||||||
:setting:`TELNETCONSOLE_USERNAME` and :setting:`TELNETCONSOLE_PASSWORD`.
|
:setting:`TELNETCONSOLE_USERNAME` and :setting:`TELNETCONSOLE_PASSWORD`.
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
|
|
@ -94,8 +96,6 @@ convenience:
|
||||||
+----------------+-------------------------------------------------------------------+
|
+----------------+-------------------------------------------------------------------+
|
||||||
| ``p`` | a shortcut to the :func:`pprint.pprint` function |
|
| ``p`` | a shortcut to the :func:`pprint.pprint` function |
|
||||||
+----------------+-------------------------------------------------------------------+
|
+----------------+-------------------------------------------------------------------+
|
||||||
| ``hpy`` | for memory debugging (see :ref:`topics-leaks`) |
|
|
||||||
+----------------+-------------------------------------------------------------------+
|
|
||||||
|
|
||||||
Telnet console usage examples
|
Telnet console usage examples
|
||||||
=============================
|
=============================
|
||||||
|
|
@ -107,8 +107,8 @@ Here are some example tasks you can do with the telnet console:
|
||||||
View engine status
|
View engine status
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
You can use the ``est()`` method of the Scrapy engine to quickly show its state
|
You can use the ``est()`` method provided by the console to quickly show the
|
||||||
using the telnet console::
|
engine status::
|
||||||
|
|
||||||
telnet localhost 6023
|
telnet localhost 6023
|
||||||
>>> est()
|
>>> est()
|
||||||
|
|
@ -192,6 +192,8 @@ Default: ``'127.0.0.1'``
|
||||||
|
|
||||||
The interface the telnet console should listen on
|
The interface the telnet console should listen on
|
||||||
|
|
||||||
|
.. seealso:: :ref:`security-telnet`
|
||||||
|
|
||||||
|
|
||||||
.. setting:: TELNETCONSOLE_USERNAME
|
.. setting:: TELNETCONSOLE_USERNAME
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,8 @@ API stability
|
||||||
|
|
||||||
API stability was one of the major goals for the *1.0* release.
|
API stability was one of the major goals for the *1.0* release.
|
||||||
|
|
||||||
Methods or functions that start with a single dash (``_``) are private and
|
Methods or functions that start with a single underscore (``_``) are private
|
||||||
should never be relied as stable.
|
and should never be relied upon as stable.
|
||||||
|
|
||||||
Also, keep in mind that stable doesn't mean complete: stable APIs could grow
|
Also, keep in mind that stable doesn't mean complete: stable APIs could grow
|
||||||
new methods or functionality but the existing methods should keep working the
|
new methods or functionality but the existing methods should keep working the
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class Root(Resource):
|
||||||
self.tail.clear()
|
self.tail.clear()
|
||||||
self.start = self.lastmark = self.lasttime = time()
|
self.start = self.lastmark = self.lasttime = time()
|
||||||
|
|
||||||
def getChild(self, request, name):
|
def getChild(self, path, request):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def render(self, request):
|
def render(self, request):
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,6 @@ class QPSSpider(Spider):
|
||||||
self.download_delay = float(self.download_delay)
|
self.download_delay = float(self.download_delay)
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
for item_or_request in self.start_requests():
|
|
||||||
yield item_or_request
|
|
||||||
|
|
||||||
def start_requests(self):
|
|
||||||
url = self.benchurl
|
url = self.benchurl
|
||||||
if self.latency is not None:
|
if self.latency is not None:
|
||||||
url += f"?latency={self.latency}"
|
url += f"?latency={self.latency}"
|
||||||
|
|
|
||||||
126
pyproject.toml
126
pyproject.toml
|
|
@ -7,8 +7,7 @@ name = "Scrapy"
|
||||||
dynamic = ["version"]
|
dynamic = ["version"]
|
||||||
description = "A high-level Web Crawling and Web Scraping framework"
|
description = "A high-level Web Crawling and Web Scraping framework"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
# Twisted pinned until Scrapy is updated for its internal TLS API changes
|
"Twisted>=21.7.0",
|
||||||
"Twisted>=21.7.0,<=25.5.0",
|
|
||||||
"cryptography>=37.0.0",
|
"cryptography>=37.0.0",
|
||||||
"cssselect>=0.9.1",
|
"cssselect>=0.9.1",
|
||||||
"defusedxml>=0.7.1",
|
"defusedxml>=0.7.1",
|
||||||
|
|
@ -20,7 +19,7 @@ dependencies = [
|
||||||
"protego>=0.1.15",
|
"protego>=0.1.15",
|
||||||
"pyOpenSSL>=22.0.0",
|
"pyOpenSSL>=22.0.0",
|
||||||
"queuelib>=1.4.2",
|
"queuelib>=1.4.2",
|
||||||
"service_identity>=18.1.0",
|
"service_identity>=23.1.0",
|
||||||
"tldextract",
|
"tldextract",
|
||||||
"w3lib>=1.17.0",
|
"w3lib>=1.17.0",
|
||||||
"zope.interface>=5.1.0",
|
"zope.interface>=5.1.0",
|
||||||
|
|
@ -40,6 +39,7 @@ classifiers = [
|
||||||
"Programming Language :: Python :: 3.11",
|
"Programming Language :: Python :: 3.11",
|
||||||
"Programming Language :: Python :: 3.12",
|
"Programming Language :: Python :: 3.12",
|
||||||
"Programming Language :: Python :: 3.13",
|
"Programming Language :: Python :: 3.13",
|
||||||
|
"Programming Language :: Python :: 3.14",
|
||||||
"Programming Language :: Python :: Implementation :: CPython",
|
"Programming Language :: Python :: Implementation :: CPython",
|
||||||
"Programming Language :: Python :: Implementation :: PyPy",
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
"Topic :: Internet :: WWW/HTTP",
|
"Topic :: Internet :: WWW/HTTP",
|
||||||
|
|
@ -86,17 +86,90 @@ pattern = "^(?P<version>.+)$"
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
strict = true
|
strict = true
|
||||||
allow_any_generics = true # 67 errors
|
|
||||||
extra_checks = false # weird addErrback() errors
|
extra_checks = false # weird addErrback() errors
|
||||||
untyped_calls_exclude = [
|
untyped_calls_exclude = [
|
||||||
"twisted",
|
"twisted",
|
||||||
]
|
]
|
||||||
warn_return_any = false # 37 errors
|
|
||||||
|
|
||||||
[[tool.mypy.overrides]]
|
[[tool.mypy.overrides]]
|
||||||
module = "tests.*"
|
module = "tests.*"
|
||||||
allow_untyped_defs = true
|
allow_untyped_defs = true
|
||||||
allow_incomplete_defs = true # 48 errors
|
allow_incomplete_defs = true # 59 errors
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
[[tool.mypy.overrides]]
|
||||||
|
module = [
|
||||||
|
"tests.mockserver.*",
|
||||||
|
"tests.spiders",
|
||||||
|
"tests.test_closespider",
|
||||||
|
"tests.test_cmdline",
|
||||||
|
"tests.test_contracts",
|
||||||
|
"tests.test_core_downloader",
|
||||||
|
"tests.test_downloader_handler_twisted_ftp",
|
||||||
|
"tests.test_downloadermiddleware_cookies",
|
||||||
|
"tests.test_downloadermiddleware_httpauth",
|
||||||
|
"tests.test_downloadermiddleware_httpcache",
|
||||||
|
"tests.test_downloadermiddleware_httpcompression",
|
||||||
|
"tests.test_downloadermiddleware_httpproxy",
|
||||||
|
"tests.test_downloadermiddleware_offsite",
|
||||||
|
"tests.test_downloadermiddleware_redirect",
|
||||||
|
"tests.test_downloadermiddleware_redirect_base",
|
||||||
|
"tests.test_downloadermiddleware_redirect_metarefresh",
|
||||||
|
"tests.test_downloadermiddleware_retry",
|
||||||
|
"tests.test_downloadermiddleware_robotstxt",
|
||||||
|
"tests.test_downloadermiddleware_stats",
|
||||||
|
"tests.test_downloaderslotssettings",
|
||||||
|
"tests.test_dupefilters",
|
||||||
|
"tests.test_engine_loop",
|
||||||
|
"tests.test_exporters",
|
||||||
|
"tests.test_extension_statsmailer",
|
||||||
|
"tests.test_extension_throttle",
|
||||||
|
"tests.test_feedexport",
|
||||||
|
"tests.test_feedexport_postprocess",
|
||||||
|
"tests.test_feedexport_storages",
|
||||||
|
"tests.test_feedexport_uri_params",
|
||||||
|
"tests.test_http2_client_protocol",
|
||||||
|
"tests.test_http_headers",
|
||||||
|
"tests.test_http_request",
|
||||||
|
"tests.test_http_request_form",
|
||||||
|
"tests.test_http_response",
|
||||||
|
"tests.test_http_response_text",
|
||||||
|
"tests.test_item",
|
||||||
|
"tests.test_link",
|
||||||
|
"tests.test_linkextractors",
|
||||||
|
"tests.test_loader",
|
||||||
|
"tests.test_logformatter",
|
||||||
|
"tests.test_logstats",
|
||||||
|
"tests.test_mail",
|
||||||
|
"tests.test_pipeline_crawl",
|
||||||
|
"tests.test_pipeline_files",
|
||||||
|
"tests.test_pipeline_images",
|
||||||
|
"tests.test_pipeline_media",
|
||||||
|
"tests.test_pipelines",
|
||||||
|
"tests.test_pqueues",
|
||||||
|
"tests.test_request_attribute_binding",
|
||||||
|
"tests.test_request_cb_kwargs",
|
||||||
|
"tests.test_request_dict",
|
||||||
|
"tests.test_request_left",
|
||||||
|
"tests.test_robotstxt_interface",
|
||||||
|
"tests.test_scheduler_base",
|
||||||
|
"tests.test_settings",
|
||||||
|
"tests.test_spider",
|
||||||
|
"tests.test_spider_crawl",
|
||||||
|
"tests.test_spidermiddleware_output_chain",
|
||||||
|
"tests.test_spidermiddleware_process_start",
|
||||||
|
"tests.test_spider_sitemap",
|
||||||
|
"tests.test_squeues",
|
||||||
|
"tests.test_squeues_request",
|
||||||
|
"tests.test_stats",
|
||||||
|
"tests.test_utils_datatypes",
|
||||||
|
"tests.test_utils_decorators",
|
||||||
|
"tests.test_utils_defer",
|
||||||
|
"tests.test_utils_deprecate",
|
||||||
|
"tests.test_utils_misc.test_return_with_argument_inside_generator",
|
||||||
|
"tests.test_utils_python",
|
||||||
|
"tests.test_utils_request",
|
||||||
|
]
|
||||||
check_untyped_defs = false
|
check_untyped_defs = false
|
||||||
|
|
||||||
# Interface classes are hard to support
|
# Interface classes are hard to support
|
||||||
|
|
@ -122,24 +195,9 @@ implicit_reexport = true
|
||||||
module = "scrapy.settings.default_settings"
|
module = "scrapy.settings.default_settings"
|
||||||
ignore_errors = true
|
ignore_errors = true
|
||||||
|
|
||||||
# deprecated modules
|
|
||||||
[[tool.mypy.overrides]]
|
|
||||||
module = [
|
|
||||||
"scrapy.core.downloader.webclient",
|
|
||||||
"scrapy.spiders.init",
|
|
||||||
"scrapy.utils.testsite",
|
|
||||||
"tests.test_webclient",
|
|
||||||
]
|
|
||||||
allow_any_generics = true
|
|
||||||
allow_untyped_calls = true
|
|
||||||
allow_untyped_defs = true
|
|
||||||
check_untyped_defs = false
|
|
||||||
warn_return_any = false
|
|
||||||
|
|
||||||
# usually no type hints
|
# usually no type hints
|
||||||
[[tool.mypy.overrides]]
|
[[tool.mypy.overrides]]
|
||||||
module = [
|
module = [
|
||||||
# "IPython.*",
|
|
||||||
"bpython",
|
"bpython",
|
||||||
"brotli",
|
"brotli",
|
||||||
"brotlicffi",
|
"brotlicffi",
|
||||||
|
|
@ -154,7 +212,7 @@ module = [
|
||||||
ignore_missing_imports = true
|
ignore_missing_imports = true
|
||||||
|
|
||||||
[tool.bumpversion]
|
[tool.bumpversion]
|
||||||
current_version = "2.15.0"
|
current_version = "2.17.0"
|
||||||
commit = true
|
commit = true
|
||||||
tag = true
|
tag = true
|
||||||
tag_name = "{new_version}"
|
tag_name = "{new_version}"
|
||||||
|
|
@ -174,6 +232,8 @@ parse = """(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)"""
|
||||||
serialize = ["{major}.{minor}"]
|
serialize = ["{major}.{minor}"]
|
||||||
|
|
||||||
[tool.coverage.run]
|
[tool.coverage.run]
|
||||||
|
# sysmon, default on 3.14, is too slow: https://github.com/coveragepy/coveragepy/issues/2172
|
||||||
|
core = "ctrace"
|
||||||
branch = true
|
branch = true
|
||||||
include = ["scrapy/*"]
|
include = ["scrapy/*"]
|
||||||
omit = ["tests/*"]
|
omit = ["tests/*"]
|
||||||
|
|
@ -200,6 +260,7 @@ jobs = 1 # >1 hides results
|
||||||
extension-pkg-allow-list=[
|
extension-pkg-allow-list=[
|
||||||
"lxml",
|
"lxml",
|
||||||
]
|
]
|
||||||
|
load-plugins = ["pylint_per_file_ignores"]
|
||||||
|
|
||||||
[tool.pylint."MESSAGES CONTROL"]
|
[tool.pylint."MESSAGES CONTROL"]
|
||||||
enable = [
|
enable = [
|
||||||
|
|
@ -241,8 +302,10 @@ disable = [
|
||||||
"too-many-positional-arguments",
|
"too-many-positional-arguments",
|
||||||
"too-many-public-methods",
|
"too-many-public-methods",
|
||||||
"too-many-return-statements",
|
"too-many-return-statements",
|
||||||
|
"undefined-variable",
|
||||||
"unused-argument",
|
"unused-argument",
|
||||||
"unused-variable",
|
"unused-variable",
|
||||||
|
"use-implicit-booleaness-not-comparison",
|
||||||
"useless-import-alias", # used as a hint to mypy
|
"useless-import-alias", # used as a hint to mypy
|
||||||
"useless-return", # https://github.com/pylint-dev/pylint/issues/6530
|
"useless-return", # https://github.com/pylint-dev/pylint/issues/6530
|
||||||
"wrong-import-position",
|
"wrong-import-position",
|
||||||
|
|
@ -259,15 +322,13 @@ disable = [
|
||||||
"unused-import",
|
"unused-import",
|
||||||
|
|
||||||
# Ones that we may want to address (fix, ignore per-line or move to "don't want to fix")
|
# Ones that we may want to address (fix, ignore per-line or move to "don't want to fix")
|
||||||
"abstract-method",
|
|
||||||
"arguments-differ",
|
"arguments-differ",
|
||||||
"arguments-renamed",
|
|
||||||
"dangerous-default-value",
|
|
||||||
"keyword-arg-before-vararg",
|
"keyword-arg-before-vararg",
|
||||||
"pointless-statement",
|
]
|
||||||
"raise-missing-from",
|
# requires `pylint_per_file_ignores` plugin
|
||||||
"unnecessary-dunder-call",
|
per-file-ignores = [
|
||||||
"used-before-assignment",
|
# Extended list of ones that we may want to address, only for tests
|
||||||
|
"./tests/*:abstract-method,arguments-renamed,dangerous-default-value,pointless-statement,raise-missing-from,unnecessary-dunder-call,used-before-assignment",
|
||||||
]
|
]
|
||||||
|
|
||||||
[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
|
|
@ -283,10 +344,13 @@ markers = [
|
||||||
"requires_uvloop: marks tests as only enabled when uvloop is known to be working",
|
"requires_uvloop: marks tests as only enabled when uvloop is known to be working",
|
||||||
"requires_botocore: marks tests that need botocore (but not boto3)",
|
"requires_botocore: marks tests that need botocore (but not boto3)",
|
||||||
"requires_boto3: marks tests that need botocore and boto3",
|
"requires_boto3: marks tests that need botocore and boto3",
|
||||||
"requires_mitmproxy: marks tests that need mitmproxy",
|
"requires_mitmproxy: marks tests that need a mitmdump executable",
|
||||||
|
"requires_internet: marks tests that need real Internet access",
|
||||||
]
|
]
|
||||||
filterwarnings = [
|
filterwarnings = [
|
||||||
"ignore::DeprecationWarning:twisted.web.static",
|
"ignore::DeprecationWarning:twisted.web.static",
|
||||||
|
# Twisted doesn't close failed sockets after CannotListenError: https://github.com/twisted/twisted/issues/6108
|
||||||
|
"ignore:Exception ignored in. <socket\\.socket.*laddr=..0\\.0\\.0\\.0., 0.:pytest.PytestUnraisableExceptionWarning",
|
||||||
]
|
]
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
|
|
@ -466,4 +530,4 @@ split-on-trailing-comma = false
|
||||||
convention = "pep257"
|
convention = "pep257"
|
||||||
|
|
||||||
[tool.sphinx-scrapy]
|
[tool.sphinx-scrapy]
|
||||||
python-version = "3.13" # Keep in sync with .github/workflows/checks.yml.
|
python-version = "3.14" # Keep in sync with .github/workflows/checks.yml.
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
2.15.0
|
2.17.0
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
# pragma: no file cover
|
||||||
from scrapy.cmdline import execute
|
from scrapy.cmdline import execute
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ class AddonManager:
|
||||||
|
|
||||||
:param settings: The :class:`~scrapy.settings.BaseSettings` object from \
|
:param settings: The :class:`~scrapy.settings.BaseSettings` object from \
|
||||||
which to read the early add-on configuration
|
which to read the early add-on configuration
|
||||||
:type settings: :class:`~scrapy.settings.Settings`
|
:type settings: :class:`~scrapy.settings.BaseSettings`
|
||||||
"""
|
"""
|
||||||
for clspath in build_component_list(settings["ADDONS"]):
|
for clspath in build_component_list(settings["ADDONS"]):
|
||||||
addoncls = load_object(clspath)
|
addoncls = load_object(clspath)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ from twisted.python import failure
|
||||||
|
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning, UsageError
|
from scrapy.exceptions import ScrapyDeprecationWarning, UsageError
|
||||||
from scrapy.utils.conf import arglist_to_dict, feed_process_params_from_cli
|
from scrapy.utils.conf import arglist_to_dict, feed_process_params_from_cli
|
||||||
|
from scrapy.utils.deprecate import method_is_overridden
|
||||||
|
from scrapy.utils.python import global_object_name
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
|
|
@ -36,6 +38,14 @@ class ScrapyCommand(ABC):
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.settings: Settings | None = None # set in scrapy.cmdline
|
self.settings: Settings | None = None # set in scrapy.cmdline
|
||||||
|
if method_is_overridden(self.__class__, ScrapyCommand, "help"):
|
||||||
|
warnings.warn(
|
||||||
|
"The ScrapyCommand.help() method is deprecated and overriding "
|
||||||
|
f"it, as the {global_object_name(self.__class__)} class does, "
|
||||||
|
"has no effect; override long_desc() instead.",
|
||||||
|
ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
|
||||||
def set_crawler(self, crawler: Crawler) -> None: # pragma: no cover
|
def set_crawler(self, crawler: Crawler) -> None: # pragma: no cover
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
|
|
@ -63,15 +73,16 @@ class ScrapyCommand(ABC):
|
||||||
def long_desc(self) -> str:
|
def long_desc(self) -> str:
|
||||||
"""A long description of the command. Return short description when not
|
"""A long description of the command. Return short description when not
|
||||||
available. It cannot contain newlines since contents will be formatted
|
available. It cannot contain newlines since contents will be formatted
|
||||||
by optparser which removes newlines and wraps text.
|
by argparse which removes newlines and wraps text.
|
||||||
"""
|
"""
|
||||||
return self.short_desc()
|
return self.short_desc()
|
||||||
|
|
||||||
def help(self) -> str:
|
def help(self) -> str:
|
||||||
"""An extensive help for the command. It will be shown when using the
|
warnings.warn(
|
||||||
"help" command. It can contain newlines since no post-formatting will
|
"ScrapyCommand.help() is deprecated, use long_desc() instead.",
|
||||||
be applied to its contents.
|
ScrapyDeprecationWarning,
|
||||||
"""
|
stacklevel=2,
|
||||||
|
)
|
||||||
return self.long_desc()
|
return self.long_desc()
|
||||||
|
|
||||||
def add_options(self, parser: argparse.ArgumentParser) -> None:
|
def add_options(self, parser: argparse.ArgumentParser) -> None:
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,9 @@ class Command(ScrapyCommand):
|
||||||
def run(self, args: list[str], opts: argparse.Namespace) -> None:
|
def run(self, args: list[str], opts: argparse.Namespace) -> None:
|
||||||
# load contracts
|
# load contracts
|
||||||
assert self.settings is not None
|
assert self.settings is not None
|
||||||
contracts = build_component_list(self.settings.getwithbase("SPIDER_CONTRACTS"))
|
contracts = build_component_list(
|
||||||
|
self.settings.get_component_priority_dict_with_base("SPIDER_CONTRACTS")
|
||||||
|
)
|
||||||
conman = ContractsManager(load_object(c) for c in contracts)
|
conman = ContractsManager(load_object(c) for c in contracts)
|
||||||
runner = TextTestRunner(verbosity=2 if opts.verbose else 1)
|
runner = TextTestRunner(verbosity=2 if opts.verbose else 1)
|
||||||
result = TextTestResult(runner.stream, runner.descriptions, runner.verbosity)
|
result = TextTestResult(runner.stream, runner.descriptions, runner.verbosity)
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,29 @@
|
||||||
import argparse
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import shlex
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import Any, ClassVar
|
from pathlib import Path
|
||||||
|
from typing import TYPE_CHECKING, Any, ClassVar
|
||||||
|
|
||||||
from scrapy.commands import ScrapyCommand
|
from scrapy.commands import ScrapyCommand
|
||||||
from scrapy.exceptions import UsageError
|
from scrapy.exceptions import UsageError
|
||||||
from scrapy.spiderloader import get_spider_loader
|
from scrapy.spiderloader import get_spider_loader
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
def _edit_file(editor: str, file_path: str | os.PathLike[str]) -> int:
|
||||||
|
"""Open ``file_path`` with ``editor`` and return the editor exit code.
|
||||||
|
|
||||||
|
``editor`` may include arguments (e.g. ``"code -w"``); it is split with
|
||||||
|
:func:`shlex.split` and the file is passed as a separate argument, so no
|
||||||
|
shell is involved.
|
||||||
|
"""
|
||||||
|
return subprocess.call([*shlex.split(editor), os.fspath(file_path)]) # noqa: S603
|
||||||
|
|
||||||
|
|
||||||
class Command(ScrapyCommand):
|
class Command(ScrapyCommand):
|
||||||
requires_project = True
|
requires_project = True
|
||||||
|
|
@ -45,4 +62,4 @@ class Command(ScrapyCommand):
|
||||||
sfile = sys.modules[spidercls.__module__].__file__
|
sfile = sys.modules[spidercls.__module__].__file__
|
||||||
assert sfile
|
assert sfile
|
||||||
sfile = sfile.replace(".pyc", ".py")
|
sfile = sfile.replace(".pyc", ".py")
|
||||||
self.exitcode = os.system(f'{editor} "{sfile}"') # noqa: S605
|
self.exitcode = _edit_file(editor, Path(sfile))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
@ -10,12 +9,14 @@ from urllib.parse import urlparse
|
||||||
|
|
||||||
import scrapy
|
import scrapy
|
||||||
from scrapy.commands import ScrapyCommand
|
from scrapy.commands import ScrapyCommand
|
||||||
|
from scrapy.commands.edit import _edit_file
|
||||||
from scrapy.exceptions import UsageError
|
from scrapy.exceptions import UsageError
|
||||||
from scrapy.spiderloader import get_spider_loader
|
from scrapy.spiderloader import get_spider_loader
|
||||||
from scrapy.utils.template import render_templatefile, string_camelcase
|
from scrapy.utils.template import render_templatefile, string_camelcase
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
def sanitize_module_name(module_name: str) -> str:
|
def sanitize_module_name(module_name: str) -> str:
|
||||||
|
|
@ -118,9 +119,11 @@ class Command(ScrapyCommand):
|
||||||
|
|
||||||
template_file = self._find_template(opts.template)
|
template_file = self._find_template(opts.template)
|
||||||
if template_file:
|
if template_file:
|
||||||
self._genspider(module, name, url, opts.template, template_file)
|
spider_file = self._genspider(
|
||||||
|
module, name, url, opts.template, template_file
|
||||||
|
)
|
||||||
if opts.edit:
|
if opts.edit:
|
||||||
self.exitcode = os.system(f'scrapy edit "{name}"') # noqa: S605
|
self.exitcode = _edit_file(self.settings["EDITOR"], spider_file)
|
||||||
|
|
||||||
def _generate_template_variables(
|
def _generate_template_variables(
|
||||||
self,
|
self,
|
||||||
|
|
@ -147,8 +150,8 @@ class Command(ScrapyCommand):
|
||||||
name: str,
|
name: str,
|
||||||
url: str,
|
url: str,
|
||||||
template_name: str,
|
template_name: str,
|
||||||
template_file: str | os.PathLike,
|
template_file: str | os.PathLike[str],
|
||||||
) -> None:
|
) -> Path:
|
||||||
"""Generate the spider module, based on the given template"""
|
"""Generate the spider module, based on the given template"""
|
||||||
assert self.settings is not None
|
assert self.settings is not None
|
||||||
tvars = self._generate_template_variables(module, name, url, template_name)
|
tvars = self._generate_template_variables(module, name, url, template_name)
|
||||||
|
|
@ -168,6 +171,7 @@ class Command(ScrapyCommand):
|
||||||
)
|
)
|
||||||
if spiders_module:
|
if spiders_module:
|
||||||
print(f"in module:\n {spiders_module.__name__}.{module}")
|
print(f"in module:\n {spiders_module.__name__}.{module}")
|
||||||
|
return Path(spider_file)
|
||||||
|
|
||||||
def _find_template(self, template: str) -> Path | None:
|
def _find_template(self, template: str) -> Path | None:
|
||||||
template_file = Path(self.templates_dir, f"{template}.tmpl")
|
template_file = Path(self.templates_dir, f"{template}.tmpl")
|
||||||
|
|
|
||||||
|
|
@ -144,17 +144,16 @@ class Command(BaseRunSpiderCommand):
|
||||||
def iterate_spider_output(self, result: _T) -> Iterable[Any]: ...
|
def iterate_spider_output(self, result: _T) -> Iterable[Any]: ...
|
||||||
|
|
||||||
def iterate_spider_output(self, result: Any) -> Iterable[Any] | Deferred[Any]:
|
def iterate_spider_output(self, result: Any) -> Iterable[Any] | Deferred[Any]:
|
||||||
|
d: Deferred[Any]
|
||||||
if inspect.isasyncgen(result):
|
if inspect.isasyncgen(result):
|
||||||
d = deferred_from_coro(
|
d = deferred_from_coro(
|
||||||
collect_asyncgen(aiter_errback(result, self.handle_exception))
|
collect_asyncgen(aiter_errback(result, self.handle_exception))
|
||||||
)
|
)
|
||||||
d.addCallback(self.iterate_spider_output)
|
return d.addCallback(self.iterate_spider_output)
|
||||||
return d
|
d = deferred_from_coro(result)
|
||||||
if inspect.iscoroutine(result):
|
if inspect.iscoroutine(result):
|
||||||
d = deferred_from_coro(result)
|
return d.addCallback(self.iterate_spider_output)
|
||||||
d.addCallback(self.iterate_spider_output)
|
return arg_to_iter(d)
|
||||||
return d
|
|
||||||
return arg_to_iter(deferred_from_coro(result))
|
|
||||||
|
|
||||||
def add_items(self, lvl: int, new_items: list[Any]) -> None:
|
def add_items(self, lvl: int, new_items: list[Any]) -> None:
|
||||||
old_items = self.items.get(lvl, [])
|
old_items = self.items.get(lvl, [])
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ class Contract:
|
||||||
request_cls: type[Request] | None = None
|
request_cls: type[Request] | None = None
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
def __init__(self, method: Callable, *args: Any):
|
def __init__(self, method: Callable[..., Any], *args: Any):
|
||||||
self.testcase_pre = _create_testcase(method, f"@{self.name} pre-hook")
|
self.testcase_pre = _create_testcase(method, f"@{self.name} pre-hook")
|
||||||
self.testcase_post = _create_testcase(method, f"@{self.name} post-hook")
|
self.testcase_post = _create_testcase(method, f"@{self.name} post-hook")
|
||||||
self.args: tuple[Any, ...] = args
|
self.args: tuple[Any, ...] = args
|
||||||
|
|
@ -51,6 +51,8 @@ class Contract:
|
||||||
results.addSuccess(self.testcase_pre)
|
results.addSuccess(self.testcase_pre)
|
||||||
cb_result = cb(response, **cb_kwargs)
|
cb_result = cb(response, **cb_kwargs)
|
||||||
if isinstance(cb_result, (AsyncGenerator, CoroutineType)):
|
if isinstance(cb_result, (AsyncGenerator, CoroutineType)):
|
||||||
|
if isinstance(cb_result, CoroutineType):
|
||||||
|
cb_result.close()
|
||||||
raise TypeError("Contracts don't support async callbacks")
|
raise TypeError("Contracts don't support async callbacks")
|
||||||
return list(cast("Iterable[Any]", iterate_spider_output(cb_result)))
|
return list(cast("Iterable[Any]", iterate_spider_output(cb_result)))
|
||||||
|
|
||||||
|
|
@ -67,6 +69,8 @@ class Contract:
|
||||||
def wrapper(response: Response, **cb_kwargs: Any) -> list[Any]:
|
def wrapper(response: Response, **cb_kwargs: Any) -> list[Any]:
|
||||||
cb_result = cb(response, **cb_kwargs)
|
cb_result = cb(response, **cb_kwargs)
|
||||||
if isinstance(cb_result, (AsyncGenerator, CoroutineType)):
|
if isinstance(cb_result, (AsyncGenerator, CoroutineType)):
|
||||||
|
if isinstance(cb_result, CoroutineType):
|
||||||
|
cb_result.close()
|
||||||
raise TypeError("Contracts don't support async callbacks")
|
raise TypeError("Contracts don't support async callbacks")
|
||||||
output = list(cast("Iterable[Any]", iterate_spider_output(cb_result)))
|
output = list(cast("Iterable[Any]", iterate_spider_output(cb_result)))
|
||||||
try:
|
try:
|
||||||
|
|
@ -105,7 +109,7 @@ class ContractsManager:
|
||||||
|
|
||||||
return methods
|
return methods
|
||||||
|
|
||||||
def extract_contracts(self, method: Callable) -> list[Contract]:
|
def extract_contracts(self, method: Callable[..., Any]) -> list[Contract]:
|
||||||
contracts: list[Contract] = []
|
contracts: list[Contract] = []
|
||||||
assert method.__doc__ is not None
|
assert method.__doc__ is not None
|
||||||
for line_ in method.__doc__.split("\n"):
|
for line_ in method.__doc__.split("\n"):
|
||||||
|
|
@ -125,7 +129,7 @@ class ContractsManager:
|
||||||
def from_spider(self, spider: Spider, results: TestResult) -> list[Request | None]:
|
def from_spider(self, spider: Spider, results: TestResult) -> list[Request | None]:
|
||||||
requests: list[Request | None] = []
|
requests: list[Request | None] = []
|
||||||
for method in self.tested_methods_from_spidercls(type(spider)):
|
for method in self.tested_methods_from_spidercls(type(spider)):
|
||||||
bound_method = spider.__getattribute__(method)
|
bound_method = getattr(spider, method)
|
||||||
try:
|
try:
|
||||||
requests.append(self.from_method(bound_method, results))
|
requests.append(self.from_method(bound_method, results))
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
@ -134,7 +138,9 @@ class ContractsManager:
|
||||||
|
|
||||||
return requests
|
return requests
|
||||||
|
|
||||||
def from_method(self, method: Callable, results: TestResult) -> Request | None:
|
def from_method(
|
||||||
|
self, method: Callable[..., Any], results: TestResult
|
||||||
|
) -> Request | None:
|
||||||
contracts = self.extract_contracts(method)
|
contracts = self.extract_contracts(method)
|
||||||
if contracts:
|
if contracts:
|
||||||
request_cls = Request
|
request_cls = Request
|
||||||
|
|
@ -170,7 +176,7 @@ class ContractsManager:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _clean_req(
|
def _clean_req(
|
||||||
self, request: Request, method: Callable, results: TestResult
|
self, request: Request, method: Callable[..., Any], results: TestResult
|
||||||
) -> None:
|
) -> None:
|
||||||
"""stop the request from returning objects and records any errors"""
|
"""stop the request from returning objects and records any errors"""
|
||||||
|
|
||||||
|
|
@ -195,7 +201,7 @@ class ContractsManager:
|
||||||
request.errback = eb_wrapper
|
request.errback = eb_wrapper
|
||||||
|
|
||||||
|
|
||||||
def _create_testcase(method: Callable, desc: str) -> TestCase:
|
def _create_testcase(method: Callable[..., Any], desc: str) -> TestCase:
|
||||||
spider = method.__self__.name # type: ignore[attr-defined]
|
spider = method.__self__.name # type: ignore[attr-defined]
|
||||||
|
|
||||||
class ContractTestCase(TestCase):
|
class ContractTestCase(TestCase):
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import random
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from time import time
|
from time import monotonic
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from twisted.internet.defer import Deferred, inlineCallbacks
|
from twisted.internet.defer import Deferred, inlineCallbacks
|
||||||
|
|
@ -128,11 +128,12 @@ class Downloader:
|
||||||
) -> Generator[Deferred[Any], Any, Response | Request]:
|
) -> Generator[Deferred[Any], Any, Response | Request]:
|
||||||
self.active.add(request)
|
self.active.add(request)
|
||||||
try:
|
try:
|
||||||
return (
|
result: Response | Request = yield (
|
||||||
yield deferred_from_coro(
|
deferred_from_coro(
|
||||||
self.middleware.download_async(self._enqueue_request, request)
|
self.middleware.download_async(self._enqueue_request, request)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
return result
|
||||||
finally:
|
finally:
|
||||||
self.active.remove(request)
|
self.active.remove(request)
|
||||||
|
|
||||||
|
|
@ -163,7 +164,8 @@ class Downloader:
|
||||||
return key, self.slots[key]
|
return key, self.slots[key]
|
||||||
|
|
||||||
def get_slot_key(self, request: Request) -> str:
|
def get_slot_key(self, request: Request) -> str:
|
||||||
if (meta_slot := request.meta.get(self.DOWNLOAD_SLOT)) is not None:
|
meta_slot: str | None = request.meta.get(self.DOWNLOAD_SLOT)
|
||||||
|
if meta_slot is not None:
|
||||||
return meta_slot
|
return meta_slot
|
||||||
|
|
||||||
key = urlparse_cached(request).hostname or ""
|
key = urlparse_cached(request).hostname or ""
|
||||||
|
|
@ -196,7 +198,7 @@ class Downloader:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Delay queue processing if a download_delay is configured
|
# Delay queue processing if a download_delay is configured
|
||||||
now = time()
|
now = monotonic()
|
||||||
delay = slot.download_delay()
|
delay = slot.download_delay()
|
||||||
if delay:
|
if delay:
|
||||||
penalty = delay - now + slot.lastseen
|
penalty = delay - now + slot.lastseen
|
||||||
|
|
@ -265,7 +267,7 @@ class Downloader:
|
||||||
slot.close()
|
slot.close()
|
||||||
|
|
||||||
def _slot_gc(self, age: float = 60) -> None:
|
def _slot_gc(self, age: float = 60) -> None:
|
||||||
mintime = time() - age
|
mintime = monotonic() - age
|
||||||
for key, slot in list(self.slots.items()):
|
for key, slot in list(self.slots.items()):
|
||||||
if not slot.active and slot.lastseen + slot.delay < mintime:
|
if not slot.active and slot.lastseen + slot.delay < mintime:
|
||||||
self.slots.pop(key).close()
|
self.slots.pop(key).close()
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import warnings
|
import warnings
|
||||||
from contextlib import contextmanager
|
|
||||||
from typing import TYPE_CHECKING, Any, cast
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
|
|
||||||
from OpenSSL import SSL
|
from OpenSSL import SSL
|
||||||
from twisted.internet._sslverify import _setAcceptableProtocols
|
|
||||||
from twisted.internet.ssl import (
|
from twisted.internet.ssl import (
|
||||||
AcceptableCiphers,
|
AcceptableCiphers,
|
||||||
CertificateOptions,
|
CertificateOptions,
|
||||||
|
TLSVersion,
|
||||||
optionsForClientTLS,
|
optionsForClientTLS,
|
||||||
)
|
)
|
||||||
from twisted.web.client import BrowserLikePolicyForHTTPS
|
from twisted.web.client import BrowserLikePolicyForHTTPS
|
||||||
|
|
@ -17,17 +16,18 @@ from zope.interface.declarations import implementer
|
||||||
from zope.interface.verify import verifyObject
|
from zope.interface.verify import verifyObject
|
||||||
|
|
||||||
from scrapy.core.downloader.tls import (
|
from scrapy.core.downloader.tls import (
|
||||||
DEFAULT_CIPHERS,
|
_TWISTED_VERSION_MAP,
|
||||||
|
_openssl_methods,
|
||||||
_ScrapyClientTLSOptions,
|
_ScrapyClientTLSOptions,
|
||||||
openssl_methods,
|
_ScrapyClientTLSOptions26,
|
||||||
)
|
)
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning
|
from scrapy.exceptions import ScrapyDeprecationWarning
|
||||||
|
from scrapy.utils._deps_compat import TWISTED_TLS_NEW_IMPL
|
||||||
from scrapy.utils.deprecate import create_deprecated_class
|
from scrapy.utils.deprecate import create_deprecated_class
|
||||||
from scrapy.utils.misc import build_from_crawler, load_object
|
from scrapy.utils.misc import build_from_crawler, load_object
|
||||||
|
from scrapy.utils.ssl import _get_cert_options_version_kwargs, _get_tls_version_limits
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Generator
|
|
||||||
|
|
||||||
from twisted.internet._sslverify import ClientTLSOptions
|
from twisted.internet._sslverify import ClientTLSOptions
|
||||||
|
|
||||||
# typing.Self requires Python 3.11
|
# typing.Self requires Python 3.11
|
||||||
|
|
@ -37,61 +37,53 @@ if TYPE_CHECKING:
|
||||||
from scrapy.settings import BaseSettings
|
from scrapy.settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def _filter_method_warning() -> Generator[None]:
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
# Twisted deprecation, https://github.com/scrapy/scrapy/issues/3288
|
|
||||||
warnings.filterwarnings(
|
|
||||||
"ignore",
|
|
||||||
message=r"Passing method to twisted\.internet\.ssl\.CertificateOptions",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
)
|
|
||||||
yield
|
|
||||||
|
|
||||||
|
|
||||||
@implementer(IPolicyForHTTPS)
|
@implementer(IPolicyForHTTPS)
|
||||||
class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):
|
class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):
|
||||||
"""Non-peer-certificate verifying HTTPS context factory.
|
"""Non-peer-certificate verifying HTTPS context factory.
|
||||||
|
|
||||||
Default OpenSSL method is ``TLS_METHOD`` (also called ``SSLv23_METHOD``)
|
Uses :setting:`DOWNLOADER_CLIENT_TLS_CIPHERS`,
|
||||||
which allows TLS protocol negotiation.
|
:setting:`DOWNLOAD_TLS_MIN_VERSION` and :setting:`DOWNLOAD_TLS_MAX_VERSION`
|
||||||
|
to configure the :class:`~twisted.internet.ssl.CertificateOptions`
|
||||||
|
instance.
|
||||||
|
|
||||||
The purpose of this custom class is to provide a ``creatorForNetloc()``
|
The purpose of this custom class is to provide a ``creatorForNetloc()``
|
||||||
method that returns a ``_ScrapyClientTLSOptions`` instance configured based
|
method that returns:
|
||||||
on TLS settings provided to the factory.
|
|
||||||
|
- a ``_ScrapyClientTLSOptions26`` or ``_ScrapyClientTLSOptions`` instance
|
||||||
|
configured based on TLS settings provided to the factory (when the
|
||||||
|
certificate verification is disabled);
|
||||||
|
- a result of ``optionsForClientTLS()`` called with those TLS settings
|
||||||
|
(when the certificate verification is enabled).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
method: int = SSL.SSLv23_METHOD, # noqa: S503
|
method: int | None = SSL.SSLv23_METHOD, # noqa: S503
|
||||||
tls_verbose_logging: bool = False,
|
tls_verbose_logging: bool = False,
|
||||||
tls_ciphers: str | None = None,
|
tls_ciphers: str | None = None,
|
||||||
*args: Any,
|
*args: Any,
|
||||||
verify_certificates: bool = False,
|
verify_certificates: bool = False,
|
||||||
|
tls_min_version: TLSVersion | None = None,
|
||||||
|
tls_max_version: TLSVersion | None = None,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
):
|
):
|
||||||
super().__init__(*args, **kwargs) # type: ignore[no-untyped-call]
|
super().__init__(*args, **kwargs) # type: ignore[no-untyped-call]
|
||||||
self._ssl_method: int = method
|
self._ssl_method: int | None = method
|
||||||
|
self.tls_min_version: TLSVersion | None = tls_min_version
|
||||||
|
self.tls_max_version: TLSVersion | None = tls_max_version
|
||||||
self.tls_verbose_logging: bool = tls_verbose_logging # unused
|
self.tls_verbose_logging: bool = tls_verbose_logging # unused
|
||||||
self.tls_ciphers: AcceptableCiphers
|
self.tls_ciphers: AcceptableCiphers | None = (
|
||||||
if tls_ciphers:
|
AcceptableCiphers.fromOpenSSLCipherString(tls_ciphers)
|
||||||
self.tls_ciphers = AcceptableCiphers.fromOpenSSLCipherString(tls_ciphers)
|
if tls_ciphers
|
||||||
else:
|
else None
|
||||||
self.tls_ciphers = DEFAULT_CIPHERS
|
)
|
||||||
with _filter_method_warning():
|
|
||||||
self._certificate_options = CertificateOptions(
|
|
||||||
method=self._ssl_method,
|
|
||||||
fixBrokenPeers=True,
|
|
||||||
acceptableCiphers=self.tls_ciphers,
|
|
||||||
)
|
|
||||||
self._ctx = self._get_context()
|
|
||||||
self._verify_certificates = verify_certificates
|
self._verify_certificates = verify_certificates
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_crawler(
|
def from_crawler(
|
||||||
cls,
|
cls,
|
||||||
crawler: Crawler,
|
crawler: Crawler,
|
||||||
method: int = SSL.SSLv23_METHOD, # noqa: S503
|
method: int | None = SSL.SSLv23_METHOD, # noqa: S503
|
||||||
*args: Any,
|
*args: Any,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> Self:
|
) -> Self:
|
||||||
|
|
@ -99,41 +91,74 @@ class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):
|
||||||
"DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING"
|
"DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING"
|
||||||
)
|
)
|
||||||
tls_ciphers: str | None = crawler.settings["DOWNLOADER_CLIENT_TLS_CIPHERS"]
|
tls_ciphers: str | None = crawler.settings["DOWNLOADER_CLIENT_TLS_CIPHERS"]
|
||||||
|
# DOWNLOADER_CLIENT_TLS_METHOD reading and handling should be also moved here
|
||||||
|
# when the deprecated load_context_factory_from_settings() is removed
|
||||||
|
tls_min_ver, tls_max_ver = _get_tls_version_limits(
|
||||||
|
crawler.settings, _TWISTED_VERSION_MAP.__getitem__
|
||||||
|
)
|
||||||
|
if tls_min_ver or tls_max_ver:
|
||||||
|
method = None
|
||||||
verify_certificates = crawler.settings.getbool("DOWNLOAD_VERIFY_CERTIFICATES")
|
verify_certificates = crawler.settings.getbool("DOWNLOAD_VERIFY_CERTIFICATES")
|
||||||
return cls( # type: ignore[misc]
|
return cls( # type: ignore[misc]
|
||||||
*args,
|
*args,
|
||||||
method=method,
|
method=method,
|
||||||
tls_verbose_logging=tls_verbose_logging,
|
tls_verbose_logging=tls_verbose_logging,
|
||||||
tls_ciphers=tls_ciphers,
|
tls_ciphers=tls_ciphers,
|
||||||
|
tls_min_version=tls_min_ver,
|
||||||
|
tls_max_version=tls_max_ver,
|
||||||
verify_certificates=verify_certificates,
|
verify_certificates=verify_certificates,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# should be removed together with ScrapyClientContextFactory
|
||||||
def getCertificateOptions(self) -> CertificateOptions: # pragma: no cover
|
def getCertificateOptions(self) -> CertificateOptions: # pragma: no cover
|
||||||
return self._certificate_options
|
return self._get_cert_options()
|
||||||
|
|
||||||
# kept for old-style HTTP/1.0 downloader context twisted calls,
|
def _get_cert_options(self) -> CertificateOptions:
|
||||||
# e.g. connectSSL()
|
return _ScrapyCertificateOptions(**self._get_cert_options_kwargs())
|
||||||
def getContext(self, hostname: Any = None, port: Any = None) -> SSL.Context:
|
|
||||||
return self._ctx
|
def _get_cert_options_kwargs(self) -> dict[str, Any]:
|
||||||
|
kwargs: dict[str, Any] = {
|
||||||
|
"fixBrokenPeers": True,
|
||||||
|
"acceptableCiphers": self.tls_ciphers,
|
||||||
|
}
|
||||||
|
if self.tls_min_version or self.tls_max_version:
|
||||||
|
kwargs.update(
|
||||||
|
_get_cert_options_version_kwargs(
|
||||||
|
self.tls_min_version, self.tls_max_version
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# when ScrapyClientContextFactory is removed self._ssl_method can just be None by default
|
||||||
|
elif self._ssl_method != SSL.SSLv23_METHOD:
|
||||||
|
kwargs["method"] = self._ssl_method
|
||||||
|
return kwargs
|
||||||
|
|
||||||
|
# should be removed together with ScrapyClientContextFactory
|
||||||
|
def getContext(
|
||||||
|
self, hostname: Any = None, port: Any = None
|
||||||
|
) -> SSL.Context: # pragma: no cover
|
||||||
|
return self._get_context()
|
||||||
|
|
||||||
def _get_context(self) -> SSL.Context:
|
def _get_context(self) -> SSL.Context:
|
||||||
ctx = self._certificate_options.getContext()
|
return self._get_cert_options().getContext()
|
||||||
ctx.set_options(0x4) # OP_LEGACY_SERVER_CONNECT
|
|
||||||
return ctx
|
|
||||||
|
|
||||||
def creatorForNetloc(self, hostname: bytes, port: int) -> ClientTLSOptions:
|
def creatorForNetloc(self, hostname: bytes, port: int) -> ClientTLSOptions:
|
||||||
if not self._verify_certificates:
|
if not self._verify_certificates:
|
||||||
return _ScrapyClientTLSOptions(hostname.decode("ascii"), self._ctx) # type: ignore[no-untyped-call]
|
# Our options class is needed to skip verification errors
|
||||||
# Note that this doesn't use self._ctx
|
if TWISTED_TLS_NEW_IMPL:
|
||||||
with _filter_method_warning():
|
return _ScrapyClientTLSOptions26(
|
||||||
return optionsForClientTLS(
|
self._get_cert_options()._makeTLSConnection,
|
||||||
hostname=hostname.decode("ascii"),
|
hostname.decode("ascii"),
|
||||||
extraCertificateOptions={
|
)
|
||||||
"method": self._ssl_method,
|
return _ScrapyClientTLSOptions(
|
||||||
"acceptableCiphers": self.tls_ciphers,
|
hostname.decode("ascii"), # type: ignore[arg-type]
|
||||||
},
|
self._get_context(), # type: ignore[arg-type]
|
||||||
)
|
)
|
||||||
|
# Otherwise use the normal Twisted function.
|
||||||
|
return optionsForClientTLS( # type: ignore[no-any-return]
|
||||||
|
hostname=hostname.decode("ascii"),
|
||||||
|
extraCertificateOptions=self._get_cert_options_kwargs(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
ScrapyClientContextFactory = create_deprecated_class(
|
ScrapyClientContextFactory = create_deprecated_class(
|
||||||
|
|
@ -158,12 +183,6 @@ class BrowserLikeContextFactory(_ScrapyClientContextFactory):
|
||||||
:meth:`creatorForNetloc` is the same as
|
:meth:`creatorForNetloc` is the same as
|
||||||
:class:`~twisted.web.client.BrowserLikePolicyForHTTPS` except this context
|
:class:`~twisted.web.client.BrowserLikePolicyForHTTPS` except this context
|
||||||
factory allows setting the TLS/SSL method to use.
|
factory allows setting the TLS/SSL method to use.
|
||||||
|
|
||||||
The default OpenSSL method is ``TLS_METHOD`` (also called
|
|
||||||
``SSLv23_METHOD``) which allows TLS protocol negotiation.
|
|
||||||
|
|
||||||
As this overrides the parent ``creatorForNetloc()`` method, only
|
|
||||||
``self._ssl_method`` is used from the parent class.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args: Any, **kwargs: Any):
|
def __init__(self, *args: Any, **kwargs: Any):
|
||||||
|
|
@ -177,11 +196,10 @@ class BrowserLikeContextFactory(_ScrapyClientContextFactory):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def creatorForNetloc(self, hostname: bytes, port: int) -> ClientTLSOptions:
|
def creatorForNetloc(self, hostname: bytes, port: int) -> ClientTLSOptions:
|
||||||
with _filter_method_warning():
|
return optionsForClientTLS( # type: ignore[no-any-return]
|
||||||
return optionsForClientTLS(
|
hostname=hostname.decode("ascii"),
|
||||||
hostname=hostname.decode("ascii"),
|
extraCertificateOptions=self._get_cert_options_kwargs(),
|
||||||
extraCertificateOptions={"method": self._ssl_method},
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@implementer(IPolicyForHTTPS)
|
@implementer(IPolicyForHTTPS)
|
||||||
|
|
@ -194,8 +212,25 @@ class _AcceptableProtocolsContextFactory:
|
||||||
the acceptable protocols on the :class:`.ClientTLSOptions` instance
|
the acceptable protocols on the :class:`.ClientTLSOptions` instance
|
||||||
returned by it. It's only needed because we support custom factories via
|
returned by it. It's only needed because we support custom factories via
|
||||||
:setting:`DOWNLOADER_CLIENTCONTEXTFACTORY`.
|
:setting:`DOWNLOADER_CLIENTCONTEXTFACTORY`.
|
||||||
|
|
||||||
|
It's a no-op on Twisted 26.4.0+, though using it with custom
|
||||||
|
factories on those Twisted versions may be not enough for HTTP/2 support.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Something needs to call set_alpn_protos() for ALPN to work.
|
||||||
|
#
|
||||||
|
# Twisted < 26.4.0 does it in OpenSSLCertificateOptions._makeContext()
|
||||||
|
# (requires passing acceptableProtocols from the factory to
|
||||||
|
# OpenSSLCertificateOptions) and in TLSMemoryBIOFactory._createConnection()
|
||||||
|
# based on H2ClientFactory.acceptableProtocols (too late, it seems).
|
||||||
|
#
|
||||||
|
# Newer Twisted does it in OpenSSLCertificateOptions._makeContext() as
|
||||||
|
# well, and in OpenSSLCertificateOptions._makeTLSConnection() based on
|
||||||
|
# H2ClientFactory.acceptableProtocols (which now works).
|
||||||
|
#
|
||||||
|
# When we drop DOWNLOADER_CLIENTCONTEXTFACTORY it looks like we can replace
|
||||||
|
# all of this with _ScrapyClientContextFactory.acceptableProtocols.
|
||||||
|
|
||||||
def __init__(self, context_factory: Any, acceptable_protocols: list[bytes]):
|
def __init__(self, context_factory: Any, acceptable_protocols: list[bytes]):
|
||||||
verifyObject(IPolicyForHTTPS, context_factory)
|
verifyObject(IPolicyForHTTPS, context_factory)
|
||||||
self._wrapped_context_factory: Any = context_factory
|
self._wrapped_context_factory: Any = context_factory
|
||||||
|
|
@ -205,7 +240,12 @@ class _AcceptableProtocolsContextFactory:
|
||||||
options: ClientTLSOptions = self._wrapped_context_factory.creatorForNetloc(
|
options: ClientTLSOptions = self._wrapped_context_factory.creatorForNetloc(
|
||||||
hostname, port
|
hostname, port
|
||||||
)
|
)
|
||||||
_setAcceptableProtocols(options._ctx, self._acceptable_protocols)
|
if not TWISTED_TLS_NEW_IMPL:
|
||||||
|
from twisted.internet._sslverify import ( # type: ignore[attr-defined] # noqa: PLC0415 # pylint: disable=no-name-in-module
|
||||||
|
_setAcceptableProtocols,
|
||||||
|
)
|
||||||
|
|
||||||
|
_setAcceptableProtocols(options._ctx, self._acceptable_protocols) # type: ignore[attr-defined]
|
||||||
return options
|
return options
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -217,11 +257,33 @@ AcceptableProtocolsContextFactory = create_deprecated_class(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class _ScrapyCertificateOptions(CertificateOptions):
|
||||||
|
"""A wrapper needed to add flags to the SSL context before it's used."""
|
||||||
|
|
||||||
|
def _makeContext(self, skipCiphers: bool = False) -> SSL.Context:
|
||||||
|
if TWISTED_TLS_NEW_IMPL:
|
||||||
|
ctx = super()._makeContext(skipCiphers)
|
||||||
|
else:
|
||||||
|
ctx = super()._makeContext()
|
||||||
|
ctx.set_options(0x4) # OP_LEGACY_SERVER_CONNECT
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
def _load_context_factory_from_settings(crawler: Crawler) -> IPolicyForHTTPS:
|
def _load_context_factory_from_settings(crawler: Crawler) -> IPolicyForHTTPS:
|
||||||
"""Create an instance of :setting:`DOWNLOADER_CLIENTCONTEXTFACTORY`.
|
"""Create an instance of :setting:`DOWNLOADER_CLIENTCONTEXTFACTORY`.
|
||||||
|
|
||||||
Also passes values of other relevant settings to the factory class.
|
Also passes values of other relevant settings to the factory class.
|
||||||
"""
|
"""
|
||||||
|
tls_method_setting: str = crawler.settings["DOWNLOADER_CLIENT_TLS_METHOD"]
|
||||||
|
if tls_method_setting != "TLS":
|
||||||
|
warnings.warn(
|
||||||
|
"Setting DOWNLOADER_CLIENT_TLS_METHOD to a non-default value is"
|
||||||
|
" deprecated, please use DOWNLOAD_TLS_MIN_VERSION and/or"
|
||||||
|
" DOWNLOAD_TLS_MAX_VERSION instead.",
|
||||||
|
ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
tls_method = _openssl_methods[tls_method_setting]
|
||||||
if crawler.settings["DOWNLOADER_CLIENTCONTEXTFACTORY"] == "SENTINEL":
|
if crawler.settings["DOWNLOADER_CLIENTCONTEXTFACTORY"] == "SENTINEL":
|
||||||
context_factory_cls = _ScrapyClientContextFactory
|
context_factory_cls = _ScrapyClientContextFactory
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
|
|
@ -233,13 +295,12 @@ def _load_context_factory_from_settings(crawler: Crawler) -> IPolicyForHTTPS:
|
||||||
context_factory_cls = load_object(
|
context_factory_cls = load_object(
|
||||||
crawler.settings["DOWNLOADER_CLIENTCONTEXTFACTORY"]
|
crawler.settings["DOWNLOADER_CLIENTCONTEXTFACTORY"]
|
||||||
)
|
)
|
||||||
ssl_method = openssl_methods[crawler.settings.get("DOWNLOADER_CLIENT_TLS_METHOD")]
|
|
||||||
return cast(
|
return cast(
|
||||||
"IPolicyForHTTPS",
|
"IPolicyForHTTPS",
|
||||||
build_from_crawler(
|
build_from_crawler(
|
||||||
context_factory_cls,
|
context_factory_cls,
|
||||||
crawler,
|
crawler,
|
||||||
method=ssl_method,
|
method=tls_method,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from abc import ABC
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from .base import BaseDownloadHandler
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from scrapy.crawler import Crawler
|
||||||
|
|
||||||
|
|
||||||
|
class BaseHttpDownloadHandler(BaseDownloadHandler, ABC):
|
||||||
|
"""Base class for built-in HTTP download handlers."""
|
||||||
|
|
||||||
|
def __init__(self, crawler: Crawler):
|
||||||
|
super().__init__(crawler)
|
||||||
|
self._default_maxsize: int = crawler.settings.getint("DOWNLOAD_MAXSIZE")
|
||||||
|
self._default_warnsize: int = crawler.settings.getint("DOWNLOAD_WARNSIZE")
|
||||||
|
self._fail_on_dataloss: bool = crawler.settings.getbool(
|
||||||
|
"DOWNLOAD_FAIL_ON_DATALOSS"
|
||||||
|
)
|
||||||
|
self._tls_verbose_logging: bool = crawler.settings.getbool(
|
||||||
|
"DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING"
|
||||||
|
)
|
||||||
|
self._fail_on_dataloss_warned: bool = False
|
||||||
|
|
@ -0,0 +1,315 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from io import BytesIO
|
||||||
|
from typing import TYPE_CHECKING, Any, ClassVar, Generic, NoReturn, TypedDict, TypeVar
|
||||||
|
from urllib.parse import quote, urlsplit
|
||||||
|
|
||||||
|
from scrapy import Request, signals
|
||||||
|
from scrapy.exceptions import (
|
||||||
|
DownloadCancelledError,
|
||||||
|
NotConfigured,
|
||||||
|
ResponseDataLossError,
|
||||||
|
)
|
||||||
|
from scrapy.utils._download_handlers import (
|
||||||
|
check_stop_download,
|
||||||
|
get_dataloss_msg,
|
||||||
|
get_maxsize_msg,
|
||||||
|
get_warnsize_msg,
|
||||||
|
make_response,
|
||||||
|
normalize_bind_address,
|
||||||
|
)
|
||||||
|
from scrapy.utils.asyncio import is_asyncio_available
|
||||||
|
from scrapy.utils.url import add_http_if_no_scheme
|
||||||
|
|
||||||
|
from ._base_http import BaseHttpDownloadHandler
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import AsyncIterable
|
||||||
|
from contextlib import AbstractAsyncContextManager
|
||||||
|
from ipaddress import IPv4Address, IPv6Address
|
||||||
|
|
||||||
|
from _typeshed import SizedBuffer
|
||||||
|
|
||||||
|
# typing.NotRequired requires Python 3.11
|
||||||
|
from typing_extensions import NotRequired
|
||||||
|
|
||||||
|
from scrapy.crawler import Crawler
|
||||||
|
from scrapy.http import Headers, Response
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_ResponseT = TypeVar("_ResponseT")
|
||||||
|
|
||||||
|
|
||||||
|
class _BaseResponseArgs(TypedDict):
|
||||||
|
status: int
|
||||||
|
url: str
|
||||||
|
headers: Headers
|
||||||
|
certificate: NotRequired[Any]
|
||||||
|
ip_address: NotRequired[IPv4Address | IPv6Address | None]
|
||||||
|
protocol: str | None
|
||||||
|
|
||||||
|
|
||||||
|
class BaseStreamingDownloadHandler(BaseHttpDownloadHandler, ABC, Generic[_ResponseT]):
|
||||||
|
"""A base class for HTTP download handlers that follow the streaming logic flow."""
|
||||||
|
|
||||||
|
_DEFAULT_CONNECT_TIMEOUT: ClassVar[float] = 10
|
||||||
|
experimental: ClassVar[bool] = False
|
||||||
|
requires_asyncio: ClassVar[bool] = True
|
||||||
|
# require subclasses to disable proxies explicitly with an explanation
|
||||||
|
supports_proxies: ClassVar[bool] = True
|
||||||
|
supports_per_request_bindaddress: ClassVar[bool] = False
|
||||||
|
|
||||||
|
def __init__(self, crawler: Crawler):
|
||||||
|
if self.requires_asyncio and not is_asyncio_available(): # pragma: no cover
|
||||||
|
raise NotConfigured(
|
||||||
|
f"{type(self).__name__} requires the asyncio support. Make"
|
||||||
|
f" sure that you have either enabled the asyncio Twisted"
|
||||||
|
f" reactor in the TWISTED_REACTOR setting or disabled the"
|
||||||
|
f" TWISTED_REACTOR_ENABLED setting. See the asyncio documentation"
|
||||||
|
f" of Scrapy for more information."
|
||||||
|
)
|
||||||
|
self._check_deps_installed()
|
||||||
|
super().__init__(crawler)
|
||||||
|
if self.experimental:
|
||||||
|
logger.warning(
|
||||||
|
f"{type(self).__name__} is experimental and is not recommended for production use."
|
||||||
|
)
|
||||||
|
self._bind_address = normalize_bind_address(
|
||||||
|
crawler.settings.get("DOWNLOAD_BIND_ADDRESS")
|
||||||
|
)
|
||||||
|
self._proxy_auth_encoding: str = crawler.settings.get("HTTPPROXY_AUTH_ENCODING")
|
||||||
|
# these are useful for many handlers but used in different ways by them
|
||||||
|
self._pool_size_total: int = crawler.settings.getint("CONCURRENT_REQUESTS")
|
||||||
|
self._pool_size_per_host: int = crawler.settings.getint(
|
||||||
|
"CONCURRENT_REQUESTS_PER_DOMAIN"
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@abstractmethod
|
||||||
|
def _check_deps_installed() -> None:
|
||||||
|
"""Raise NotConfigured if the required deps are not installed."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _make_request(
|
||||||
|
self, request: Request, timeout: float
|
||||||
|
) -> AbstractAsyncContextManager[_ResponseT]:
|
||||||
|
"""Return an async context manager yielding the library-specific response.
|
||||||
|
|
||||||
|
Exceptions raised by the library should be reraised as Scrapy-specific ones.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@abstractmethod
|
||||||
|
def _extract_headers(response: _ResponseT) -> Headers:
|
||||||
|
"""Convert library-specific response headers to a
|
||||||
|
:class:`~scrapy.http.headers.Headers` object."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@abstractmethod
|
||||||
|
def _build_base_response_args(
|
||||||
|
response: _ResponseT, request: Request, headers: Headers
|
||||||
|
) -> _BaseResponseArgs:
|
||||||
|
"""Build kwargs for :func:`scrapy.utils._download_handlers.make_response`."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@abstractmethod
|
||||||
|
def _iter_body_chunks(response: _ResponseT) -> AsyncIterable[SizedBuffer]:
|
||||||
|
"""Return an async iterable yielding body chunks from the response."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@abstractmethod
|
||||||
|
def _is_dataloss_exception(exc: Exception) -> bool:
|
||||||
|
"""Return True if ``exc`` represents dataloss."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def _log_tls_info(self, response: _ResponseT, request: Request) -> None:
|
||||||
|
"""Log TLS connection details, if possible."""
|
||||||
|
|
||||||
|
async def download_request(self, request: Request) -> Response:
|
||||||
|
if not self.supports_proxies and request.meta.get("proxy"):
|
||||||
|
raise NotImplementedError(f"{type(self).__name__} doesn't support proxies.")
|
||||||
|
if not self.supports_per_request_bindaddress and request.meta.get(
|
||||||
|
"bindaddress"
|
||||||
|
):
|
||||||
|
logger.error(
|
||||||
|
f"The 'bindaddress' request meta key is not supported by"
|
||||||
|
f" {type(self).__name__} and will be ignored."
|
||||||
|
)
|
||||||
|
timeout: float = request.meta.get(
|
||||||
|
"download_timeout", self._DEFAULT_CONNECT_TIMEOUT
|
||||||
|
)
|
||||||
|
start_time = time.monotonic()
|
||||||
|
async with self._make_request(request, timeout) as response:
|
||||||
|
request.meta["download_latency"] = time.monotonic() - start_time
|
||||||
|
return await self._read_response(response, request)
|
||||||
|
|
||||||
|
async def _read_response(self, response: _ResponseT, request: Request) -> Response:
|
||||||
|
maxsize: int = request.meta.get("download_maxsize", self._default_maxsize)
|
||||||
|
warnsize: int = request.meta.get("download_warnsize", self._default_warnsize)
|
||||||
|
|
||||||
|
headers = self._extract_headers(response)
|
||||||
|
content_length = headers.get("Content-Length")
|
||||||
|
expected_size = int(content_length) if content_length is not None else None
|
||||||
|
if maxsize and expected_size and expected_size > maxsize:
|
||||||
|
self._cancel_maxsize(expected_size, maxsize, request, expected=True)
|
||||||
|
|
||||||
|
reached_warnsize = False
|
||||||
|
if warnsize and expected_size and expected_size > warnsize:
|
||||||
|
reached_warnsize = True
|
||||||
|
logger.warning(
|
||||||
|
get_warnsize_msg(expected_size, warnsize, request, expected=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
make_response_base_args = self._build_base_response_args(
|
||||||
|
response, request, headers
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._tls_verbose_logging:
|
||||||
|
self._log_tls_info(response, request)
|
||||||
|
|
||||||
|
if stop_download := check_stop_download(
|
||||||
|
signals.headers_received,
|
||||||
|
self.crawler,
|
||||||
|
request,
|
||||||
|
headers=headers,
|
||||||
|
body_length=expected_size,
|
||||||
|
):
|
||||||
|
return make_response(
|
||||||
|
**make_response_base_args,
|
||||||
|
stop_download=stop_download,
|
||||||
|
)
|
||||||
|
|
||||||
|
response_body = BytesIO()
|
||||||
|
bytes_received = 0
|
||||||
|
try:
|
||||||
|
async for chunk in self._iter_body_chunks(response):
|
||||||
|
response_body.write(chunk)
|
||||||
|
bytes_received += len(chunk)
|
||||||
|
|
||||||
|
if stop_download := check_stop_download(
|
||||||
|
signals.bytes_received, self.crawler, request, data=chunk
|
||||||
|
):
|
||||||
|
return make_response(
|
||||||
|
**make_response_base_args,
|
||||||
|
body=response_body.getvalue(),
|
||||||
|
stop_download=stop_download,
|
||||||
|
)
|
||||||
|
|
||||||
|
if maxsize and bytes_received > maxsize:
|
||||||
|
response_body.truncate(0)
|
||||||
|
self._cancel_maxsize(
|
||||||
|
bytes_received, maxsize, request, expected=False
|
||||||
|
)
|
||||||
|
|
||||||
|
if warnsize and bytes_received > warnsize and not reached_warnsize:
|
||||||
|
reached_warnsize = True
|
||||||
|
logger.warning(
|
||||||
|
get_warnsize_msg(
|
||||||
|
bytes_received, warnsize, request, expected=False
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
if not self._is_dataloss_exception(e):
|
||||||
|
raise
|
||||||
|
fail_on_dataloss: bool = request.meta.get(
|
||||||
|
"download_fail_on_dataloss", self._fail_on_dataloss
|
||||||
|
)
|
||||||
|
if not fail_on_dataloss:
|
||||||
|
return make_response(
|
||||||
|
**make_response_base_args,
|
||||||
|
body=response_body.getvalue(),
|
||||||
|
flags=["dataloss"],
|
||||||
|
)
|
||||||
|
if not self._fail_on_dataloss_warned:
|
||||||
|
logger.warning(get_dataloss_msg(request.url))
|
||||||
|
self._fail_on_dataloss_warned = True
|
||||||
|
raise ResponseDataLossError(str(e)) from e
|
||||||
|
|
||||||
|
return make_response(
|
||||||
|
**make_response_base_args,
|
||||||
|
body=response_body.getvalue(),
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _request_headers(request: Request) -> Headers:
|
||||||
|
"""Get a prepared copy of the request headers.
|
||||||
|
|
||||||
|
This removes the Proxy-Authorization header.
|
||||||
|
"""
|
||||||
|
headers = request.headers.copy()
|
||||||
|
headers.pop(b"Proxy-Authorization", None)
|
||||||
|
return headers
|
||||||
|
|
||||||
|
def _get_bind_address_host(self) -> str | None:
|
||||||
|
"""Return the host portion of the bind address.
|
||||||
|
|
||||||
|
Needed for handlers that don't support the bind port.
|
||||||
|
"""
|
||||||
|
if self._bind_address is None:
|
||||||
|
return None
|
||||||
|
host, port = self._bind_address
|
||||||
|
if port != 0:
|
||||||
|
logger.warning(
|
||||||
|
"DOWNLOAD_BIND_ADDRESS specifies a port (%s), but %s does not "
|
||||||
|
"support binding to a specific local port. Ignoring the port "
|
||||||
|
"and binding only to %r.",
|
||||||
|
port,
|
||||||
|
type(self).__name__,
|
||||||
|
host,
|
||||||
|
)
|
||||||
|
return host
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _cancel_maxsize(
|
||||||
|
size: int, limit: int, request: Request, *, expected: bool
|
||||||
|
) -> NoReturn:
|
||||||
|
warning_msg = get_maxsize_msg(size, limit, request, expected=expected)
|
||||||
|
logger.warning(warning_msg)
|
||||||
|
raise DownloadCancelledError(warning_msg)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _extract_proxy(request: Request) -> tuple[str | None, str | None]:
|
||||||
|
"""Return a tuple of the proxy URL with a scheme and the value of the
|
||||||
|
Proxy-Authorization header.
|
||||||
|
|
||||||
|
This is useful for handlers that take the proxy headers separately.
|
||||||
|
"""
|
||||||
|
proxy: str | None = request.meta.get("proxy")
|
||||||
|
if not proxy:
|
||||||
|
return None, None
|
||||||
|
proxy = add_http_if_no_scheme(proxy)
|
||||||
|
auth_header: bytes | None = request.headers.get(b"Proxy-Authorization")
|
||||||
|
return proxy, auth_header.decode("ascii") if auth_header else None
|
||||||
|
|
||||||
|
def _extract_proxy_url_with_creds(self, request: Request) -> str | None:
|
||||||
|
"""Return the proxy URL with the userinfo added based on the
|
||||||
|
Proxy-Authorization header.
|
||||||
|
|
||||||
|
This is useful for handlers that cannot take the proxy headers
|
||||||
|
separately.
|
||||||
|
"""
|
||||||
|
proxy_url, auth_header = self._extract_proxy(request)
|
||||||
|
if proxy_url is None or auth_header is None:
|
||||||
|
return proxy_url
|
||||||
|
scheme, token = auth_header.split(" ", 1)
|
||||||
|
if scheme != "Basic":
|
||||||
|
raise ValueError(
|
||||||
|
f"Expected Basic auth in Proxy-Authorization, got {scheme}"
|
||||||
|
)
|
||||||
|
user, password = (
|
||||||
|
base64.b64decode(token).decode(self._proxy_auth_encoding).split(":", 1)
|
||||||
|
)
|
||||||
|
parts = urlsplit(proxy_url)
|
||||||
|
netloc = f"{quote(user)}:{quote(password)}@{parts.netloc}"
|
||||||
|
return parts._replace(netloc=netloc).geturl()
|
||||||
|
|
@ -3,131 +3,166 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import logging
|
|
||||||
import ssl
|
import ssl
|
||||||
from http.cookiejar import Cookie, CookieJar
|
from contextlib import asynccontextmanager
|
||||||
from io import BytesIO
|
from socket import gaierror
|
||||||
from typing import TYPE_CHECKING, Any, NoReturn, TypedDict
|
from typing import TYPE_CHECKING, ClassVar
|
||||||
|
|
||||||
from scrapy import Request, signals
|
|
||||||
from scrapy.exceptions import (
|
from scrapy.exceptions import (
|
||||||
CannotResolveHostError,
|
CannotResolveHostError,
|
||||||
DownloadCancelledError,
|
|
||||||
DownloadConnectionRefusedError,
|
DownloadConnectionRefusedError,
|
||||||
DownloadFailedError,
|
DownloadFailedError,
|
||||||
DownloadTimeoutError,
|
DownloadTimeoutError,
|
||||||
NotConfigured,
|
NotConfigured,
|
||||||
ResponseDataLossError,
|
|
||||||
UnsupportedURLSchemeError,
|
UnsupportedURLSchemeError,
|
||||||
)
|
)
|
||||||
from scrapy.http import Headers, Response
|
from scrapy.http import Headers
|
||||||
from scrapy.utils._download_handlers import (
|
from scrapy.utils._download_handlers import NullCookieJar
|
||||||
BaseHttpDownloadHandler,
|
from scrapy.utils.python import _iter_exc_causes
|
||||||
check_stop_download,
|
from scrapy.utils.ssl import (
|
||||||
get_dataloss_msg,
|
_log_sslobj_debug_info,
|
||||||
get_maxsize_msg,
|
_make_insecure_ssl_ctx,
|
||||||
get_warnsize_msg,
|
_make_ssl_context,
|
||||||
make_response,
|
|
||||||
normalize_bind_address,
|
|
||||||
)
|
)
|
||||||
from scrapy.utils.asyncio import is_asyncio_available
|
|
||||||
from scrapy.utils.ssl import _log_sslobj_debug_info, _make_ssl_context
|
from ._base_streaming import BaseStreamingDownloadHandler, _BaseResponseArgs
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from contextlib import AbstractAsyncContextManager
|
from collections.abc import AsyncIterator
|
||||||
from http.client import HTTPResponse
|
|
||||||
from ipaddress import IPv4Address, IPv6Address
|
|
||||||
from urllib.request import Request as ULRequest
|
|
||||||
|
|
||||||
from httpcore import AsyncNetworkStream
|
from httpcore import AsyncNetworkStream
|
||||||
|
|
||||||
|
from scrapy import Request
|
||||||
from scrapy.crawler import Crawler
|
from scrapy.crawler import Crawler
|
||||||
|
|
||||||
|
|
||||||
|
HAS_SOCKS = HAS_HTTP2 = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import httpx
|
import httpx
|
||||||
except ImportError:
|
except ImportError:
|
||||||
httpx = None # type: ignore[assignment]
|
httpx = None # type: ignore[assignment]
|
||||||
|
else:
|
||||||
|
# a small hack to avoid importing these optional extras unconditionally
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
DOWNLOAD_FAILED_EXCEPTIONS: tuple[type[BaseException], ...] = (
|
||||||
|
httpx.RequestError,
|
||||||
|
httpx.InvalidURL,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import h2.exceptions
|
||||||
|
|
||||||
class _BaseResponseArgs(TypedDict):
|
HAS_HTTP2 = True
|
||||||
status: int
|
DOWNLOAD_FAILED_EXCEPTIONS += (h2.exceptions.InvalidBodyLengthError,)
|
||||||
url: str
|
except ImportError: # pragma: no cover
|
||||||
headers: Headers
|
|
||||||
ip_address: IPv4Address | IPv6Address
|
|
||||||
protocol: str
|
|
||||||
|
|
||||||
|
|
||||||
# workaround for (and from) https://github.com/encode/httpx/issues/2992
|
|
||||||
class _NullCookieJar(CookieJar): # pragma: no cover
|
|
||||||
"""A CookieJar that rejects all cookies."""
|
|
||||||
|
|
||||||
def extract_cookies(self, response: HTTPResponse, request: ULRequest) -> None:
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def set_cookie(self, cookie: Cookie) -> None:
|
try:
|
||||||
|
import socksio.exceptions
|
||||||
|
|
||||||
|
HAS_SOCKS = True
|
||||||
|
DOWNLOAD_FAILED_EXCEPTIONS += (socksio.exceptions.ProtocolError,)
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HttpxDownloadHandler(BaseHttpDownloadHandler):
|
if TYPE_CHECKING:
|
||||||
_DEFAULT_CONNECT_TIMEOUT = 10
|
_Base = BaseStreamingDownloadHandler[httpx.Response]
|
||||||
|
else:
|
||||||
|
_Base = BaseStreamingDownloadHandler
|
||||||
|
|
||||||
|
|
||||||
|
class HttpxDownloadHandler(_Base):
|
||||||
|
experimental: ClassVar[bool] = True
|
||||||
|
|
||||||
def __init__(self, crawler: Crawler):
|
def __init__(self, crawler: Crawler):
|
||||||
# we skip HttpxDownloadHandler tests with the non-asyncio reactor
|
super().__init__(crawler)
|
||||||
if not is_asyncio_available(): # pragma: no cover
|
self._verify_certificates: bool = crawler.settings.getbool(
|
||||||
|
"DOWNLOAD_VERIFY_CERTIFICATES"
|
||||||
|
)
|
||||||
|
self._enable_h2: bool = crawler.settings.getbool("HTTPX_HTTP2_ENABLED")
|
||||||
|
if self._enable_h2 and not HAS_HTTP2: # pragma: no cover
|
||||||
raise NotConfigured(
|
raise NotConfigured(
|
||||||
f"{type(self).__name__} requires the asyncio support. Make"
|
f"HTTP/2 support in {type(self).__name__} requires the 'httpx[http2]' extra to be installed."
|
||||||
f" sure that you have either enabled the asyncio Twisted"
|
|
||||||
f" reactor in the TWISTED_REACTOR setting or disabled the"
|
|
||||||
f" TWISTED_REACTOR_ENABLED setting. See the asyncio"
|
|
||||||
f" documentation of Scrapy for more information."
|
|
||||||
)
|
)
|
||||||
|
self._ssl_context: ssl.SSLContext = _make_ssl_context(crawler.settings)
|
||||||
|
self._bind_host: str | None = self._get_bind_address_host()
|
||||||
|
self._limits: httpx.Limits = httpx.Limits(
|
||||||
|
# hard limit on simultaneous connections
|
||||||
|
max_connections=self._pool_size_total,
|
||||||
|
# total number of idle connections in the pool (extra ones are closed)
|
||||||
|
max_keepalive_connections=self._pool_size_total,
|
||||||
|
)
|
||||||
|
|
||||||
|
self._default_client: httpx.AsyncClient = self._make_client()
|
||||||
|
# httpx doesn't support per-request proxies: https://github.com/encode/httpx/discussions/3183,
|
||||||
|
# so we keep a pool of clients per proxy URL. LRU eviction can be added here if needed.
|
||||||
|
self._proxy_clients: dict[str, httpx.AsyncClient] = {}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _check_deps_installed() -> None:
|
||||||
if httpx is None: # pragma: no cover
|
if httpx is None: # pragma: no cover
|
||||||
raise NotConfigured(
|
raise NotConfigured(
|
||||||
f"{type(self).__name__} requires the httpx library to be installed."
|
"HttpxDownloadHandler requires the httpx library to be installed."
|
||||||
)
|
)
|
||||||
super().__init__(crawler)
|
|
||||||
logger.warning(
|
|
||||||
"HttpxDownloadHandler is experimental and is not recommended for production use."
|
|
||||||
)
|
|
||||||
bind_address = crawler.settings.get("DOWNLOAD_BIND_ADDRESS")
|
|
||||||
bind_address = normalize_bind_address(bind_address)
|
|
||||||
|
|
||||||
self._bind_address: str | None = None
|
def _make_client(self, proxy_url: str | None = None) -> httpx.AsyncClient:
|
||||||
|
if proxy_url:
|
||||||
|
if proxy_url.startswith("https:") and not self._verify_certificates:
|
||||||
|
proxy_ssl_context = _make_insecure_ssl_ctx()
|
||||||
|
else:
|
||||||
|
proxy_ssl_context = None
|
||||||
|
proxy = httpx.Proxy(proxy_url, ssl_context=proxy_ssl_context)
|
||||||
|
else:
|
||||||
|
proxy = None
|
||||||
|
|
||||||
if bind_address is not None:
|
client = httpx.AsyncClient(
|
||||||
host, port = bind_address
|
cookies=NullCookieJar(),
|
||||||
if port != 0:
|
|
||||||
logger.warning(
|
|
||||||
"DOWNLOAD_BIND_ADDRESS specifies a port (%s), but %s does not "
|
|
||||||
"support binding to a specific local port. Ignoring the port "
|
|
||||||
"and binding only to %r.",
|
|
||||||
port,
|
|
||||||
type(self).__name__,
|
|
||||||
host,
|
|
||||||
)
|
|
||||||
self._bind_address = host
|
|
||||||
|
|
||||||
self._client = httpx.AsyncClient(
|
|
||||||
cookies=_NullCookieJar(),
|
|
||||||
transport=httpx.AsyncHTTPTransport(
|
transport=httpx.AsyncHTTPTransport(
|
||||||
verify=_make_ssl_context(crawler.settings),
|
verify=self._ssl_context,
|
||||||
local_address=self._bind_address,
|
local_address=self._bind_host,
|
||||||
|
http2=self._enable_h2,
|
||||||
|
limits=self._limits,
|
||||||
|
trust_env=False,
|
||||||
|
proxy=proxy,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
# https://github.com/encode/httpx/discussions/1566
|
||||||
|
for header_name in ("accept", "accept-encoding", "user-agent"):
|
||||||
|
client.headers.pop(header_name, None)
|
||||||
|
return client
|
||||||
|
|
||||||
async def download_request(self, request: Request) -> Response:
|
def _get_client(self, proxy_url: str | None) -> httpx.AsyncClient:
|
||||||
self._warn_unsupported_meta(request.meta)
|
if proxy_url is None:
|
||||||
|
return self._default_client
|
||||||
|
if cached := self._proxy_clients.get(proxy_url):
|
||||||
|
return cached
|
||||||
|
client = self._make_client(proxy_url)
|
||||||
|
self._proxy_clients[proxy_url] = client
|
||||||
|
return client
|
||||||
|
|
||||||
timeout: float = request.meta.get(
|
@asynccontextmanager
|
||||||
"download_timeout", self._DEFAULT_CONNECT_TIMEOUT
|
async def _make_request(
|
||||||
)
|
self, request: Request, timeout: float
|
||||||
|
) -> AsyncIterator[httpx.Response]:
|
||||||
|
proxy = self._extract_proxy_url_with_creds(request)
|
||||||
|
if proxy and proxy.startswith("socks") and not HAS_SOCKS: # pragma: no cover
|
||||||
|
raise ValueError(
|
||||||
|
f"SOCKS proxy support in {type(self).__name__} requires the 'httpx[socks]' extra to be installed."
|
||||||
|
)
|
||||||
|
client = self._get_client(proxy)
|
||||||
|
headers = self._request_headers(request).to_tuple_list()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with self._get_httpx_response(request, timeout) as httpx_response:
|
async with client.stream(
|
||||||
return await self._read_response(httpx_response, request)
|
request.method,
|
||||||
|
request.url,
|
||||||
|
content=request.body,
|
||||||
|
headers=headers,
|
||||||
|
timeout=timeout,
|
||||||
|
) as response:
|
||||||
|
yield response
|
||||||
except httpx.TimeoutException as e:
|
except httpx.TimeoutException as e:
|
||||||
raise DownloadTimeoutError(
|
raise DownloadTimeoutError(
|
||||||
f"Getting {request.url} took longer than {timeout} seconds."
|
f"Getting {request.url} took longer than {timeout} seconds."
|
||||||
|
|
@ -135,168 +170,58 @@ class HttpxDownloadHandler(BaseHttpDownloadHandler):
|
||||||
except httpx.UnsupportedProtocol as e:
|
except httpx.UnsupportedProtocol as e:
|
||||||
raise UnsupportedURLSchemeError(str(e)) from e
|
raise UnsupportedURLSchemeError(str(e)) from e
|
||||||
except httpx.ConnectError as e:
|
except httpx.ConnectError as e:
|
||||||
error_message = str(e)
|
if any(isinstance(c, gaierror) for c in _iter_exc_causes(e)):
|
||||||
if (
|
raise CannotResolveHostError(str(e)) from e
|
||||||
"Name or service not known" in error_message
|
|
||||||
or "getaddrinfo failed" in error_message
|
|
||||||
or "nodename nor servname" in error_message
|
|
||||||
or "Temporary failure in name resolution" in error_message
|
|
||||||
):
|
|
||||||
raise CannotResolveHostError(error_message) from e
|
|
||||||
raise DownloadConnectionRefusedError(str(e)) from e
|
raise DownloadConnectionRefusedError(str(e)) from e
|
||||||
except httpx.NetworkError as e:
|
except httpx.ProxyError as e:
|
||||||
|
raise DownloadConnectionRefusedError(str(e)) from e
|
||||||
|
except DOWNLOAD_FAILED_EXCEPTIONS as e:
|
||||||
raise DownloadFailedError(str(e)) from e
|
raise DownloadFailedError(str(e)) from e
|
||||||
except httpx.RemoteProtocolError as e:
|
|
||||||
raise DownloadFailedError(str(e)) from e
|
|
||||||
|
|
||||||
def _warn_unsupported_meta(self, meta: dict[str, Any]) -> None:
|
|
||||||
if meta.get("bindaddress"):
|
|
||||||
# configurable only per-client:
|
|
||||||
# https://github.com/encode/httpx/issues/755#issuecomment-2746121794
|
|
||||||
logger.error(
|
|
||||||
f"The 'bindaddress' request meta key is not supported by"
|
|
||||||
f" {type(self).__name__} and will be ignored."
|
|
||||||
)
|
|
||||||
if meta.get("proxy"):
|
|
||||||
# configurable only per-client:
|
|
||||||
# https://github.com/encode/httpx/issues/486
|
|
||||||
logger.error(
|
|
||||||
f"The 'proxy' request meta key is not supported by"
|
|
||||||
f" {type(self).__name__} and will be ignored."
|
|
||||||
)
|
|
||||||
|
|
||||||
def _get_httpx_response(
|
|
||||||
self, request: Request, timeout: float
|
|
||||||
) -> AbstractAsyncContextManager[httpx.Response]:
|
|
||||||
return self._client.stream(
|
|
||||||
request.method,
|
|
||||||
request.url,
|
|
||||||
content=request.body,
|
|
||||||
headers=request.headers.to_tuple_list(),
|
|
||||||
timeout=timeout,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _read_response(
|
|
||||||
self, httpx_response: httpx.Response, request: Request
|
|
||||||
) -> Response:
|
|
||||||
maxsize: int = request.meta.get("download_maxsize", self._default_maxsize)
|
|
||||||
warnsize: int = request.meta.get("download_warnsize", self._default_warnsize)
|
|
||||||
|
|
||||||
content_length = httpx_response.headers.get("Content-Length")
|
|
||||||
expected_size = int(content_length) if content_length is not None else None
|
|
||||||
if maxsize and expected_size and expected_size > maxsize:
|
|
||||||
self._cancel_maxsize(expected_size, maxsize, request, expected=True)
|
|
||||||
|
|
||||||
reached_warnsize = False
|
|
||||||
if warnsize and expected_size and expected_size > warnsize:
|
|
||||||
reached_warnsize = True
|
|
||||||
logger.warning(
|
|
||||||
get_warnsize_msg(expected_size, warnsize, request, expected=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
headers = Headers(httpx_response.headers.multi_items())
|
|
||||||
network_stream: AsyncNetworkStream = httpx_response.extensions["network_stream"]
|
|
||||||
|
|
||||||
make_response_base_args: _BaseResponseArgs = {
|
|
||||||
"status": httpx_response.status_code,
|
|
||||||
"url": request.url,
|
|
||||||
"headers": headers,
|
|
||||||
"ip_address": self._get_server_ip(network_stream),
|
|
||||||
"protocol": httpx_response.http_version,
|
|
||||||
}
|
|
||||||
|
|
||||||
self._log_tls_info(network_stream)
|
|
||||||
|
|
||||||
if stop_download := check_stop_download(
|
|
||||||
signals.headers_received,
|
|
||||||
self.crawler,
|
|
||||||
request,
|
|
||||||
headers=headers,
|
|
||||||
body_length=expected_size,
|
|
||||||
):
|
|
||||||
return make_response(
|
|
||||||
**make_response_base_args,
|
|
||||||
stop_download=stop_download,
|
|
||||||
)
|
|
||||||
|
|
||||||
response_body = BytesIO()
|
|
||||||
bytes_received = 0
|
|
||||||
try:
|
|
||||||
async for chunk in httpx_response.aiter_raw():
|
|
||||||
response_body.write(chunk)
|
|
||||||
bytes_received += len(chunk)
|
|
||||||
|
|
||||||
if stop_download := check_stop_download(
|
|
||||||
signals.bytes_received, self.crawler, request, data=chunk
|
|
||||||
):
|
|
||||||
return make_response(
|
|
||||||
**make_response_base_args,
|
|
||||||
body=response_body.getvalue(),
|
|
||||||
stop_download=stop_download,
|
|
||||||
)
|
|
||||||
|
|
||||||
if maxsize and bytes_received > maxsize:
|
|
||||||
response_body.truncate(0)
|
|
||||||
self._cancel_maxsize(
|
|
||||||
bytes_received, maxsize, request, expected=False
|
|
||||||
)
|
|
||||||
|
|
||||||
if warnsize and bytes_received > warnsize and not reached_warnsize:
|
|
||||||
reached_warnsize = True
|
|
||||||
logger.warning(
|
|
||||||
get_warnsize_msg(
|
|
||||||
bytes_received, warnsize, request, expected=False
|
|
||||||
)
|
|
||||||
)
|
|
||||||
except httpx.RemoteProtocolError as e:
|
|
||||||
# special handling of the dataloss case
|
|
||||||
if (
|
|
||||||
"peer closed connection without sending complete message body"
|
|
||||||
not in str(e)
|
|
||||||
):
|
|
||||||
raise
|
|
||||||
fail_on_dataloss: bool = request.meta.get(
|
|
||||||
"download_fail_on_dataloss", self._fail_on_dataloss
|
|
||||||
)
|
|
||||||
if not fail_on_dataloss:
|
|
||||||
return make_response(
|
|
||||||
**make_response_base_args,
|
|
||||||
body=response_body.getvalue(),
|
|
||||||
flags=["dataloss"],
|
|
||||||
)
|
|
||||||
self._log_dataloss_warning(request.url)
|
|
||||||
raise ResponseDataLossError(str(e)) from e
|
|
||||||
|
|
||||||
return make_response(
|
|
||||||
**make_response_base_args,
|
|
||||||
body=response_body.getvalue(),
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_server_ip(network_stream: AsyncNetworkStream) -> IPv4Address | IPv6Address:
|
def _extract_headers(response: httpx.Response) -> Headers:
|
||||||
extra_server_addr = network_stream.get_extra_info("server_addr")
|
return Headers(response.headers.multi_items())
|
||||||
return ipaddress.ip_address(extra_server_addr[0])
|
|
||||||
|
|
||||||
def _log_tls_info(self, network_stream: AsyncNetworkStream) -> None:
|
@staticmethod
|
||||||
if not self._tls_verbose_logging:
|
def _build_base_response_args(
|
||||||
return
|
response: httpx.Response,
|
||||||
|
request: Request,
|
||||||
|
headers: Headers,
|
||||||
|
) -> _BaseResponseArgs:
|
||||||
|
network_stream: AsyncNetworkStream = response.extensions["network_stream"]
|
||||||
|
server_addr = network_stream.get_extra_info("server_addr")
|
||||||
|
ip_address = ipaddress.ip_address(server_addr[0])
|
||||||
|
ssl_object = network_stream.get_extra_info("ssl_object")
|
||||||
|
if isinstance(ssl_object, ssl.SSLObject):
|
||||||
|
cert = ssl_object.getpeercert(binary_form=True)
|
||||||
|
else:
|
||||||
|
cert = None
|
||||||
|
return {
|
||||||
|
"status": response.status_code,
|
||||||
|
"url": request.url,
|
||||||
|
"headers": headers,
|
||||||
|
"certificate": cert,
|
||||||
|
"ip_address": ip_address,
|
||||||
|
"protocol": response.http_version,
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _iter_body_chunks(response: httpx.Response) -> AsyncIterator[bytes]:
|
||||||
|
return response.aiter_raw()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _is_dataloss_exception(exc: Exception) -> bool:
|
||||||
|
return isinstance(
|
||||||
|
exc, httpx.RemoteProtocolError
|
||||||
|
) and "peer closed connection without sending complete message body" in str(exc)
|
||||||
|
|
||||||
|
def _log_tls_info(self, response: httpx.Response, request: Request) -> None:
|
||||||
|
network_stream: AsyncNetworkStream = response.extensions["network_stream"]
|
||||||
extra_ssl_object = network_stream.get_extra_info("ssl_object")
|
extra_ssl_object = network_stream.get_extra_info("ssl_object")
|
||||||
if isinstance(extra_ssl_object, ssl.SSLObject):
|
if isinstance(extra_ssl_object, ssl.SSLObject):
|
||||||
_log_sslobj_debug_info(extra_ssl_object)
|
_log_sslobj_debug_info(extra_ssl_object)
|
||||||
|
|
||||||
def _log_dataloss_warning(self, url: str) -> None:
|
|
||||||
if self._fail_on_dataloss_warned:
|
|
||||||
return
|
|
||||||
logger.warning(get_dataloss_msg(url))
|
|
||||||
self._fail_on_dataloss_warned = True
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _cancel_maxsize(
|
|
||||||
size: int, limit: int, request: Request, *, expected: bool
|
|
||||||
) -> NoReturn:
|
|
||||||
warning_msg = get_maxsize_msg(size, limit, request, expected=expected)
|
|
||||||
logger.warning(warning_msg)
|
|
||||||
raise DownloadCancelledError(warning_msg)
|
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
await self._client.aclose()
|
await self._default_client.aclose()
|
||||||
|
for client in self._proxy_clients.values():
|
||||||
|
await client.aclose()
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@
|
||||||
An asynchronous FTP file download handler for scrapy which somehow emulates an http response.
|
An asynchronous FTP file download handler for scrapy which somehow emulates an http response.
|
||||||
|
|
||||||
FTP connection parameters are passed using the request meta field:
|
FTP connection parameters are passed using the request meta field:
|
||||||
- ftp_user (required)
|
- ftp_user (optional, falls back to FTP_USER)
|
||||||
- ftp_password (required)
|
- ftp_password (optional, falls back to FTP_PASSWORD)
|
||||||
- ftp_passive (by default, enabled) sets FTP connection passive mode
|
- ftp_passive (optional, falls back to FTP_PASSIVE_MODE) sets FTP connection passive mode
|
||||||
- ftp_local_filename
|
- ftp_local_filename
|
||||||
- If not given, file data will come in the response.body, as a normal scrapy Response,
|
- If not given, file data will come in the response.body, as a normal scrapy Response,
|
||||||
which will imply that the entire file will be on memory.
|
which will imply that the entire file will be on memory.
|
||||||
|
|
@ -119,7 +119,10 @@ class FTPDownloadHandler(BaseDownloadHandler):
|
||||||
httpcode = self.CODE_MAPPING.get(ftpcode, self.CODE_MAPPING["default"])
|
httpcode = self.CODE_MAPPING.get(ftpcode, self.CODE_MAPPING["default"])
|
||||||
return Response(url=request.url, status=httpcode, body=message.encode())
|
return Response(url=request.url, status=httpcode, body=message.encode())
|
||||||
raise
|
raise
|
||||||
protocol.close()
|
finally:
|
||||||
|
protocol.close()
|
||||||
|
assert client.transport
|
||||||
|
client.transport.loseConnection()
|
||||||
headers = {"local filename": protocol.filename or b"", "size": protocol.size}
|
headers = {"local filename": protocol.filename or b"", "size": protocol.size}
|
||||||
body = protocol.filename or protocol.body.read()
|
body = protocol.filename or protocol.body.read()
|
||||||
respcls = responsetypes.from_args(url=request.url, body=body)
|
respcls = responsetypes.from_args(url=request.url, body=body)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
# pragma: no file cover
|
# pragma: no file cover
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from scrapy.core.downloader.handlers.http10 import HTTP10DownloadHandler
|
|
||||||
from scrapy.core.downloader.handlers.http11 import (
|
from scrapy.core.downloader.handlers.http11 import (
|
||||||
HTTP11DownloadHandler as HTTPDownloadHandler,
|
HTTP11DownloadHandler as HTTPDownloadHandler,
|
||||||
)
|
)
|
||||||
|
|
@ -16,6 +15,5 @@ warnings.warn(
|
||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"HTTP10DownloadHandler",
|
|
||||||
"HTTPDownloadHandler",
|
"HTTPDownloadHandler",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
"""Download handlers for http and https schemes"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import warnings
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
from scrapy.core.downloader.contextfactory import _ScrapyClientContextFactory
|
|
||||||
from scrapy.exceptions import NotConfigured, ScrapyDeprecationWarning
|
|
||||||
from scrapy.utils.defer import maybe_deferred_to_future
|
|
||||||
from scrapy.utils.misc import build_from_crawler, load_object
|
|
||||||
from scrapy.utils.python import to_unicode
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from twisted.internet.interfaces import IConnector
|
|
||||||
|
|
||||||
# typing.Self requires Python 3.11
|
|
||||||
from typing_extensions import Self
|
|
||||||
|
|
||||||
from scrapy import Request
|
|
||||||
from scrapy.core.downloader.webclient import ScrapyHTTPClientFactory
|
|
||||||
from scrapy.crawler import Crawler
|
|
||||||
from scrapy.http import Response
|
|
||||||
from scrapy.settings import BaseSettings
|
|
||||||
|
|
||||||
|
|
||||||
class HTTP10DownloadHandler:
|
|
||||||
lazy = False
|
|
||||||
|
|
||||||
def __init__(self, settings: BaseSettings, crawler: Crawler):
|
|
||||||
warnings.warn(
|
|
||||||
"HTTP10DownloadHandler is deprecated and will be removed in a future Scrapy version.",
|
|
||||||
category=ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
if not crawler.settings.getbool("TWISTED_REACTOR_ENABLED"): # pragma: no cover
|
|
||||||
raise NotConfigured(f"{type(self).__name__} requires a Twisted reactor.")
|
|
||||||
self.HTTPClientFactory: type[ScrapyHTTPClientFactory] = load_object(
|
|
||||||
settings["DOWNLOADER_HTTPCLIENTFACTORY"]
|
|
||||||
)
|
|
||||||
if settings["DOWNLOADER_CLIENTCONTEXTFACTORY"] == "SENTINEL":
|
|
||||||
self.ClientContextFactory: type[_ScrapyClientContextFactory] = (
|
|
||||||
_ScrapyClientContextFactory
|
|
||||||
)
|
|
||||||
else: # pragma: no cover
|
|
||||||
warnings.warn(
|
|
||||||
"The 'DOWNLOADER_CLIENTCONTEXTFACTORY' setting is deprecated.",
|
|
||||||
category=ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
self.ClientContextFactory = load_object(
|
|
||||||
settings["DOWNLOADER_CLIENTCONTEXTFACTORY"]
|
|
||||||
)
|
|
||||||
self._settings: BaseSettings = settings
|
|
||||||
self._crawler: Crawler = crawler
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_crawler(cls, crawler: Crawler) -> Self:
|
|
||||||
return cls(crawler.settings, crawler)
|
|
||||||
|
|
||||||
async def download_request(self, request: Request) -> Response:
|
|
||||||
factory = self.HTTPClientFactory(request)
|
|
||||||
self._connect(factory)
|
|
||||||
return await maybe_deferred_to_future(factory.deferred)
|
|
||||||
|
|
||||||
def _connect(self, factory: ScrapyHTTPClientFactory) -> IConnector:
|
|
||||||
from twisted.internet import reactor
|
|
||||||
|
|
||||||
host, port = to_unicode(factory.host), factory.port
|
|
||||||
if factory.scheme == b"https":
|
|
||||||
client_context_factory = build_from_crawler(
|
|
||||||
self.ClientContextFactory,
|
|
||||||
self._crawler,
|
|
||||||
)
|
|
||||||
return reactor.connectSSL(host, port, factory, client_context_factory)
|
|
||||||
return reactor.connectTCP(host, port, factory)
|
|
||||||
|
|
||||||
async def close(self) -> None:
|
|
||||||
pass
|
|
||||||
|
|
@ -6,8 +6,9 @@ import ipaddress
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
from functools import partial
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from time import time
|
from time import monotonic
|
||||||
from typing import TYPE_CHECKING, Any, TypedDict, TypeVar, cast
|
from typing import TYPE_CHECKING, Any, TypedDict, TypeVar, cast
|
||||||
from urllib.parse import urldefrag, urlparse
|
from urllib.parse import urldefrag, urlparse
|
||||||
|
|
||||||
|
|
@ -40,7 +41,6 @@ from scrapy.exceptions import (
|
||||||
)
|
)
|
||||||
from scrapy.http import Headers, Response
|
from scrapy.http import Headers, Response
|
||||||
from scrapy.utils._download_handlers import (
|
from scrapy.utils._download_handlers import (
|
||||||
BaseHttpDownloadHandler,
|
|
||||||
check_stop_download,
|
check_stop_download,
|
||||||
get_dataloss_msg,
|
get_dataloss_msg,
|
||||||
get_maxsize_msg,
|
get_maxsize_msg,
|
||||||
|
|
@ -56,6 +56,8 @@ from scrapy.utils.python import to_bytes, to_unicode
|
||||||
from scrapy.utils.ssl import _log_ssl_conn_debug_info
|
from scrapy.utils.ssl import _log_ssl_conn_debug_info
|
||||||
from scrapy.utils.url import add_http_if_no_scheme
|
from scrapy.utils.url import add_http_if_no_scheme
|
||||||
|
|
||||||
|
from ._base_http import BaseHttpDownloadHandler
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from twisted.internet.base import ReactorBase
|
from twisted.internet.base import ReactorBase
|
||||||
from twisted.internet.interfaces import IConsumer
|
from twisted.internet.interfaces import IConsumer
|
||||||
|
|
@ -102,7 +104,6 @@ class HTTP11DownloadHandler(BaseHttpDownloadHandler):
|
||||||
self._disconnect_timeout: int = 1
|
self._disconnect_timeout: int = 1
|
||||||
|
|
||||||
async def download_request(self, request: Request) -> Response:
|
async def download_request(self, request: Request) -> Response:
|
||||||
"""Return a deferred for the HTTP download"""
|
|
||||||
if hasattr(self._crawler.spider, "download_maxsize"): # pragma: no cover
|
if hasattr(self._crawler.spider, "download_maxsize"): # pragma: no cover
|
||||||
warn_on_deprecated_spider_attribute("download_maxsize", "DOWNLOAD_MAXSIZE")
|
warn_on_deprecated_spider_attribute("download_maxsize", "DOWNLOAD_MAXSIZE")
|
||||||
if hasattr(self._crawler.spider, "download_warnsize"): # pragma: no cover
|
if hasattr(self._crawler.spider, "download_warnsize"): # pragma: no cover
|
||||||
|
|
@ -110,7 +111,7 @@ class HTTP11DownloadHandler(BaseHttpDownloadHandler):
|
||||||
"download_warnsize", "DOWNLOAD_WARNSIZE"
|
"download_warnsize", "DOWNLOAD_WARNSIZE"
|
||||||
)
|
)
|
||||||
|
|
||||||
agent = ScrapyAgent(
|
agent = _ScrapyAgent(
|
||||||
contextFactory=self._contextFactory,
|
contextFactory=self._contextFactory,
|
||||||
bindAddress=self._bind_address,
|
bindAddress=self._bind_address,
|
||||||
pool=self._pool,
|
pool=self._pool,
|
||||||
|
|
@ -160,7 +161,7 @@ class TunnelError(Exception):
|
||||||
"""An HTTP CONNECT tunnel could not be established by the proxy."""
|
"""An HTTP CONNECT tunnel could not be established by the proxy."""
|
||||||
|
|
||||||
|
|
||||||
class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
class _TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
||||||
"""An endpoint that tunnels through proxies to allow HTTPS downloads. To
|
"""An endpoint that tunnels through proxies to allow HTTPS downloads. To
|
||||||
accomplish that, this endpoint sends an HTTP CONNECT to the proxy.
|
accomplish that, this endpoint sends an HTTP CONNECT to the proxy.
|
||||||
The HTTP CONNECT is always sent when using this endpoint, I think this could
|
The HTTP CONNECT is always sent when using this endpoint, I think this could
|
||||||
|
|
@ -196,7 +197,7 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
||||||
def requestTunnel(self, protocol: Protocol) -> Protocol:
|
def requestTunnel(self, protocol: Protocol) -> Protocol:
|
||||||
"""Asks the proxy to open a tunnel."""
|
"""Asks the proxy to open a tunnel."""
|
||||||
assert protocol.transport
|
assert protocol.transport
|
||||||
tunnelReq = tunnel_request_data(
|
tunnelReq = _tunnel_request_data(
|
||||||
self._tunneledHost, self._tunneledPort, self._proxyAuthHeader
|
self._tunneledHost, self._tunneledPort, self._proxyAuthHeader
|
||||||
)
|
)
|
||||||
protocol.transport.write(tunnelReq)
|
protocol.transport.write(tunnelReq)
|
||||||
|
|
@ -220,11 +221,12 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
||||||
if b"\r\n\r\n" not in self._connectBuffer:
|
if b"\r\n\r\n" not in self._connectBuffer:
|
||||||
return
|
return
|
||||||
self._protocol.dataReceived = self._protocolDataReceived # type: ignore[method-assign]
|
self._protocol.dataReceived = self._protocolDataReceived # type: ignore[method-assign]
|
||||||
respm = TunnelingTCP4ClientEndpoint._responseMatcher.match(self._connectBuffer)
|
respm = _TunnelingTCP4ClientEndpoint._responseMatcher.match(self._connectBuffer)
|
||||||
if respm and int(respm.group("status")) == 200:
|
if respm and int(respm.group("status")) == 200:
|
||||||
# set proper Server Name Indication extension
|
# set proper Server Name Indication extension
|
||||||
sslOptions = self._contextFactory.creatorForNetloc( # type: ignore[call-arg,misc]
|
sslOptions = self._contextFactory.creatorForNetloc( # type: ignore[call-arg,misc]
|
||||||
self._tunneledHost, self._tunneledPort
|
self._tunneledHost, # type: ignore[arg-type]
|
||||||
|
self._tunneledPort,
|
||||||
)
|
)
|
||||||
self._protocol.transport.startTLS(sslOptions, self._protocolFactory)
|
self._protocol.transport.startTLS(sslOptions, self._protocolFactory)
|
||||||
self._tunnelReadyDeferred.callback(self._protocol)
|
self._tunnelReadyDeferred.callback(self._protocol)
|
||||||
|
|
@ -256,18 +258,18 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
||||||
return self._tunnelReadyDeferred
|
return self._tunnelReadyDeferred
|
||||||
|
|
||||||
|
|
||||||
def tunnel_request_data(
|
def _tunnel_request_data(
|
||||||
host: str, port: int, proxy_auth_header: bytes | None = None
|
host: str, port: int, proxy_auth_header: bytes | None = None
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
r"""
|
r"""
|
||||||
Return binary content of a CONNECT request.
|
Return binary content of a CONNECT request.
|
||||||
|
|
||||||
>>> from scrapy.utils.python import to_unicode as s
|
>>> from scrapy.utils.python import to_unicode as s
|
||||||
>>> s(tunnel_request_data("example.com", 8080))
|
>>> s(_tunnel_request_data("example.com", 8080))
|
||||||
'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\n\r\n'
|
'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\n\r\n'
|
||||||
>>> s(tunnel_request_data("example.com", 8080, b"123"))
|
>>> s(_tunnel_request_data("example.com", 8080, b"123"))
|
||||||
'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\nProxy-Authorization: 123\r\n\r\n'
|
'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\nProxy-Authorization: 123\r\n\r\n'
|
||||||
>>> s(tunnel_request_data(b"example.com", "8090"))
|
>>> s(_tunnel_request_data(b"example.com", "8090"))
|
||||||
'CONNECT example.com:8090 HTTP/1.1\r\nHost: example.com:8090\r\n\r\n'
|
'CONNECT example.com:8090 HTTP/1.1\r\nHost: example.com:8090\r\n\r\n'
|
||||||
"""
|
"""
|
||||||
host_value = to_bytes(host, encoding="ascii") + b":" + to_bytes(str(port))
|
host_value = to_bytes(host, encoding="ascii") + b":" + to_bytes(str(port))
|
||||||
|
|
@ -279,8 +281,8 @@ def tunnel_request_data(
|
||||||
return tunnel_req
|
return tunnel_req
|
||||||
|
|
||||||
|
|
||||||
class TunnelingAgent(Agent):
|
class _TunnelingAgent(Agent):
|
||||||
"""An agent that uses a L{TunnelingTCP4ClientEndpoint} to make HTTPS
|
"""An agent that uses a ``_TunnelingTCP4ClientEndpoint`` to make HTTPS
|
||||||
downloads. It may look strange that we have chosen to subclass Agent and not
|
downloads. It may look strange that we have chosen to subclass Agent and not
|
||||||
ProxyAgent but consider that after the tunnel is opened the proxy is
|
ProxyAgent but consider that after the tunnel is opened the proxy is
|
||||||
transparent to the client; thus the agent should behave like there is no
|
transparent to the client; thus the agent should behave like there is no
|
||||||
|
|
@ -301,8 +303,8 @@ class TunnelingAgent(Agent):
|
||||||
self._proxyConf: tuple[str, int, bytes | None] = proxyConf
|
self._proxyConf: tuple[str, int, bytes | None] = proxyConf
|
||||||
self._contextFactory: IPolicyForHTTPS = contextFactory
|
self._contextFactory: IPolicyForHTTPS = contextFactory
|
||||||
|
|
||||||
def _getEndpoint(self, uri: URI) -> TunnelingTCP4ClientEndpoint:
|
def _getEndpoint(self, uri: URI) -> _TunnelingTCP4ClientEndpoint:
|
||||||
return TunnelingTCP4ClientEndpoint(
|
return _TunnelingTCP4ClientEndpoint(
|
||||||
reactor=self._reactor,
|
reactor=self._reactor,
|
||||||
host=uri.host,
|
host=uri.host,
|
||||||
port=uri.port,
|
port=uri.port,
|
||||||
|
|
@ -337,17 +339,19 @@ class TunnelingAgent(Agent):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ScrapyProxyAgent(Agent):
|
class _ScrapyProxyAgent(Agent):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
reactor: ReactorBase,
|
reactor: ReactorBase,
|
||||||
proxyURI: bytes,
|
proxyURI: bytes,
|
||||||
|
contextFactory: IPolicyForHTTPS,
|
||||||
connectTimeout: float | None = None,
|
connectTimeout: float | None = None,
|
||||||
bindAddress: tuple[str, int] | None = None,
|
bindAddress: tuple[str, int] | None = None,
|
||||||
pool: HTTPConnectionPool | None = None,
|
pool: HTTPConnectionPool | None = None,
|
||||||
):
|
):
|
||||||
super().__init__( # type: ignore[no-untyped-call]
|
super().__init__( # type: ignore[no-untyped-call]
|
||||||
reactor=reactor,
|
reactor=reactor,
|
||||||
|
contextFactory=contextFactory,
|
||||||
connectTimeout=connectTimeout,
|
connectTimeout=connectTimeout,
|
||||||
bindAddress=bindAddress,
|
bindAddress=bindAddress,
|
||||||
pool=pool,
|
pool=pool,
|
||||||
|
|
@ -377,11 +381,7 @@ class ScrapyProxyAgent(Agent):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ScrapyAgent:
|
class _ScrapyAgent:
|
||||||
_Agent = Agent
|
|
||||||
_ProxyAgent = ScrapyProxyAgent
|
|
||||||
_TunnelingAgent = TunnelingAgent
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
|
|
@ -420,10 +420,14 @@ class ScrapyAgent:
|
||||||
if not proxy_port:
|
if not proxy_port:
|
||||||
proxy_port = 443 if proxy_parsed.scheme == "https" else 80
|
proxy_port = 443 if proxy_parsed.scheme == "https" else 80
|
||||||
if urlparse_cached(request).scheme == "https":
|
if urlparse_cached(request).scheme == "https":
|
||||||
|
if proxy_parsed.scheme == "https": # pragma: no cover
|
||||||
|
raise NotImplementedError(
|
||||||
|
"HTTPS proxies for HTTPS destinations are not supported"
|
||||||
|
)
|
||||||
assert proxy_host is not None
|
assert proxy_host is not None
|
||||||
proxyAuth = request.headers.get(b"Proxy-Authorization", None)
|
proxyAuth = request.headers.get(b"Proxy-Authorization", None)
|
||||||
proxyConf = (proxy_host, proxy_port, proxyAuth)
|
proxyConf = (proxy_host, proxy_port, proxyAuth)
|
||||||
return self._TunnelingAgent(
|
return _TunnelingAgent(
|
||||||
reactor=reactor,
|
reactor=reactor,
|
||||||
proxyConf=proxyConf,
|
proxyConf=proxyConf,
|
||||||
contextFactory=self._contextFactory,
|
contextFactory=self._contextFactory,
|
||||||
|
|
@ -431,15 +435,16 @@ class ScrapyAgent:
|
||||||
bindAddress=bindaddress,
|
bindAddress=bindaddress,
|
||||||
pool=self._pool,
|
pool=self._pool,
|
||||||
)
|
)
|
||||||
return self._ProxyAgent(
|
return _ScrapyProxyAgent(
|
||||||
reactor=reactor,
|
reactor=reactor,
|
||||||
proxyURI=to_bytes(proxy, encoding="ascii"),
|
proxyURI=to_bytes(proxy, encoding="ascii"),
|
||||||
|
contextFactory=self._contextFactory,
|
||||||
connectTimeout=timeout,
|
connectTimeout=timeout,
|
||||||
bindAddress=bindaddress,
|
bindAddress=bindaddress,
|
||||||
pool=self._pool,
|
pool=self._pool,
|
||||||
)
|
)
|
||||||
|
|
||||||
return self._Agent( # type: ignore[no-untyped-call]
|
return Agent(
|
||||||
reactor=reactor,
|
reactor=reactor,
|
||||||
contextFactory=self._contextFactory,
|
contextFactory=self._contextFactory,
|
||||||
connectTimeout=timeout,
|
connectTimeout=timeout,
|
||||||
|
|
@ -457,10 +462,10 @@ class ScrapyAgent:
|
||||||
url = urldefrag(request.url)[0]
|
url = urldefrag(request.url)[0]
|
||||||
method = to_bytes(request.method)
|
method = to_bytes(request.method)
|
||||||
headers = TxHeaders(request.headers)
|
headers = TxHeaders(request.headers)
|
||||||
if isinstance(agent, self._TunnelingAgent):
|
if isinstance(agent, _TunnelingAgent):
|
||||||
headers.removeHeader(b"Proxy-Authorization")
|
headers.removeHeader(b"Proxy-Authorization")
|
||||||
bodyproducer = _RequestBodyProducer(request.body) if request.body else None
|
bodyproducer = _RequestBodyProducer(request.body) if request.body else None
|
||||||
start_time = time()
|
start_time = monotonic()
|
||||||
d: Deferred[IResponse] = agent.request(
|
d: Deferred[IResponse] = agent.request(
|
||||||
method,
|
method,
|
||||||
to_bytes(url, encoding="ascii"),
|
to_bytes(url, encoding="ascii"),
|
||||||
|
|
@ -489,7 +494,7 @@ class ScrapyAgent:
|
||||||
raise DownloadTimeoutError(f"Getting {url} took longer than {timeout} seconds.")
|
raise DownloadTimeoutError(f"Getting {url} took longer than {timeout} seconds.")
|
||||||
|
|
||||||
def _cb_latency(self, result: _T, request: Request, start_time: float) -> _T:
|
def _cb_latency(self, result: _T, request: Request, start_time: float) -> _T:
|
||||||
request.meta["download_latency"] = time() - start_time
|
request.meta["download_latency"] = monotonic() - start_time
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -539,7 +544,8 @@ class ScrapyAgent:
|
||||||
expected_size, maxsize, request, expected=True
|
expected_size, maxsize, request, expected=True
|
||||||
)
|
)
|
||||||
logger.warning(warning_msg)
|
logger.warning(warning_msg)
|
||||||
txresponse._transport.loseConnection()
|
# Abort connection immediately.
|
||||||
|
txresponse._transport._producer.abortConnection()
|
||||||
raise DownloadCancelledError(warning_msg)
|
raise DownloadCancelledError(warning_msg)
|
||||||
|
|
||||||
if warnsize and expected_size > warnsize:
|
if warnsize and expected_size > warnsize:
|
||||||
|
|
@ -547,11 +553,7 @@ class ScrapyAgent:
|
||||||
get_warnsize_msg(expected_size, warnsize, request, expected=True)
|
get_warnsize_msg(expected_size, warnsize, request, expected=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _cancel(_: Any) -> None:
|
d: Deferred[_ResultT] = Deferred(partial(self._cancel, txresponse=txresponse))
|
||||||
# Abort connection immediately.
|
|
||||||
txresponse._transport._producer.abortConnection()
|
|
||||||
|
|
||||||
d: Deferred[_ResultT] = Deferred(_cancel)
|
|
||||||
txresponse.deliverBody(
|
txresponse.deliverBody(
|
||||||
_ResponseReader(
|
_ResponseReader(
|
||||||
finished=d,
|
finished=d,
|
||||||
|
|
@ -570,6 +572,11 @@ class ScrapyAgent:
|
||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _cancel(_: Any, txresponse: TxResponse) -> None:
|
||||||
|
# Abort connection immediately.
|
||||||
|
txresponse._transport._producer.abortConnection()
|
||||||
|
|
||||||
def _cb_bodydone(self, result: _ResultT, url: str) -> Response:
|
def _cb_bodydone(self, result: _ResultT, url: str) -> Response:
|
||||||
headers = self._headers_from_twisted_response(result["txresponse"])
|
headers = self._headers_from_twisted_response(result["txresponse"])
|
||||||
try:
|
try:
|
||||||
|
|
@ -667,17 +674,17 @@ class _ResponseReader(Protocol):
|
||||||
assert hostname is not None
|
assert hostname is not None
|
||||||
_log_ssl_conn_debug_info(hostname, connection)
|
_log_ssl_conn_debug_info(hostname, connection)
|
||||||
|
|
||||||
def dataReceived(self, bodyBytes: bytes) -> None:
|
def dataReceived(self, data: bytes) -> None:
|
||||||
# This maybe called several times after cancel was called with buffered data.
|
# This maybe called several times after cancel was called with buffered data.
|
||||||
if self._finished.called:
|
if self._finished.called:
|
||||||
return
|
return
|
||||||
|
|
||||||
assert self.transport
|
assert self.transport
|
||||||
self._bodybuf.write(bodyBytes)
|
self._bodybuf.write(data)
|
||||||
self._bytes_received += len(bodyBytes)
|
self._bytes_received += len(data)
|
||||||
|
|
||||||
if stop_download := check_stop_download(
|
if stop_download := check_stop_download(
|
||||||
signals.bytes_received, self._crawler, self._request, data=bodyBytes
|
signals.bytes_received, self._crawler, self._request, data=data
|
||||||
):
|
):
|
||||||
self.transport.stopProducing()
|
self.transport.stopProducing()
|
||||||
self.transport.loseConnection()
|
self.transport.loseConnection()
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,23 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from time import time
|
from time import monotonic
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from urllib.parse import urldefrag
|
from urllib.parse import urldefrag
|
||||||
|
|
||||||
from twisted.web.client import URI
|
|
||||||
|
|
||||||
from scrapy.core.downloader.contextfactory import _load_context_factory_from_settings
|
from scrapy.core.downloader.contextfactory import _load_context_factory_from_settings
|
||||||
from scrapy.core.downloader.handlers.base import BaseDownloadHandler
|
from scrapy.core.downloader.handlers.base import BaseDownloadHandler
|
||||||
from scrapy.core.http2.agent import H2Agent, H2ConnectionPool, ScrapyProxyH2Agent
|
from scrapy.core.http2.agent import H2Agent, H2ConnectionPool
|
||||||
from scrapy.exceptions import DownloadTimeoutError, NotConfigured
|
from scrapy.exceptions import (
|
||||||
|
DownloadTimeoutError,
|
||||||
|
NotConfigured,
|
||||||
|
UnsupportedURLSchemeError,
|
||||||
|
)
|
||||||
from scrapy.utils._download_handlers import (
|
from scrapy.utils._download_handlers import (
|
||||||
normalize_bind_address,
|
normalize_bind_address,
|
||||||
wrap_twisted_exceptions,
|
wrap_twisted_exceptions,
|
||||||
)
|
)
|
||||||
from scrapy.utils.defer import maybe_deferred_to_future
|
from scrapy.utils.defer import maybe_deferred_to_future
|
||||||
from scrapy.utils.httpobj import urlparse_cached
|
from scrapy.utils.httpobj import urlparse_cached
|
||||||
from scrapy.utils.python import to_bytes
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from twisted.internet.base import DelayedCall
|
from twisted.internet.base import DelayedCall
|
||||||
|
|
@ -44,7 +45,11 @@ class H2DownloadHandler(BaseDownloadHandler):
|
||||||
self._bind_address = crawler.settings.get("DOWNLOAD_BIND_ADDRESS")
|
self._bind_address = crawler.settings.get("DOWNLOAD_BIND_ADDRESS")
|
||||||
|
|
||||||
async def download_request(self, request: Request) -> Response:
|
async def download_request(self, request: Request) -> Response:
|
||||||
agent = ScrapyH2Agent(
|
if urlparse_cached(request).scheme == "http": # pragma: no cover
|
||||||
|
raise UnsupportedURLSchemeError(
|
||||||
|
f"{type(self).__name__} doesn't support plain HTTP."
|
||||||
|
)
|
||||||
|
agent = _ScrapyH2Agent(
|
||||||
context_factory=self._context_factory,
|
context_factory=self._context_factory,
|
||||||
pool=self._pool,
|
pool=self._pool,
|
||||||
bind_address=self._bind_address,
|
bind_address=self._bind_address,
|
||||||
|
|
@ -60,10 +65,7 @@ class H2DownloadHandler(BaseDownloadHandler):
|
||||||
self._pool.close_connections()
|
self._pool.close_connections()
|
||||||
|
|
||||||
|
|
||||||
class ScrapyH2Agent:
|
class _ScrapyH2Agent:
|
||||||
_Agent = H2Agent
|
|
||||||
_ProxyAgent = ScrapyProxyH2Agent
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
context_factory: IPolicyForHTTPS,
|
context_factory: IPolicyForHTTPS,
|
||||||
|
|
@ -81,25 +83,11 @@ class ScrapyH2Agent:
|
||||||
def _get_agent(self, request: Request, timeout: float | None) -> H2Agent:
|
def _get_agent(self, request: Request, timeout: float | None) -> H2Agent:
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
|
|
||||||
|
if request.meta.get("proxy"): # pragma: no cover
|
||||||
|
raise NotImplementedError(f"{type(self).__name__} doesn't support proxies.")
|
||||||
bind_address = request.meta.get("bindaddress") or self._bind_address
|
bind_address = request.meta.get("bindaddress") or self._bind_address
|
||||||
bind_address = normalize_bind_address(bind_address)
|
bind_address = normalize_bind_address(bind_address)
|
||||||
proxy = request.meta.get("proxy")
|
return H2Agent(
|
||||||
if proxy:
|
|
||||||
if urlparse_cached(request).scheme == "https":
|
|
||||||
# ToDo
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Tunneling via CONNECT method using HTTP/2.0 is not yet supported"
|
|
||||||
)
|
|
||||||
return self._ProxyAgent(
|
|
||||||
reactor=reactor,
|
|
||||||
context_factory=self._context_factory,
|
|
||||||
proxy_uri=URI.fromBytes(to_bytes(proxy, encoding="ascii")),
|
|
||||||
connect_timeout=timeout,
|
|
||||||
bind_address=bind_address,
|
|
||||||
pool=self._pool,
|
|
||||||
)
|
|
||||||
|
|
||||||
return self._Agent(
|
|
||||||
reactor=reactor,
|
reactor=reactor,
|
||||||
context_factory=self._context_factory,
|
context_factory=self._context_factory,
|
||||||
connect_timeout=timeout,
|
connect_timeout=timeout,
|
||||||
|
|
@ -113,7 +101,7 @@ class ScrapyH2Agent:
|
||||||
timeout = request.meta.get("download_timeout") or self._connect_timeout
|
timeout = request.meta.get("download_timeout") or self._connect_timeout
|
||||||
agent = self._get_agent(request, timeout)
|
agent = self._get_agent(request, timeout)
|
||||||
|
|
||||||
start_time = time()
|
start_time = monotonic()
|
||||||
d = agent.request(request, spider)
|
d = agent.request(request, spider)
|
||||||
d.addCallback(self._cb_latency, request, start_time)
|
d.addCallback(self._cb_latency, request, start_time)
|
||||||
|
|
||||||
|
|
@ -125,7 +113,7 @@ class ScrapyH2Agent:
|
||||||
def _cb_latency(
|
def _cb_latency(
|
||||||
response: Response, request: Request, start_time: float
|
response: Response, request: Request, start_time: float
|
||||||
) -> Response:
|
) -> Response:
|
||||||
request.meta["download_latency"] = time() - start_time
|
request.meta["download_latency"] = monotonic() - start_time
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,17 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
import warnings
|
||||||
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
|
|
||||||
from scrapy.core.downloader.handlers.base import BaseDownloadHandler
|
from scrapy.core.downloader.handlers.base import BaseDownloadHandler
|
||||||
from scrapy.exceptions import NotConfigured
|
from scrapy.exceptions import NotConfigured, ScrapyDeprecationWarning
|
||||||
from scrapy.utils.boto import is_botocore_available
|
from scrapy.utils.boto import is_botocore_available
|
||||||
from scrapy.utils.httpobj import urlparse_cached
|
from scrapy.utils.httpobj import urlparse_cached
|
||||||
from scrapy.utils.misc import build_from_crawler, load_object
|
from scrapy.utils.misc import build_from_crawler, load_object
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Mapping
|
||||||
|
|
||||||
from scrapy import Request
|
from scrapy import Request
|
||||||
from scrapy.crawler import Crawler
|
from scrapy.crawler import Crawler
|
||||||
from scrapy.http import Response
|
from scrapy.http import Response
|
||||||
|
|
@ -39,7 +42,7 @@ class S3DownloadHandler(BaseDownloadHandler):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
_http_handler = build_from_crawler(
|
_http_handler: BaseDownloadHandler = build_from_crawler(
|
||||||
load_object(crawler.settings.getwithbase("DOWNLOAD_HANDLERS")["https"]),
|
load_object(crawler.settings.getwithbase("DOWNLOAD_HANDLERS")["https"]),
|
||||||
crawler,
|
crawler,
|
||||||
)
|
)
|
||||||
|
|
@ -47,7 +50,16 @@ class S3DownloadHandler(BaseDownloadHandler):
|
||||||
|
|
||||||
async def download_request(self, request: Request) -> Response:
|
async def download_request(self, request: Request) -> Response:
|
||||||
p = urlparse_cached(request)
|
p = urlparse_cached(request)
|
||||||
scheme = "https" if request.meta.get("is_secure") else "http"
|
if request.meta.get("is_secure") is False:
|
||||||
|
warnings.warn(
|
||||||
|
"Passing is_secure=False for s3:// requests is deprecated."
|
||||||
|
" In future Scrapy releases this flag will be ignored.",
|
||||||
|
ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
scheme = "http"
|
||||||
|
else:
|
||||||
|
scheme = "https"
|
||||||
bucket = p.hostname
|
bucket = p.hostname
|
||||||
path = p.path + "?" + p.query if p.query else p.path
|
path = p.path + "?" + p.query if p.query else p.path
|
||||||
url = f"{scheme}://{bucket}.s3.amazonaws.com{path}"
|
url = f"{scheme}://{bucket}.s3.amazonaws.com{path}"
|
||||||
|
|
@ -59,7 +71,7 @@ class S3DownloadHandler(BaseDownloadHandler):
|
||||||
awsrequest = botocore.awsrequest.AWSRequest(
|
awsrequest = botocore.awsrequest.AWSRequest(
|
||||||
method=request.method,
|
method=request.method,
|
||||||
url=f"{scheme}://s3.amazonaws.com/{bucket}{path}",
|
url=f"{scheme}://s3.amazonaws.com/{bucket}{path}",
|
||||||
headers=request.headers.to_unicode_dict(),
|
headers=cast("Mapping[str, Any]", request.headers.to_unicode_dict()),
|
||||||
data=request.body,
|
data=request.body,
|
||||||
)
|
)
|
||||||
assert self._signer
|
assert self._signer
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import warnings
|
import warnings
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import TYPE_CHECKING, Any, cast
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning, _InvalidOutput
|
from scrapy.exceptions import ScrapyDeprecationWarning, _InvalidOutput
|
||||||
from scrapy.http import Request, Response
|
from scrapy.http import Request, Response
|
||||||
|
|
@ -36,7 +36,9 @@ class DownloaderMiddlewareManager(MiddlewareManager):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_mwlist_from_settings(cls, settings: BaseSettings) -> list[Any]:
|
def _get_mwlist_from_settings(cls, settings: BaseSettings) -> list[Any]:
|
||||||
return build_component_list(settings.getwithbase("DOWNLOADER_MIDDLEWARES"))
|
return build_component_list(
|
||||||
|
settings.get_component_priority_dict_with_base("DOWNLOADER_MIDDLEWARES")
|
||||||
|
)
|
||||||
|
|
||||||
def _add_middleware(self, mw: Any) -> None:
|
def _add_middleware(self, mw: Any) -> None:
|
||||||
if hasattr(mw, "process_request"):
|
if hasattr(mw, "process_request"):
|
||||||
|
|
@ -73,87 +75,82 @@ class DownloaderMiddlewareManager(MiddlewareManager):
|
||||||
download_func: Callable[[Request], Coroutine[Any, Any, Response]],
|
download_func: Callable[[Request], Coroutine[Any, Any, Response]],
|
||||||
request: Request,
|
request: Request,
|
||||||
) -> Response | Request:
|
) -> Response | Request:
|
||||||
async def process_request(request: Request) -> Response | Request:
|
|
||||||
for method in self.methods["process_request"]:
|
|
||||||
method = cast("Callable", method)
|
|
||||||
if method in self._mw_methods_requiring_spider:
|
|
||||||
response = await ensure_awaitable(
|
|
||||||
method(request=request, spider=self._spider),
|
|
||||||
_warn=global_object_name(method),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
response = await ensure_awaitable(
|
|
||||||
method(request=request), _warn=global_object_name(method)
|
|
||||||
)
|
|
||||||
if response is not None and not isinstance(
|
|
||||||
response, (Response, Request)
|
|
||||||
):
|
|
||||||
raise _InvalidOutput(
|
|
||||||
f"Middleware {method.__qualname__} must return None, Response or "
|
|
||||||
f"Request, got {response.__class__.__name__}"
|
|
||||||
)
|
|
||||||
if response:
|
|
||||||
return response
|
|
||||||
return await download_func(request)
|
|
||||||
|
|
||||||
async def process_response(response: Response | Request) -> Response | Request:
|
|
||||||
if response is None:
|
|
||||||
raise TypeError("Received None in process_response")
|
|
||||||
if isinstance(response, Request):
|
|
||||||
return response
|
|
||||||
|
|
||||||
for method in self.methods["process_response"]:
|
|
||||||
method = cast("Callable", method)
|
|
||||||
if method in self._mw_methods_requiring_spider:
|
|
||||||
response = await ensure_awaitable(
|
|
||||||
method(request=request, response=response, spider=self._spider),
|
|
||||||
_warn=global_object_name(method),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
response = await ensure_awaitable(
|
|
||||||
method(request=request, response=response),
|
|
||||||
_warn=global_object_name(method),
|
|
||||||
)
|
|
||||||
if not isinstance(response, (Response, Request)):
|
|
||||||
raise _InvalidOutput(
|
|
||||||
f"Middleware {method.__qualname__} must return Response or Request, "
|
|
||||||
f"got {type(response)}"
|
|
||||||
)
|
|
||||||
if isinstance(response, Request):
|
|
||||||
return response
|
|
||||||
return response
|
|
||||||
|
|
||||||
async def process_exception(exception: Exception) -> Response | Request:
|
|
||||||
for method in self.methods["process_exception"]:
|
|
||||||
method = cast("Callable", method)
|
|
||||||
if method in self._mw_methods_requiring_spider:
|
|
||||||
response = await ensure_awaitable(
|
|
||||||
method(
|
|
||||||
request=request, exception=exception, spider=self._spider
|
|
||||||
),
|
|
||||||
_warn=global_object_name(method),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
response = await ensure_awaitable(
|
|
||||||
method(request=request, exception=exception),
|
|
||||||
_warn=global_object_name(method),
|
|
||||||
)
|
|
||||||
if response is not None and not isinstance(
|
|
||||||
response, (Response, Request)
|
|
||||||
):
|
|
||||||
raise _InvalidOutput(
|
|
||||||
f"Middleware {method.__qualname__} must return None, Response or "
|
|
||||||
f"Request, got {type(response)}"
|
|
||||||
)
|
|
||||||
if response:
|
|
||||||
return response
|
|
||||||
raise exception
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result: Response | Request = await process_request(request)
|
result: Response | Request = await self._process_request(
|
||||||
|
request, download_func
|
||||||
|
)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
await _defer_sleep_async()
|
await _defer_sleep_async()
|
||||||
# either returns a request or response (which we pass to process_response())
|
# either returns a request or response (which we pass to process_response())
|
||||||
# or reraises the exception
|
# or reraises the exception
|
||||||
result = await process_exception(ex)
|
result = await self._process_exception(ex, request)
|
||||||
return await process_response(result)
|
return await self._process_response(result, request)
|
||||||
|
|
||||||
|
def _handle_mw_method(self, method: Callable[..., Any], **kwargs: Any) -> Any:
|
||||||
|
if method in self._mw_methods_requiring_spider:
|
||||||
|
kwargs["spider"] = self._spider
|
||||||
|
|
||||||
|
return method(**kwargs)
|
||||||
|
|
||||||
|
async def _process_request(
|
||||||
|
self,
|
||||||
|
request: Request,
|
||||||
|
download_func: Callable[[Request], Coroutine[Any, Any, Response]],
|
||||||
|
) -> Response | Request:
|
||||||
|
for method in self.methods["process_request"]:
|
||||||
|
assert method is not None
|
||||||
|
response = await ensure_awaitable(
|
||||||
|
self._handle_mw_method(method, request=request),
|
||||||
|
_warn=global_object_name(method),
|
||||||
|
)
|
||||||
|
if response is not None and not isinstance(response, (Response, Request)):
|
||||||
|
raise _InvalidOutput(
|
||||||
|
f"Middleware {method.__qualname__} must return None, Response or "
|
||||||
|
f"Request, got {response.__class__.__name__}"
|
||||||
|
)
|
||||||
|
if response:
|
||||||
|
return response
|
||||||
|
return await download_func(request)
|
||||||
|
|
||||||
|
async def _process_response(
|
||||||
|
self, response: Response | Request, request: Request
|
||||||
|
) -> Response | Request:
|
||||||
|
if response is None:
|
||||||
|
raise TypeError("Received None in process_response")
|
||||||
|
if isinstance(response, Request):
|
||||||
|
return response
|
||||||
|
|
||||||
|
for method in self.methods["process_response"]:
|
||||||
|
assert method is not None
|
||||||
|
response = await ensure_awaitable(
|
||||||
|
self._handle_mw_method(method, request=request, response=response),
|
||||||
|
_warn=global_object_name(method),
|
||||||
|
)
|
||||||
|
|
||||||
|
if not isinstance(response, (Response, Request)):
|
||||||
|
raise _InvalidOutput(
|
||||||
|
f"Middleware {method.__qualname__} must return Response or Request, "
|
||||||
|
f"got {type(response)}"
|
||||||
|
)
|
||||||
|
if isinstance(response, Request):
|
||||||
|
return response
|
||||||
|
return response
|
||||||
|
|
||||||
|
async def _process_exception(
|
||||||
|
self, exception: Exception, request: Request | Response
|
||||||
|
) -> Response | Request:
|
||||||
|
for method in self.methods["process_exception"]:
|
||||||
|
assert method is not None
|
||||||
|
response = await ensure_awaitable(
|
||||||
|
self._handle_mw_method(method, request=request, exception=exception),
|
||||||
|
_warn=global_object_name(method),
|
||||||
|
)
|
||||||
|
if response is not None and not isinstance(response, (Response, Request)):
|
||||||
|
raise _InvalidOutput(
|
||||||
|
f"Middleware {method.__qualname__} must return None, Response or "
|
||||||
|
f"Request, got {type(response)}"
|
||||||
|
)
|
||||||
|
if response:
|
||||||
|
return response
|
||||||
|
raise exception
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,77 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
import warnings
|
||||||
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from OpenSSL import SSL
|
from OpenSSL import SSL
|
||||||
from service_identity import VerificationError
|
from service_identity import VerificationError
|
||||||
from service_identity.exceptions import CertificateError
|
from service_identity.exceptions import CertificateError
|
||||||
from service_identity.pyopenssl import verify_hostname, verify_ip_address
|
from service_identity.hazmat import (
|
||||||
|
DNS_ID,
|
||||||
|
IPAddress_ID,
|
||||||
|
ServiceID,
|
||||||
|
verify_service_identity,
|
||||||
|
)
|
||||||
|
from service_identity.pyopenssl import (
|
||||||
|
extract_patterns,
|
||||||
|
verify_hostname,
|
||||||
|
verify_ip_address,
|
||||||
|
)
|
||||||
from twisted.internet._sslverify import ClientTLSOptions
|
from twisted.internet._sslverify import ClientTLSOptions
|
||||||
from twisted.internet.ssl import AcceptableCiphers
|
from twisted.internet.ssl import AcceptableCiphers, TLSVersion
|
||||||
|
|
||||||
|
from scrapy.exceptions import ScrapyDeprecationWarning
|
||||||
from scrapy.utils.deprecate import create_deprecated_class
|
from scrapy.utils.deprecate import create_deprecated_class
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
from OpenSSL.crypto import X509
|
||||||
|
from twisted.protocols.tls import TLSMemoryBIOProtocol
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
METHOD_TLS = "TLS"
|
_openssl_methods: dict[str, int] = {
|
||||||
METHOD_TLSv10 = "TLSv1.0"
|
"TLS": SSL.SSLv23_METHOD, # protocol negotiation (recommended)
|
||||||
METHOD_TLSv11 = "TLSv1.1"
|
"TLSv1.0": SSL.TLSv1_METHOD, # TLS 1.0 only
|
||||||
METHOD_TLSv12 = "TLSv1.2"
|
"TLSv1.1": SSL.TLSv1_1_METHOD, # TLS 1.1 only
|
||||||
|
"TLSv1.2": SSL.TLSv1_2_METHOD, # TLS 1.2 only
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
openssl_methods: dict[str, int] = {
|
def __getattr__(name: str) -> Any:
|
||||||
METHOD_TLS: SSL.SSLv23_METHOD, # protocol negotiation (recommended)
|
if name == "DEFAULT_CIPHERS":
|
||||||
METHOD_TLSv10: SSL.TLSv1_METHOD, # TLS 1.0 only
|
warnings.warn(
|
||||||
METHOD_TLSv11: SSL.TLSv1_1_METHOD, # TLS 1.1 only
|
"scrapy.core.downloader.tls.DEFAULT_CIPHERS is deprecated.",
|
||||||
METHOD_TLSv12: SSL.TLSv1_2_METHOD, # TLS 1.2 only
|
ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
return AcceptableCiphers.fromOpenSSLCipherString("DEFAULT")
|
||||||
|
deprecated = {
|
||||||
|
"METHOD_TLS": "TLS",
|
||||||
|
"METHOD_TLSv10": "TLSv1.0",
|
||||||
|
"METHOD_TLSv11": "TLSv1.1",
|
||||||
|
"METHOD_TLSv12": "TLSv1.2",
|
||||||
|
"openssl_methods": _openssl_methods,
|
||||||
|
}
|
||||||
|
if name in deprecated:
|
||||||
|
warnings.warn(
|
||||||
|
f"scrapy.core.downloader.tls.{name} is deprecated.",
|
||||||
|
ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
return deprecated[name]
|
||||||
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||||
|
|
||||||
|
|
||||||
|
_TWISTED_VERSION_MAP: dict[str, TLSVersion] = {
|
||||||
|
"TLSv1.0": TLSVersion.TLSv1_0,
|
||||||
|
"TLSv1.1": TLSVersion.TLSv1_1,
|
||||||
|
"TLSv1.2": TLSVersion.TLSv1_2,
|
||||||
|
"TLSv1.3": TLSVersion.TLSv1_3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -39,6 +87,8 @@ class _ScrapyClientTLSOptions(ClientTLSOptions):
|
||||||
|
|
||||||
Instances of this class are returned from
|
Instances of this class are returned from
|
||||||
:class:`._ScrapyClientContextFactory`.
|
:class:`._ScrapyClientContextFactory`.
|
||||||
|
|
||||||
|
This class is used on Twisted older than 26.4.0.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _identityVerifyingInfoCallback(
|
def _identityVerifyingInfoCallback(
|
||||||
|
|
@ -64,7 +114,7 @@ class _ScrapyClientTLSOptions(ClientTLSOptions):
|
||||||
e,
|
e,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
super()._identityVerifyingInfoCallback(connection, where, ret) # type: ignore[no-untyped-call]
|
super()._identityVerifyingInfoCallback(connection, where, ret) # type: ignore[misc]
|
||||||
|
|
||||||
|
|
||||||
ScrapyClientTLSOptions = create_deprecated_class(
|
ScrapyClientTLSOptions = create_deprecated_class(
|
||||||
|
|
@ -75,6 +125,62 @@ ScrapyClientTLSOptions = create_deprecated_class(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_CIPHERS: AcceptableCiphers = AcceptableCiphers.fromOpenSSLCipherString(
|
class _ScrapyClientTLSOptions26(ClientTLSOptions):
|
||||||
"DEFAULT"
|
"""
|
||||||
)
|
SSL Client connection creator ignoring certificate verification errors
|
||||||
|
(for genuinely invalid certificates or bugs in verification code).
|
||||||
|
|
||||||
|
Same as Twisted's private _sslverify.ClientTLSOptions,
|
||||||
|
except that VerificationError, CertificateError and ValueError
|
||||||
|
exceptions are caught, so that the connection is not closed, only
|
||||||
|
logging warnings.
|
||||||
|
|
||||||
|
Instances of this class are returned from
|
||||||
|
:class:`._ScrapyClientContextFactory`.
|
||||||
|
|
||||||
|
This class is used on Twisted 26.4.0 and newer.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def clientConnectionForTLS(
|
||||||
|
self, tlsProtocol: TLSMemoryBIOProtocol
|
||||||
|
) -> SSL.Connection:
|
||||||
|
"""This method is needed to override the verify callback."""
|
||||||
|
conn = super().clientConnectionForTLS(tlsProtocol)
|
||||||
|
callback = self._verifyCB(self._hostnameIsDnsName, self._hostnameASCII)
|
||||||
|
conn.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, callback)
|
||||||
|
return conn
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _verifyCB(
|
||||||
|
hostIsDNS: bool, hostnameASCII: str
|
||||||
|
) -> Callable[[SSL.Connection, X509, int, int, int], bool]:
|
||||||
|
svcid: ServiceID = (
|
||||||
|
DNS_ID(hostnameASCII) if hostIsDNS else IPAddress_ID(hostnameASCII)
|
||||||
|
)
|
||||||
|
|
||||||
|
def verifyCallback(
|
||||||
|
conn: SSL.Connection, cert: X509, err: int, depth: int, ok: int
|
||||||
|
) -> bool:
|
||||||
|
if depth != 0:
|
||||||
|
# We are only verifying the leaf certificate.
|
||||||
|
return True
|
||||||
|
|
||||||
|
try:
|
||||||
|
verify_service_identity(extract_patterns(cert), [svcid], [])
|
||||||
|
except (CertificateError, VerificationError) as e:
|
||||||
|
logger.warning(
|
||||||
|
'Remote certificate is not valid for hostname "%s"; %s',
|
||||||
|
hostnameASCII,
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
except ValueError as e:
|
||||||
|
logger.warning(
|
||||||
|
"Ignoring error while verifying certificate "
|
||||||
|
'from host "%s" (exception: %r)',
|
||||||
|
hostnameASCII,
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
return verifyCallback
|
||||||
|
|
|
||||||
|
|
@ -1,239 +0,0 @@
|
||||||
"""Deprecated HTTP/1.0 helper classes used by HTTP10DownloadHandler."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import warnings
|
|
||||||
from time import time
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
from urllib.parse import urldefrag, urlparse, urlunparse
|
|
||||||
|
|
||||||
from twisted.internet import defer
|
|
||||||
from twisted.internet.protocol import ClientFactory
|
|
||||||
from twisted.web.http import HTTPClient
|
|
||||||
|
|
||||||
from scrapy.exceptions import DownloadTimeoutError, ScrapyDeprecationWarning
|
|
||||||
from scrapy.http import Headers, Response
|
|
||||||
from scrapy.responsetypes import responsetypes
|
|
||||||
from scrapy.utils.httpobj import urlparse_cached
|
|
||||||
from scrapy.utils.python import to_bytes, to_unicode
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from scrapy import Request
|
|
||||||
|
|
||||||
|
|
||||||
class ScrapyHTTPPageGetter(HTTPClient):
|
|
||||||
delimiter = b"\n"
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
warnings.warn(
|
|
||||||
"ScrapyHTTPPageGetter is deprecated and will be removed in a future Scrapy version.",
|
|
||||||
category=ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
def connectionMade(self):
|
|
||||||
self.headers = Headers() # bucket for response headers
|
|
||||||
|
|
||||||
# Method command
|
|
||||||
self.sendCommand(self.factory.method, self.factory.path)
|
|
||||||
# Headers
|
|
||||||
for key, values in self.factory.headers.items():
|
|
||||||
for value in values:
|
|
||||||
self.sendHeader(key, value)
|
|
||||||
self.endHeaders()
|
|
||||||
# Body
|
|
||||||
if self.factory.body is not None:
|
|
||||||
self.transport.write(self.factory.body)
|
|
||||||
|
|
||||||
def lineReceived(self, line):
|
|
||||||
return HTTPClient.lineReceived(self, line.rstrip())
|
|
||||||
|
|
||||||
def handleHeader(self, key, value):
|
|
||||||
self.headers.appendlist(key, value)
|
|
||||||
|
|
||||||
def handleStatus(self, version, status, message):
|
|
||||||
self.factory.gotStatus(version, status, message)
|
|
||||||
|
|
||||||
def handleEndHeaders(self):
|
|
||||||
self.factory.gotHeaders(self.headers)
|
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
|
||||||
self._connection_lost_reason = reason
|
|
||||||
HTTPClient.connectionLost(self, reason)
|
|
||||||
self.factory.noPage(reason)
|
|
||||||
|
|
||||||
def handleResponse(self, response):
|
|
||||||
if self.factory.method.upper() == b"HEAD":
|
|
||||||
self.factory.page(b"")
|
|
||||||
elif self.length is not None and self.length > 0:
|
|
||||||
self.factory.noPage(self._connection_lost_reason)
|
|
||||||
else:
|
|
||||||
self.factory.page(response)
|
|
||||||
self.transport.loseConnection()
|
|
||||||
|
|
||||||
def timeout(self):
|
|
||||||
self.transport.loseConnection()
|
|
||||||
|
|
||||||
# transport cleanup needed for HTTPS connections
|
|
||||||
if self.factory.url.startswith(b"https"):
|
|
||||||
self.transport.stopProducing()
|
|
||||||
|
|
||||||
self.factory.noPage(
|
|
||||||
DownloadTimeoutError(
|
|
||||||
f"Getting {self.factory.url} took longer "
|
|
||||||
f"than {self.factory.timeout} seconds."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# This class used to inherit from Twisted’s
|
|
||||||
# twisted.web.client.HTTPClientFactory. When that class was deprecated in
|
|
||||||
# Twisted (https://github.com/twisted/twisted/pull/643), we merged its
|
|
||||||
# non-overridden code into this class.
|
|
||||||
class ScrapyHTTPClientFactory(ClientFactory):
|
|
||||||
protocol = ScrapyHTTPPageGetter
|
|
||||||
|
|
||||||
waiting = 1
|
|
||||||
noisy = False
|
|
||||||
followRedirect = False
|
|
||||||
afterFoundGet = False
|
|
||||||
|
|
||||||
def _build_response(self, body, request):
|
|
||||||
request.meta["download_latency"] = self.headers_time - self.start_time
|
|
||||||
status = int(self.status)
|
|
||||||
headers = Headers(self.response_headers)
|
|
||||||
respcls = responsetypes.from_args(headers=headers, url=self._url, body=body)
|
|
||||||
return respcls(
|
|
||||||
url=self._url,
|
|
||||||
status=status,
|
|
||||||
headers=headers,
|
|
||||||
body=body,
|
|
||||||
protocol=to_unicode(self.version),
|
|
||||||
)
|
|
||||||
|
|
||||||
def _set_connection_attributes(self, request):
|
|
||||||
proxy = request.meta.get("proxy")
|
|
||||||
if proxy:
|
|
||||||
proxy_parsed = urlparse(to_bytes(proxy, encoding="ascii"))
|
|
||||||
self.scheme = proxy_parsed.scheme
|
|
||||||
self.host = proxy_parsed.hostname
|
|
||||||
self.port = proxy_parsed.port
|
|
||||||
self.netloc = proxy_parsed.netloc
|
|
||||||
if self.port is None:
|
|
||||||
self.port = 443 if proxy_parsed.scheme == b"https" else 80
|
|
||||||
self.path = self.url
|
|
||||||
else:
|
|
||||||
parsed = urlparse_cached(request)
|
|
||||||
path_str = urlunparse(
|
|
||||||
("", "", parsed.path or "/", parsed.params, parsed.query, "")
|
|
||||||
)
|
|
||||||
self.path = to_bytes(path_str, encoding="ascii")
|
|
||||||
assert parsed.hostname is not None
|
|
||||||
self.host = to_bytes(parsed.hostname, encoding="ascii")
|
|
||||||
self.port = parsed.port
|
|
||||||
self.scheme = to_bytes(parsed.scheme, encoding="ascii")
|
|
||||||
self.netloc = to_bytes(parsed.netloc, encoding="ascii")
|
|
||||||
if self.port is None:
|
|
||||||
self.port = 443 if self.scheme == b"https" else 80
|
|
||||||
|
|
||||||
def __init__(self, request: Request, timeout: float = 180):
|
|
||||||
warnings.warn(
|
|
||||||
"ScrapyHTTPClientFactory is deprecated and will be removed in a future Scrapy version.",
|
|
||||||
category=ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
self._url: str = urldefrag(request.url)[0]
|
|
||||||
# converting to bytes to comply to Twisted interface
|
|
||||||
self.url: bytes = to_bytes(self._url, encoding="ascii")
|
|
||||||
self.method: bytes = to_bytes(request.method, encoding="ascii")
|
|
||||||
self.body: bytes | None = request.body or None
|
|
||||||
self.headers: Headers = Headers(request.headers)
|
|
||||||
self.response_headers: Headers | None = None
|
|
||||||
self.timeout: float = request.meta.get("download_timeout") or timeout
|
|
||||||
self.start_time: float = time()
|
|
||||||
self.deferred: defer.Deferred[Response] = defer.Deferred().addCallback(
|
|
||||||
self._build_response, request
|
|
||||||
)
|
|
||||||
|
|
||||||
# Fixes Twisted 11.1.0+ support as HTTPClientFactory is expected
|
|
||||||
# to have _disconnectedDeferred. See Twisted r32329.
|
|
||||||
# As Scrapy implements it's own logic to handle redirects is not
|
|
||||||
# needed to add the callback _waitForDisconnect.
|
|
||||||
# Specifically this avoids the AttributeError exception when
|
|
||||||
# clientConnectionFailed method is called.
|
|
||||||
self._disconnectedDeferred: defer.Deferred[None] = defer.Deferred()
|
|
||||||
|
|
||||||
self._set_connection_attributes(request)
|
|
||||||
|
|
||||||
# set Host header based on url
|
|
||||||
self.headers.setdefault("Host", self.netloc)
|
|
||||||
|
|
||||||
# set Content-Length based len of body
|
|
||||||
if self.body is not None:
|
|
||||||
self.headers["Content-Length"] = len(self.body)
|
|
||||||
# just in case a broken http/1.1 decides to keep connection alive
|
|
||||||
self.headers.setdefault("Connection", "close")
|
|
||||||
# Content-Length must be specified in POST method even with no body
|
|
||||||
elif self.method == b"POST":
|
|
||||||
self.headers["Content-Length"] = 0
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return f"<{self.__class__.__name__}: {self._url}>"
|
|
||||||
|
|
||||||
def _cancelTimeout(self, result, timeoutCall):
|
|
||||||
if timeoutCall.active():
|
|
||||||
timeoutCall.cancel()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def buildProtocol(self, addr):
|
|
||||||
p = ClientFactory.buildProtocol(self, addr)
|
|
||||||
p.followRedirect = self.followRedirect
|
|
||||||
p.afterFoundGet = self.afterFoundGet
|
|
||||||
if self.timeout:
|
|
||||||
from twisted.internet import reactor
|
|
||||||
|
|
||||||
timeoutCall = reactor.callLater(self.timeout, p.timeout)
|
|
||||||
self.deferred.addBoth(self._cancelTimeout, timeoutCall)
|
|
||||||
return p
|
|
||||||
|
|
||||||
def gotHeaders(self, headers):
|
|
||||||
self.headers_time = time()
|
|
||||||
self.response_headers = headers
|
|
||||||
|
|
||||||
def gotStatus(self, version, status, message):
|
|
||||||
"""
|
|
||||||
Set the status of the request on us.
|
|
||||||
@param version: The HTTP version.
|
|
||||||
@type version: L{bytes}
|
|
||||||
@param status: The HTTP status code, an integer represented as a
|
|
||||||
bytestring.
|
|
||||||
@type status: L{bytes}
|
|
||||||
@param message: The HTTP status message.
|
|
||||||
@type message: L{bytes}
|
|
||||||
"""
|
|
||||||
self.version, self.status, self.message = version, status, message
|
|
||||||
|
|
||||||
def page(self, page):
|
|
||||||
if self.waiting:
|
|
||||||
self.waiting = 0
|
|
||||||
self.deferred.callback(page)
|
|
||||||
|
|
||||||
def noPage(self, reason):
|
|
||||||
if self.waiting:
|
|
||||||
self.waiting = 0
|
|
||||||
self.deferred.errback(reason)
|
|
||||||
|
|
||||||
def clientConnectionFailed(self, _, reason):
|
|
||||||
"""
|
|
||||||
When a connection attempt fails, the request cannot be issued. If no
|
|
||||||
result has yet been provided to the result Deferred, provide the
|
|
||||||
connection failure reason as an error result.
|
|
||||||
"""
|
|
||||||
if self.waiting:
|
|
||||||
self.waiting = 0
|
|
||||||
# If the connection attempt failed, there is nothing more to
|
|
||||||
# disconnect, so just fire that Deferred now.
|
|
||||||
self._disconnectedDeferred.callback(None)
|
|
||||||
self.deferred.errback(reason)
|
|
||||||
|
|
@ -11,6 +11,7 @@ import asyncio
|
||||||
import contextlib
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
import warnings
|
import warnings
|
||||||
|
from functools import partial
|
||||||
from time import time
|
from time import time
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
@ -273,7 +274,7 @@ class ExecutionEngine:
|
||||||
"""
|
"""
|
||||||
assert self._start is not None
|
assert self._start is not None
|
||||||
try:
|
try:
|
||||||
item_or_request = await self._start.__anext__()
|
item_or_request = await anext(self._start)
|
||||||
except StopAsyncIteration:
|
except StopAsyncIteration:
|
||||||
self._start = None
|
self._start = None
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
|
|
@ -352,6 +353,10 @@ class ExecutionEngine:
|
||||||
or self.scraper.slot.needs_backout()
|
or self.scraper.slot.needs_backout()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _remove_request(self, _: Any, request: Request) -> None:
|
||||||
|
assert self._slot
|
||||||
|
self._slot.remove_request(request)
|
||||||
|
|
||||||
def _start_scheduled_request(self) -> bool:
|
def _start_scheduled_request(self) -> bool:
|
||||||
assert self._slot is not None # typing
|
assert self._slot is not None # typing
|
||||||
assert self.spider is not None # typing
|
assert self.spider is not None # typing
|
||||||
|
|
@ -371,11 +376,7 @@ class ExecutionEngine:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _remove_request(_: Any) -> None:
|
d2: Deferred[None] = d.addBoth(partial(self._remove_request, request=request))
|
||||||
assert self._slot
|
|
||||||
self._slot.remove_request(request)
|
|
||||||
|
|
||||||
d2: Deferred[None] = d.addBoth(_remove_request)
|
|
||||||
d2.addErrback(
|
d2.addErrback(
|
||||||
lambda f: logger.info(
|
lambda f: logger.info(
|
||||||
"Error while removing request from slot",
|
"Error while removing request from slot",
|
||||||
|
|
@ -612,30 +613,33 @@ class ExecutionEngine:
|
||||||
"Closing spider (%(reason)s)", {"reason": reason}, extra={"spider": spider}
|
"Closing spider (%(reason)s)", {"reason": reason}, extra={"spider": spider}
|
||||||
)
|
)
|
||||||
|
|
||||||
def log_failure(msg: str) -> None:
|
|
||||||
logger.error(msg, exc_info=True, extra={"spider": spider}) # noqa: LOG014
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self._slot.close()
|
await self._slot.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Slot close failure")
|
logger.error("Slot close failure", exc_info=True, extra={"spider": spider})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.downloader.close()
|
self.downloader.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Downloader close failure")
|
logger.error(
|
||||||
|
"Downloader close failure", exc_info=True, extra={"spider": spider}
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self.scraper.close_spider_async()
|
await self.scraper.close_spider_async()
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Scraper close failure")
|
logger.error(
|
||||||
|
"Scraper close failure", exc_info=True, extra={"spider": spider}
|
||||||
|
)
|
||||||
|
|
||||||
if hasattr(self._slot.scheduler, "close"):
|
if hasattr(self._slot.scheduler, "close"):
|
||||||
try:
|
try:
|
||||||
if (d := self._slot.scheduler.close(reason)) is not None:
|
if (d := self._slot.scheduler.close(reason)) is not None:
|
||||||
await maybe_deferred_to_future(d)
|
await maybe_deferred_to_future(d)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Scheduler close failure")
|
logger.error(
|
||||||
|
"Scheduler close failure", exc_info=True, extra={"spider": spider}
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self.signals.send_catch_log_async(
|
await self.signals.send_catch_log_async(
|
||||||
|
|
@ -644,7 +648,11 @@ class ExecutionEngine:
|
||||||
reason=reason,
|
reason=reason,
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Error while sending spider_close signal")
|
logger.error(
|
||||||
|
"Error while sending spider_close signal",
|
||||||
|
exc_info=True,
|
||||||
|
extra={"spider": spider},
|
||||||
|
)
|
||||||
|
|
||||||
assert self.crawler.stats
|
assert self.crawler.stats
|
||||||
try:
|
try:
|
||||||
|
|
@ -661,7 +669,7 @@ class ExecutionEngine:
|
||||||
else:
|
else:
|
||||||
self.crawler.stats.close_spider(reason=reason)
|
self.crawler.stats.close_spider(reason=reason)
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Stats close failure")
|
logger.error("Stats close failure")
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
"Spider closed (%(reason)s)",
|
"Spider closed (%(reason)s)",
|
||||||
|
|
@ -675,4 +683,4 @@ class ExecutionEngine:
|
||||||
try:
|
try:
|
||||||
await ensure_awaitable(self._spider_closed_callback(spider))
|
await ensure_awaitable(self._spider_closed_callback(spider))
|
||||||
except Exception:
|
except Exception:
|
||||||
log_failure("Error running spider_closed_callback")
|
logger.error("Error running spider_closed_callback")
|
||||||
|
|
|
||||||
|
|
@ -114,11 +114,7 @@ class H2ConnectionPool:
|
||||||
d.errback(ResponseFailed(errors))
|
d.errback(ResponseFailed(errors))
|
||||||
|
|
||||||
def close_connections(self) -> None:
|
def close_connections(self) -> None:
|
||||||
"""Close all the HTTP/2 connections and remove them from pool
|
"""Close all the HTTP/2 connections and remove them from pool."""
|
||||||
|
|
||||||
Returns:
|
|
||||||
Deferred that fires when all connections have been closed
|
|
||||||
"""
|
|
||||||
for conn in self._connections.values():
|
for conn in self._connections.values():
|
||||||
assert conn.transport is not None # typing
|
assert conn.transport is not None # typing
|
||||||
conn.transport.abortConnection()
|
conn.transport.abortConnection()
|
||||||
|
|
@ -143,7 +139,7 @@ class H2Agent:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_endpoint(self, uri: URI) -> HostnameEndpoint:
|
def get_endpoint(self, uri: URI) -> HostnameEndpoint:
|
||||||
return self.endpoint_factory.endpointForURI(uri)
|
return self.endpoint_factory.endpointForURI(uri) # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_key(self, uri: URI) -> ConnectionKeyT:
|
def get_key(self, uri: URI) -> ConnectionKeyT:
|
||||||
"""
|
"""
|
||||||
|
|
@ -165,30 +161,3 @@ class H2Agent:
|
||||||
lambda conn: conn.request(request, spider)
|
lambda conn: conn.request(request, spider)
|
||||||
)
|
)
|
||||||
return d2
|
return d2
|
||||||
|
|
||||||
|
|
||||||
class ScrapyProxyH2Agent(H2Agent):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
reactor: ReactorBase,
|
|
||||||
proxy_uri: URI,
|
|
||||||
pool: H2ConnectionPool,
|
|
||||||
context_factory: BrowserLikePolicyForHTTPS = BrowserLikePolicyForHTTPS(), # noqa: B008
|
|
||||||
connect_timeout: float | None = None,
|
|
||||||
bind_address: tuple[str, int] | None = None,
|
|
||||||
) -> None:
|
|
||||||
super().__init__(
|
|
||||||
reactor=reactor,
|
|
||||||
pool=pool,
|
|
||||||
context_factory=context_factory,
|
|
||||||
connect_timeout=connect_timeout,
|
|
||||||
bind_address=bind_address,
|
|
||||||
)
|
|
||||||
self._proxy_uri = proxy_uri
|
|
||||||
|
|
||||||
def get_endpoint(self, uri: URI) -> HostnameEndpoint:
|
|
||||||
return self.endpoint_factory.endpointForURI(self._proxy_uri)
|
|
||||||
|
|
||||||
def get_key(self, uri: URI) -> ConnectionKeyT:
|
|
||||||
"""We use the proxy uri instead of uri obtained from request url"""
|
|
||||||
return b"http-proxy", self._proxy_uri.host, self._proxy_uri.port
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ from scrapy.utils.ssl import _log_ssl_conn_debug_info
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ipaddress import IPv4Address, IPv6Address
|
from ipaddress import IPv4Address, IPv6Address
|
||||||
|
|
||||||
from hpack import HeaderTuple
|
|
||||||
from twisted.internet.defer import Deferred
|
from twisted.internet.defer import Deferred
|
||||||
from twisted.python.failure import Failure
|
from twisted.python.failure import Failure
|
||||||
from twisted.web.client import URI
|
from twisted.web.client import URI
|
||||||
|
|
@ -102,7 +101,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):
|
||||||
uri is used to verify that incoming client requests have correct
|
uri is used to verify that incoming client requests have correct
|
||||||
base URL.
|
base URL.
|
||||||
settings -- Scrapy project settings
|
settings -- Scrapy project settings
|
||||||
conn_lost_deferred -- Deferred fires with the reason: Failure to notify
|
conn_lost_deferred -- Deferred that fires with the list of underlying exceptions to notify
|
||||||
that connection was lost
|
that connection was lost
|
||||||
tls_verbose_logging -- Whether to log TLS details
|
tls_verbose_logging -- Whether to log TLS details
|
||||||
"""
|
"""
|
||||||
|
|
@ -376,7 +375,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):
|
||||||
|
|
||||||
def _handle_events(self, events: list[Event]) -> None:
|
def _handle_events(self, events: list[Event]) -> None:
|
||||||
"""Private method which acts as a bridge between the events
|
"""Private method which acts as a bridge between the events
|
||||||
received from the HTTP/2 data and IH2EventsHandler
|
received from the HTTP/2 data and the handlers in this class.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
events -- A list of events that the remote peer triggered by sending data
|
events -- A list of events that the remote peer triggered by sending data
|
||||||
|
|
@ -419,7 +418,7 @@ class H2ClientProtocol(Protocol, TimeoutMixin):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass # We ignore server-initiated events
|
pass # We ignore server-initiated events
|
||||||
else:
|
else:
|
||||||
stream.receive_headers(cast("list[HeaderTuple]", event.headers))
|
stream.receive_headers(cast("list[tuple[str, str]]", event.headers))
|
||||||
|
|
||||||
def settings_acknowledged(self, event: SettingsAcknowledged) -> None:
|
def settings_acknowledged(self, event: SettingsAcknowledged) -> None:
|
||||||
self.metadata["settings_acknowledged"] = True
|
self.metadata["settings_acknowledged"] = True
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,6 @@ from scrapy.utils.httpobj import urlparse_cached
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
from hpack import HeaderTuple
|
|
||||||
|
|
||||||
from scrapy.core.http2.protocol import H2ClientProtocol
|
from scrapy.core.http2.protocol import H2ClientProtocol
|
||||||
from scrapy.http import Request, Response
|
from scrapy.http import Request, Response
|
||||||
|
|
||||||
|
|
@ -153,18 +151,20 @@ class Stream:
|
||||||
"flow_controlled_size": 0,
|
"flow_controlled_size": 0,
|
||||||
# Headers received after sending the request
|
# Headers received after sending the request
|
||||||
"headers": Headers(),
|
"headers": Headers(),
|
||||||
|
# Response status code
|
||||||
|
"status": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
def _cancel(_: Any) -> None:
|
self._deferred_response: Deferred[Response] = Deferred(self._cancel)
|
||||||
# Close this stream as gracefully as possible
|
|
||||||
# If the associated request is initiated we reset this stream
|
|
||||||
# else we directly call close() method
|
|
||||||
if self.metadata["request_sent"]:
|
|
||||||
self.reset_stream(StreamCloseReason.CANCELLED)
|
|
||||||
else:
|
|
||||||
self.close(StreamCloseReason.CANCELLED)
|
|
||||||
|
|
||||||
self._deferred_response: Deferred[Response] = Deferred(_cancel)
|
def _cancel(self, _: Any) -> None:
|
||||||
|
# Close this stream as gracefully as possible
|
||||||
|
# If the associated request is initiated we reset this stream
|
||||||
|
# else we directly call close() method
|
||||||
|
if self.metadata["request_sent"]:
|
||||||
|
self.reset_stream(StreamCloseReason.CANCELLED)
|
||||||
|
else:
|
||||||
|
self.close(StreamCloseReason.CANCELLED)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Stream(id={self.stream_id!r})"
|
return f"Stream(id={self.stream_id!r})"
|
||||||
|
|
@ -361,9 +361,13 @@ class Stream:
|
||||||
self._response["flow_controlled_size"], self.stream_id
|
self._response["flow_controlled_size"], self.stream_id
|
||||||
)
|
)
|
||||||
|
|
||||||
def receive_headers(self, headers: list[HeaderTuple]) -> None:
|
def receive_headers(self, headers: list[tuple[str, str]]) -> None:
|
||||||
for name, value in headers:
|
for name, value in headers:
|
||||||
self._response["headers"].appendlist(name, value)
|
if name == ":status":
|
||||||
|
# it's a pseudo-header
|
||||||
|
self._response["status"] = int(value)
|
||||||
|
else:
|
||||||
|
self._response["headers"].appendlist(name, value)
|
||||||
|
|
||||||
# Check if we exceed the allowed max data size which can be received
|
# Check if we exceed the allowed max data size which can be received
|
||||||
expected_size = int(self._response["headers"].get(b"Content-Length", -1))
|
expected_size = int(self._response["headers"].get(b"Content-Length", -1))
|
||||||
|
|
@ -453,7 +457,8 @@ class Stream:
|
||||||
|
|
||||||
# There maybe no :status in headers, we make
|
# There maybe no :status in headers, we make
|
||||||
# HTTP Status Code: 499 - Client Closed Request
|
# HTTP Status Code: 499 - Client Closed Request
|
||||||
self._response["headers"][":status"] = "499"
|
if self._response["status"] is None:
|
||||||
|
self._response["status"] = 499
|
||||||
self._fire_response_deferred()
|
self._fire_response_deferred()
|
||||||
|
|
||||||
elif reason is StreamCloseReason.RESET:
|
elif reason is StreamCloseReason.RESET:
|
||||||
|
|
@ -492,7 +497,7 @@ class Stream:
|
||||||
|
|
||||||
response = make_response(
|
response = make_response(
|
||||||
url=self._request.url,
|
url=self._request.url,
|
||||||
status=int(self._response["headers"][":status"]),
|
status=self._response["status"],
|
||||||
headers=self._response["headers"],
|
headers=self._response["headers"],
|
||||||
body=self._response["body"].getvalue(),
|
body=self._response["body"].getvalue(),
|
||||||
certificate=self._protocol.metadata["certificate"],
|
certificate=self._protocol.metadata["certificate"],
|
||||||
|
|
|
||||||
|
|
@ -4,17 +4,14 @@ import json
|
||||||
import logging
|
import logging
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
from warnings import warn
|
|
||||||
|
|
||||||
# working around https://github.com/sphinx-doc/sphinx/issues/10400
|
# working around https://github.com/sphinx-doc/sphinx/issues/10400
|
||||||
from twisted.internet.defer import Deferred # noqa: TC002
|
from twisted.internet.defer import Deferred # noqa: TC002
|
||||||
|
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning
|
|
||||||
from scrapy.spiders import Spider # noqa: TC001
|
from scrapy.spiders import Spider # noqa: TC001
|
||||||
from scrapy.utils.job import job_dir
|
from scrapy.utils.job import job_dir
|
||||||
from scrapy.utils.misc import build_from_crawler, load_object
|
from scrapy.utils.misc import build_from_crawler, load_object
|
||||||
from scrapy.utils.python import global_object_name
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
# requires queuelib >= 1.6.2
|
# requires queuelib >= 1.6.2
|
||||||
|
|
@ -134,10 +131,10 @@ class Scheduler(BaseScheduler):
|
||||||
(:setting:`SCHEDULER_PRIORITY_QUEUE`) that sort requests by
|
(:setting:`SCHEDULER_PRIORITY_QUEUE`) that sort requests by
|
||||||
:attr:`~scrapy.http.Request.priority`.
|
:attr:`~scrapy.http.Request.priority`.
|
||||||
|
|
||||||
By default, a single, memory-based priority queue is used for all requests.
|
By default, memory-based priority queues are used for all requests.
|
||||||
When using :setting:`JOBDIR`, a disk-based priority queue is also created,
|
When using :setting:`JOBDIR`, disk-based priority queues are also created,
|
||||||
and only unserializable requests are stored in the memory-based priority
|
and only unserializable requests are stored in the memory-based priority
|
||||||
queue. For a given priority value, requests in memory take precedence over
|
queues. For a given priority value, requests in memory take precedence over
|
||||||
requests in disk.
|
requests in disk.
|
||||||
|
|
||||||
Each priority queue stores requests in separate internal queues, one per
|
Each priority queue stores requests in separate internal queues, one per
|
||||||
|
|
@ -212,8 +209,8 @@ class Scheduler(BaseScheduler):
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
While pending requests are below the configured values of
|
While pending requests are below the configured values of
|
||||||
:setting:`CONCURRENT_REQUESTS`, :setting:`CONCURRENT_REQUESTS_PER_DOMAIN`
|
:setting:`CONCURRENT_REQUESTS` or
|
||||||
or :setting:`CONCURRENT_REQUESTS_PER_IP`, those requests are sent
|
:setting:`CONCURRENT_REQUESTS_PER_DOMAIN`, those requests are sent
|
||||||
concurrently.
|
concurrently.
|
||||||
|
|
||||||
As a result, the first few requests of a crawl may not follow the desired
|
As a result, the first few requests of a crawl may not follow the desired
|
||||||
|
|
@ -292,11 +289,11 @@ class Scheduler(BaseScheduler):
|
||||||
|
|
||||||
:param dqclass: A class to be used as persistent request queue.
|
:param dqclass: A class to be used as persistent request queue.
|
||||||
The value for the :setting:`SCHEDULER_DISK_QUEUE` setting is used by default.
|
The value for the :setting:`SCHEDULER_DISK_QUEUE` setting is used by default.
|
||||||
:type dqclass: class
|
:type dqclass: type
|
||||||
|
|
||||||
:param mqclass: A class to be used as non-persistent request queue.
|
:param mqclass: A class to be used as non-persistent request queue.
|
||||||
The value for the :setting:`SCHEDULER_MEMORY_QUEUE` setting is used by default.
|
The value for the :setting:`SCHEDULER_MEMORY_QUEUE` setting is used by default.
|
||||||
:type mqclass: class
|
:type mqclass: type
|
||||||
|
|
||||||
:param logunser: A boolean that indicates whether or not unserializable requests should be logged.
|
:param logunser: A boolean that indicates whether or not unserializable requests should be logged.
|
||||||
The value for the :setting:`SCHEDULER_DEBUG` setting is used by default.
|
The value for the :setting:`SCHEDULER_DEBUG` setting is used by default.
|
||||||
|
|
@ -309,7 +306,7 @@ class Scheduler(BaseScheduler):
|
||||||
|
|
||||||
:param pqclass: A class to be used as priority queue for requests.
|
:param pqclass: A class to be used as priority queue for requests.
|
||||||
The value for the :setting:`SCHEDULER_PRIORITY_QUEUE` setting is used by default.
|
The value for the :setting:`SCHEDULER_PRIORITY_QUEUE` setting is used by default.
|
||||||
:type pqclass: class
|
:type pqclass: type
|
||||||
|
|
||||||
:param crawler: The crawler object corresponding to the current crawl.
|
:param crawler: The crawler object corresponding to the current crawl.
|
||||||
:type crawler: :class:`scrapy.crawler.Crawler`
|
:type crawler: :class:`scrapy.crawler.Crawler`
|
||||||
|
|
@ -337,7 +334,7 @@ class Scheduler(BaseScheduler):
|
||||||
cls = crawler.settings[f"SCHEDULER_START_{queue}_QUEUE"]
|
cls = crawler.settings[f"SCHEDULER_START_{queue}_QUEUE"]
|
||||||
if not cls:
|
if not cls:
|
||||||
return None
|
return None
|
||||||
return load_object(cls)
|
return cast("type[BaseQueue]", load_object(cls))
|
||||||
|
|
||||||
def has_pending_requests(self) -> bool:
|
def has_pending_requests(self) -> bool:
|
||||||
return len(self) > 0
|
return len(self) > 0
|
||||||
|
|
@ -345,7 +342,7 @@ class Scheduler(BaseScheduler):
|
||||||
def open(self, spider: Spider) -> Deferred[None] | None:
|
def open(self, spider: Spider) -> Deferred[None] | None:
|
||||||
"""
|
"""
|
||||||
(1) initialize the memory queue
|
(1) initialize the memory queue
|
||||||
(2) initialize the disk queue if the ``jobdir`` attribute is a valid directory
|
(2) initialize the disk queue if the ``jobdir`` argument wasn't empty
|
||||||
(3) return the result of the dupefilter's ``open`` method
|
(3) return the result of the dupefilter's ``open`` method
|
||||||
"""
|
"""
|
||||||
self.spider: Spider = spider
|
self.spider: Spider = spider
|
||||||
|
|
@ -450,28 +447,13 @@ class Scheduler(BaseScheduler):
|
||||||
"""Create a new priority queue instance, with in-memory storage"""
|
"""Create a new priority queue instance, with in-memory storage"""
|
||||||
assert self.crawler
|
assert self.crawler
|
||||||
assert self.pqclass
|
assert self.pqclass
|
||||||
try:
|
return build_from_crawler(
|
||||||
return build_from_crawler(
|
self.pqclass,
|
||||||
self.pqclass,
|
self.crawler,
|
||||||
self.crawler,
|
downstream_queue_cls=self.mqclass,
|
||||||
downstream_queue_cls=self.mqclass,
|
key="",
|
||||||
key="",
|
start_queue_cls=self._smqclass,
|
||||||
start_queue_cls=self._smqclass,
|
)
|
||||||
)
|
|
||||||
except TypeError: # pragma: no cover
|
|
||||||
warn(
|
|
||||||
f"The __init__ method of {global_object_name(self.pqclass)} "
|
|
||||||
"does not support a `start_queue_cls` keyword-only "
|
|
||||||
"parameter.",
|
|
||||||
ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return build_from_crawler(
|
|
||||||
self.pqclass,
|
|
||||||
self.crawler,
|
|
||||||
downstream_queue_cls=self.mqclass,
|
|
||||||
key="",
|
|
||||||
)
|
|
||||||
|
|
||||||
def _dq(self) -> ScrapyPriorityQueue:
|
def _dq(self) -> ScrapyPriorityQueue:
|
||||||
"""Create a new priority queue instance, with disk storage"""
|
"""Create a new priority queue instance, with disk storage"""
|
||||||
|
|
@ -479,30 +461,14 @@ class Scheduler(BaseScheduler):
|
||||||
assert self.dqdir
|
assert self.dqdir
|
||||||
assert self.pqclass
|
assert self.pqclass
|
||||||
state = self._read_dqs_state(self.dqdir)
|
state = self._read_dqs_state(self.dqdir)
|
||||||
try:
|
q = build_from_crawler(
|
||||||
q = build_from_crawler(
|
self.pqclass,
|
||||||
self.pqclass,
|
self.crawler,
|
||||||
self.crawler,
|
downstream_queue_cls=self.dqclass,
|
||||||
downstream_queue_cls=self.dqclass,
|
key=self.dqdir,
|
||||||
key=self.dqdir,
|
startprios=state,
|
||||||
startprios=state,
|
start_queue_cls=self._sdqclass,
|
||||||
start_queue_cls=self._sdqclass,
|
)
|
||||||
)
|
|
||||||
except TypeError: # pragma: no cover
|
|
||||||
warn(
|
|
||||||
f"The __init__ method of {global_object_name(self.pqclass)} "
|
|
||||||
"does not support a `start_queue_cls` keyword-only "
|
|
||||||
"parameter.",
|
|
||||||
ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
q = build_from_crawler(
|
|
||||||
self.pqclass,
|
|
||||||
self.crawler,
|
|
||||||
downstream_queue_cls=self.dqclass,
|
|
||||||
key=self.dqdir,
|
|
||||||
startprios=state,
|
|
||||||
)
|
|
||||||
if q:
|
if q:
|
||||||
logger.info(
|
logger.info(
|
||||||
"Resuming crawl (%(queuesize)d requests scheduled)",
|
"Resuming crawl (%(queuesize)d requests scheduled)",
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ from scrapy.exceptions import (
|
||||||
from scrapy.http import Request, Response
|
from scrapy.http import Request, Response
|
||||||
from scrapy.pipelines import ItemPipelineManager
|
from scrapy.pipelines import ItemPipelineManager
|
||||||
from scrapy.utils.asyncio import _parallel_asyncio, is_asyncio_available
|
from scrapy.utils.asyncio import _parallel_asyncio, is_asyncio_available
|
||||||
from scrapy.utils.decorators import _warn_spider_arg
|
|
||||||
from scrapy.utils.defer import (
|
from scrapy.utils.defer import (
|
||||||
_defer_sleep_async,
|
_defer_sleep_async,
|
||||||
_schedule_coro,
|
_schedule_coro,
|
||||||
|
|
@ -126,7 +125,7 @@ class Scraper:
|
||||||
|
|
||||||
def _check_deprecated_itemproc_method(self, method: str) -> None:
|
def _check_deprecated_itemproc_method(self, method: str) -> None:
|
||||||
itemproc_cls = type(self.itemproc)
|
itemproc_cls = type(self.itemproc)
|
||||||
if not hasattr(self.itemproc, "process_item_async"):
|
if not hasattr(self.itemproc, f"{method}_async"):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
f"{global_object_name(itemproc_cls)} doesn't define a {method}_async() method,"
|
f"{global_object_name(itemproc_cls)} doesn't define a {method}_async() method,"
|
||||||
f" this is deprecated and the method will be required in future Scrapy versions.",
|
f" this is deprecated and the method will be required in future Scrapy versions.",
|
||||||
|
|
@ -178,9 +177,7 @@ class Scraper:
|
||||||
self.itemproc.open_spider(self.crawler.spider)
|
self.itemproc.open_spider(self.crawler.spider)
|
||||||
)
|
)
|
||||||
|
|
||||||
def close_spider(
|
def close_spider(self) -> Deferred[None]: # pragma: no cover
|
||||||
self, spider: Spider | None = None
|
|
||||||
) -> Deferred[None]: # pragma: no cover
|
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
"Scraper.close_spider() is deprecated, use close_spider_async() instead",
|
"Scraper.close_spider() is deprecated, use close_spider_async() instead",
|
||||||
ScrapyDeprecationWarning,
|
ScrapyDeprecationWarning,
|
||||||
|
|
@ -217,9 +214,8 @@ class Scraper:
|
||||||
self.slot.closing.callback(self.crawler.spider)
|
self.slot.closing.callback(self.crawler.spider)
|
||||||
|
|
||||||
@inlineCallbacks
|
@inlineCallbacks
|
||||||
@_warn_spider_arg
|
|
||||||
def enqueue_scrape(
|
def enqueue_scrape(
|
||||||
self, result: Response | Failure, request: Request, spider: Spider | None = None
|
self, result: Response | Failure, request: Request
|
||||||
) -> Generator[Deferred[Any], Any, None]:
|
) -> Generator[Deferred[Any], Any, None]:
|
||||||
if self.slot is None:
|
if self.slot is None:
|
||||||
raise RuntimeError("Scraper slot not assigned")
|
raise RuntimeError("Scraper slot not assigned")
|
||||||
|
|
@ -349,13 +345,11 @@ class Scraper:
|
||||||
)
|
)
|
||||||
return await ensure_awaitable(iterate_spider_output(output))
|
return await ensure_awaitable(iterate_spider_output(output))
|
||||||
|
|
||||||
@_warn_spider_arg
|
|
||||||
def handle_spider_error(
|
def handle_spider_error(
|
||||||
self,
|
self,
|
||||||
_failure: Failure,
|
_failure: Failure,
|
||||||
request: Request,
|
request: Request,
|
||||||
response: Response | Failure,
|
response: Response | Failure,
|
||||||
spider: Spider | None = None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Handle an exception raised by a spider callback or errback."""
|
"""Handle an exception raised by a spider callback or errback."""
|
||||||
assert self.crawler.spider
|
assert self.crawler.spider
|
||||||
|
|
@ -391,7 +385,6 @@ class Scraper:
|
||||||
result: Iterable[_T] | AsyncIterator[_T],
|
result: Iterable[_T] | AsyncIterator[_T],
|
||||||
request: Request,
|
request: Request,
|
||||||
response: Response | Failure,
|
response: Response | Failure,
|
||||||
spider: Spider | None = None,
|
|
||||||
) -> Deferred[None]: # pragma: no cover
|
) -> Deferred[None]: # pragma: no cover
|
||||||
"""Pass items/requests produced by a callback to ``_process_spidermw_output()`` in parallel."""
|
"""Pass items/requests produced by a callback to ``_process_spidermw_output()`` in parallel."""
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
|
|
@ -448,7 +441,7 @@ class Scraper:
|
||||||
self, output: Any, response: Response | Failure
|
self, output: Any, response: Response | Failure
|
||||||
) -> Deferred[None]:
|
) -> Deferred[None]:
|
||||||
"""Process each Request/Item (given in the output parameter) returned
|
"""Process each Request/Item (given in the output parameter) returned
|
||||||
from the given spider.
|
from the spider.
|
||||||
|
|
||||||
Items are sent to the item pipelines, requests are scheduled.
|
Items are sent to the item pipelines, requests are scheduled.
|
||||||
"""
|
"""
|
||||||
|
|
@ -458,7 +451,7 @@ class Scraper:
|
||||||
self, output: Any, response: Response | Failure
|
self, output: Any, response: Response | Failure
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Process each Request/Item (given in the output parameter) returned
|
"""Process each Request/Item (given in the output parameter) returned
|
||||||
from the given spider.
|
from the spider.
|
||||||
|
|
||||||
Items are sent to the item pipelines, requests are scheduled.
|
Items are sent to the item pipelines, requests are scheduled.
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -9,31 +9,29 @@ from __future__ import annotations
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import AsyncIterator, Callable, Coroutine, Iterable
|
from collections.abc import AsyncIterator, Callable, Coroutine, Iterable
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from inspect import isasyncgenfunction, iscoroutine
|
from inspect import isasyncgenfunction
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar, cast
|
from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
|
||||||
from twisted.internet.defer import Deferred, inlineCallbacks
|
|
||||||
from twisted.python.failure import Failure
|
from twisted.python.failure import Failure
|
||||||
|
|
||||||
from scrapy import Request, Spider
|
from scrapy import Request, Spider
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning, _InvalidOutput
|
from scrapy.exceptions import ScrapyDeprecationWarning, _InvalidOutput
|
||||||
from scrapy.http import Response
|
from scrapy.http import Response
|
||||||
from scrapy.middleware import MiddlewareManager
|
from scrapy.middleware import MiddlewareManager
|
||||||
from scrapy.utils.asyncgen import as_async_generator, collect_asyncgen
|
from scrapy.utils.asyncgen import as_async_generator
|
||||||
from scrapy.utils.conf import build_component_list
|
from scrapy.utils.conf import build_component_list
|
||||||
from scrapy.utils.defer import (
|
from scrapy.utils.defer import (
|
||||||
_defer_sleep_async,
|
_defer_sleep_async,
|
||||||
deferred_from_coro,
|
deferred_from_coro,
|
||||||
maybe_deferred_to_future,
|
maybe_deferred_to_future,
|
||||||
)
|
)
|
||||||
from scrapy.utils.python import MutableAsyncChain, MutableChain, global_object_name
|
from scrapy.utils.python import MutableAsyncChain, global_object_name
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Generator
|
from twisted.internet.defer import Deferred
|
||||||
|
|
||||||
from scrapy.crawler import Crawler
|
|
||||||
from scrapy.settings import BaseSettings
|
from scrapy.settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -47,96 +45,27 @@ ScrapeFunc: TypeAlias = Callable[
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def _isiterable(o: Any) -> bool:
|
|
||||||
return isinstance(o, (Iterable, AsyncIterator))
|
|
||||||
|
|
||||||
|
|
||||||
class SpiderMiddlewareManager(MiddlewareManager):
|
class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
component_name = "spider middleware"
|
component_name = "spider middleware"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_mwlist_from_settings(cls, settings: BaseSettings) -> list[Any]:
|
def _get_mwlist_from_settings(cls, settings: BaseSettings) -> list[Any]:
|
||||||
return build_component_list(settings.getwithbase("SPIDER_MIDDLEWARES"))
|
return build_component_list(
|
||||||
|
settings.get_component_priority_dict_with_base("SPIDER_MIDDLEWARES")
|
||||||
def __init__(self, *middlewares: Any, crawler: Crawler | None = None) -> None:
|
)
|
||||||
self._check_deprecated_process_start_requests_use(middlewares)
|
|
||||||
super().__init__(*middlewares, crawler=crawler)
|
|
||||||
|
|
||||||
def _check_deprecated_process_start_requests_use(
|
|
||||||
self, middlewares: tuple[Any, ...]
|
|
||||||
) -> None:
|
|
||||||
deprecated_middlewares = [
|
|
||||||
middleware
|
|
||||||
for middleware in middlewares
|
|
||||||
if hasattr(middleware, "process_start_requests")
|
|
||||||
and not hasattr(middleware, "process_start")
|
|
||||||
]
|
|
||||||
modern_middlewares = [
|
|
||||||
middleware
|
|
||||||
for middleware in middlewares
|
|
||||||
if not hasattr(middleware, "process_start_requests")
|
|
||||||
and hasattr(middleware, "process_start")
|
|
||||||
]
|
|
||||||
if deprecated_middlewares and modern_middlewares:
|
|
||||||
raise ValueError(
|
|
||||||
"You are trying to combine spider middlewares that only "
|
|
||||||
"define the deprecated process_start_requests() method () "
|
|
||||||
"with spider middlewares that only define the "
|
|
||||||
"process_start() method (). This is not possible. You must "
|
|
||||||
"either disable or make universal 1 of those 2 sets of "
|
|
||||||
"spider middlewares. Making a spider middleware universal "
|
|
||||||
"means having it define both methods. See the release notes "
|
|
||||||
"of Scrapy 2.13 for details: "
|
|
||||||
"https://docs.scrapy.org/en/2.13/news.html"
|
|
||||||
)
|
|
||||||
|
|
||||||
self._use_start_requests = bool(deprecated_middlewares)
|
|
||||||
if self._use_start_requests:
|
|
||||||
deprecated_middleware_list = ", ".join(
|
|
||||||
global_object_name(middleware.__class__)
|
|
||||||
for middleware in deprecated_middlewares
|
|
||||||
)
|
|
||||||
warn(
|
|
||||||
f"The following enabled spider middlewares, directly or "
|
|
||||||
f"through their parent classes, define the deprecated "
|
|
||||||
f"process_start_requests() method: "
|
|
||||||
f"{deprecated_middleware_list}. process_start_requests() has "
|
|
||||||
f"been deprecated in favor of a new method, process_start(), "
|
|
||||||
f"to support asynchronous code execution. "
|
|
||||||
f"process_start_requests() will stop being called in a future "
|
|
||||||
f"version of Scrapy. If you use Scrapy 2.13 or higher "
|
|
||||||
f"only, replace process_start_requests() with "
|
|
||||||
f"process_start(); note that process_start() is a coroutine "
|
|
||||||
f"(async def). If you need to maintain compatibility with "
|
|
||||||
f"lower Scrapy versions, when defining "
|
|
||||||
f"process_start_requests() in a spider middleware class, "
|
|
||||||
f"define process_start() as well. See the release notes of "
|
|
||||||
f"Scrapy 2.13 for details: "
|
|
||||||
f"https://docs.scrapy.org/en/2.13/news.html",
|
|
||||||
ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _add_middleware(self, mw: Any) -> None:
|
def _add_middleware(self, mw: Any) -> None:
|
||||||
if hasattr(mw, "process_spider_input"):
|
if hasattr(mw, "process_spider_input"):
|
||||||
self.methods["process_spider_input"].append(mw.process_spider_input)
|
self.methods["process_spider_input"].append(mw.process_spider_input)
|
||||||
self._check_mw_method_spider_arg(mw.process_spider_input)
|
self._check_mw_method_spider_arg(mw.process_spider_input)
|
||||||
|
|
||||||
if self._use_start_requests:
|
if hasattr(mw, "process_start"):
|
||||||
if hasattr(mw, "process_start_requests"):
|
|
||||||
self.methods["process_start_requests"].appendleft(
|
|
||||||
mw.process_start_requests
|
|
||||||
)
|
|
||||||
elif hasattr(mw, "process_start"):
|
|
||||||
self.methods["process_start"].appendleft(mw.process_start)
|
self.methods["process_start"].appendleft(mw.process_start)
|
||||||
|
|
||||||
process_spider_output = self._get_async_method_pair(mw, "process_spider_output")
|
process_spider_output = self._get_process_spider_output(mw)
|
||||||
self.methods["process_spider_output"].appendleft(process_spider_output)
|
self.methods["process_spider_output"].appendleft(process_spider_output)
|
||||||
if callable(process_spider_output):
|
if process_spider_output is not None:
|
||||||
self._check_mw_method_spider_arg(process_spider_output)
|
self._check_mw_method_spider_arg(process_spider_output)
|
||||||
elif isinstance(process_spider_output, tuple):
|
|
||||||
for m in process_spider_output:
|
|
||||||
self._check_mw_method_spider_arg(m)
|
|
||||||
|
|
||||||
process_spider_exception = getattr(mw, "process_spider_exception", None)
|
process_spider_exception = getattr(mw, "process_spider_exception", None)
|
||||||
self.methods["process_spider_exception"].appendleft(process_spider_exception)
|
self.methods["process_spider_exception"].appendleft(process_spider_exception)
|
||||||
|
|
@ -150,7 +79,7 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
request: Request,
|
request: Request,
|
||||||
) -> Iterable[_T] | AsyncIterator[_T]:
|
) -> Iterable[_T] | AsyncIterator[_T]:
|
||||||
for method in self.methods["process_spider_input"]:
|
for method in self.methods["process_spider_input"]:
|
||||||
method = cast("Callable", method)
|
assert method is not None
|
||||||
try:
|
try:
|
||||||
if method in self._mw_methods_requiring_spider:
|
if method in self._mw_methods_requiring_spider:
|
||||||
result = method(response=response, spider=self._spider)
|
result = method(response=response, spider=self._spider)
|
||||||
|
|
@ -168,54 +97,28 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
return await scrape_func(Failure(), request)
|
return await scrape_func(Failure(), request)
|
||||||
return await scrape_func(response, request)
|
return await scrape_func(response, request)
|
||||||
|
|
||||||
def _evaluate_iterable(
|
async def _evaluate_iterable(
|
||||||
self,
|
self,
|
||||||
response: Response,
|
response: Response,
|
||||||
iterable: Iterable[_T] | AsyncIterator[_T],
|
iterable: AsyncIterator[_T],
|
||||||
exception_processor_index: int,
|
exception_processor_index: int,
|
||||||
recover_to: MutableChain[_T] | MutableAsyncChain[_T],
|
recover_to: MutableAsyncChain[_T],
|
||||||
) -> Iterable[_T] | AsyncIterator[_T]:
|
) -> AsyncIterator[_T]:
|
||||||
def process_sync(iterable: Iterable[_T]) -> Iterable[_T]:
|
try:
|
||||||
try:
|
async for r in iterable:
|
||||||
yield from iterable
|
yield r
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
exception_result = cast(
|
exception_result: MutableAsyncChain[_T] = self._process_spider_exception(
|
||||||
"Failure | MutableChain[_T]",
|
response, ex, exception_processor_index
|
||||||
self._process_spider_exception(
|
)
|
||||||
response, ex, exception_processor_index
|
recover_to.extend(exception_result)
|
||||||
),
|
|
||||||
)
|
|
||||||
if isinstance(exception_result, Failure):
|
|
||||||
raise
|
|
||||||
assert isinstance(recover_to, MutableChain)
|
|
||||||
recover_to.extend(exception_result)
|
|
||||||
|
|
||||||
async def process_async(iterable: AsyncIterator[_T]) -> AsyncIterator[_T]:
|
|
||||||
try:
|
|
||||||
async for r in iterable:
|
|
||||||
yield r
|
|
||||||
except Exception as ex:
|
|
||||||
exception_result = cast(
|
|
||||||
"Failure | MutableAsyncChain[_T]",
|
|
||||||
self._process_spider_exception(
|
|
||||||
response, ex, exception_processor_index
|
|
||||||
),
|
|
||||||
)
|
|
||||||
if isinstance(exception_result, Failure):
|
|
||||||
raise
|
|
||||||
assert isinstance(recover_to, MutableAsyncChain)
|
|
||||||
recover_to.extend(exception_result)
|
|
||||||
|
|
||||||
if isinstance(iterable, AsyncIterator):
|
|
||||||
return process_async(iterable)
|
|
||||||
return process_sync(iterable)
|
|
||||||
|
|
||||||
def _process_spider_exception(
|
def _process_spider_exception(
|
||||||
self,
|
self,
|
||||||
response: Response,
|
response: Response,
|
||||||
exception: Exception,
|
exception: Exception,
|
||||||
start_index: int = 0,
|
start_index: int = 0,
|
||||||
) -> MutableChain[_T] | MutableAsyncChain[_T]:
|
) -> MutableAsyncChain[_T]:
|
||||||
# don't handle _InvalidOutput exception
|
# don't handle _InvalidOutput exception
|
||||||
if isinstance(exception, _InvalidOutput):
|
if isinstance(exception, _InvalidOutput):
|
||||||
raise exception
|
raise exception
|
||||||
|
|
@ -225,28 +128,18 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
for method_index, method in enumerate(method_list, start=start_index):
|
for method_index, method in enumerate(method_list, start=start_index):
|
||||||
if method is None:
|
if method is None:
|
||||||
continue
|
continue
|
||||||
method = cast("Callable", method)
|
|
||||||
if method in self._mw_methods_requiring_spider:
|
if method in self._mw_methods_requiring_spider:
|
||||||
result = method(
|
result = method(
|
||||||
response=response, exception=exception, spider=self._spider
|
response=response, exception=exception, spider=self._spider
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
result = method(response=response, exception=exception)
|
result = method(response=response, exception=exception)
|
||||||
if _isiterable(result):
|
if isinstance(result, (Iterable, AsyncIterator)):
|
||||||
# stop exception handling by handing control over to the
|
# stop exception handling by handing control over to the
|
||||||
# process_spider_output chain if an iterable has been returned
|
# process_spider_output chain if an iterable has been returned
|
||||||
dfd: Deferred[MutableChain[_T] | MutableAsyncChain[_T]] = (
|
if isinstance(result, Iterable):
|
||||||
self._process_spider_output(response, result, method_index + 1)
|
result = as_async_generator(result)
|
||||||
)
|
return self._process_spider_output(response, result, method_index + 1)
|
||||||
# _process_spider_output() returns a Deferred only because of downgrading so this can be
|
|
||||||
# simplified when downgrading is removed.
|
|
||||||
if dfd.called:
|
|
||||||
# the result is available immediately if _process_spider_output didn't do downgrading
|
|
||||||
return cast("MutableChain[_T] | MutableAsyncChain[_T]", dfd.result)
|
|
||||||
# we forbid waiting here because otherwise we would need to return a deferred from
|
|
||||||
# _process_spider_exception too, which complicates the architecture
|
|
||||||
msg = f"Async iterable returned from {global_object_name(method)} cannot be downgraded"
|
|
||||||
raise _InvalidOutput(msg)
|
|
||||||
if result is None:
|
if result is None:
|
||||||
continue
|
continue
|
||||||
msg = (
|
msg = (
|
||||||
|
|
@ -256,124 +149,35 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
raise _InvalidOutput(msg)
|
raise _InvalidOutput(msg)
|
||||||
raise exception
|
raise exception
|
||||||
|
|
||||||
# This method cannot be made async def, as _process_spider_exception relies on the Deferred result
|
def _process_spider_output(
|
||||||
# being available immediately which doesn't work when it's a wrapped coroutine.
|
|
||||||
# It also needs @inlineCallbacks only because of downgrading so it can be removed when downgrading is removed.
|
|
||||||
@inlineCallbacks
|
|
||||||
def _process_spider_output( # noqa: PLR0912
|
|
||||||
self,
|
self,
|
||||||
response: Response,
|
response: Response,
|
||||||
result: Iterable[_T] | AsyncIterator[_T],
|
result: AsyncIterator[_T],
|
||||||
start_index: int = 0,
|
start_index: int = 0,
|
||||||
) -> Generator[Deferred[Any], Any, MutableChain[_T] | MutableAsyncChain[_T]]:
|
) -> MutableAsyncChain[_T]:
|
||||||
# items in this iterable do not need to go through the process_spider_output
|
# items in this iterable do not need to go through the process_spider_output
|
||||||
# chain, they went through it already from the process_spider_exception method
|
# chain, they went through it already from the process_spider_exception method
|
||||||
recovered: MutableChain[_T] | MutableAsyncChain[_T]
|
recovered: MutableAsyncChain[_T] = MutableAsyncChain()
|
||||||
last_result_is_async = isinstance(result, AsyncIterator)
|
|
||||||
recovered = MutableAsyncChain() if last_result_is_async else MutableChain()
|
|
||||||
|
|
||||||
# There are three cases for the middleware: def foo, async def foo, def foo + async def foo_async.
|
|
||||||
# 1. def foo. Sync iterables are passed as is, async ones are downgraded.
|
|
||||||
# 2. async def foo. Sync iterables are upgraded, async ones are passed as is.
|
|
||||||
# 3. def foo + async def foo_async. Iterables are passed to the respective method.
|
|
||||||
# Storing methods and method tuples in the same list is weird but we should be able to roll this back
|
|
||||||
# when we drop this compatibility feature.
|
|
||||||
|
|
||||||
method_list = islice(self.methods["process_spider_output"], start_index, None)
|
method_list = islice(self.methods["process_spider_output"], start_index, None)
|
||||||
for method_index, method_pair in enumerate(method_list, start=start_index):
|
for method_index, method in enumerate(method_list, start=start_index):
|
||||||
if method_pair is None:
|
if method is None:
|
||||||
continue
|
continue
|
||||||
need_upgrade = need_downgrade = False
|
if method in self._mw_methods_requiring_spider:
|
||||||
if isinstance(method_pair, tuple):
|
result = method(response=response, result=result, spider=self._spider)
|
||||||
# This tuple handling is only needed until _async compatibility methods are removed.
|
|
||||||
method_sync, method_async = method_pair
|
|
||||||
method = method_async if last_result_is_async else method_sync
|
|
||||||
else:
|
else:
|
||||||
method = method_pair
|
result = method(response=response, result=result)
|
||||||
if not last_result_is_async and isasyncgenfunction(method):
|
result = self._evaluate_iterable(
|
||||||
need_upgrade = True
|
response, result, method_index + 1, recovered
|
||||||
elif last_result_is_async and not isasyncgenfunction(method):
|
)
|
||||||
need_downgrade = True
|
return MutableAsyncChain(result, recovered)
|
||||||
try:
|
|
||||||
if need_upgrade:
|
|
||||||
# Iterable -> AsyncIterator
|
|
||||||
result = as_async_generator(result)
|
|
||||||
elif need_downgrade:
|
|
||||||
logger.warning(
|
|
||||||
f"Async iterable passed to {global_object_name(method)} was"
|
|
||||||
f" downgraded to a non-async one. This is deprecated and will"
|
|
||||||
f" stop working in a future version of Scrapy. Please see"
|
|
||||||
f" https://docs.scrapy.org/en/latest/topics/coroutines.html#for-middleware-users"
|
|
||||||
f" for more information."
|
|
||||||
)
|
|
||||||
assert isinstance(result, AsyncIterator)
|
|
||||||
# AsyncIterator -> Iterable
|
|
||||||
result = yield deferred_from_coro(collect_asyncgen(result))
|
|
||||||
if isinstance(recovered, AsyncIterator):
|
|
||||||
recovered_collected = yield deferred_from_coro(
|
|
||||||
collect_asyncgen(recovered)
|
|
||||||
)
|
|
||||||
recovered = MutableChain(recovered_collected)
|
|
||||||
# might fail directly if the output value is not a generator
|
|
||||||
if method in self._mw_methods_requiring_spider:
|
|
||||||
result = method(
|
|
||||||
response=response, result=result, spider=self._spider
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
result = method(response=response, result=result)
|
|
||||||
except Exception as ex:
|
|
||||||
exception_result: Failure | MutableChain[_T] | MutableAsyncChain[_T] = (
|
|
||||||
self._process_spider_exception(response, ex, method_index + 1)
|
|
||||||
)
|
|
||||||
if isinstance(exception_result, Failure):
|
|
||||||
raise
|
|
||||||
return exception_result
|
|
||||||
if _isiterable(result):
|
|
||||||
result = self._evaluate_iterable(
|
|
||||||
response, result, method_index + 1, recovered
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
if iscoroutine(result):
|
|
||||||
result.close() # Silence warning about not awaiting
|
|
||||||
msg = (
|
|
||||||
f"{global_object_name(method)} must be an asynchronous "
|
|
||||||
f"generator (i.e. use yield)"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
msg = (
|
|
||||||
f"{global_object_name(method)} must return an iterable, got "
|
|
||||||
f"{type(result)}"
|
|
||||||
)
|
|
||||||
raise _InvalidOutput(msg)
|
|
||||||
last_result_is_async = isinstance(result, AsyncIterator)
|
|
||||||
|
|
||||||
if last_result_is_async:
|
|
||||||
return MutableAsyncChain(result, recovered)
|
|
||||||
return MutableChain(result, recovered) # type: ignore[arg-type]
|
|
||||||
|
|
||||||
async def _process_callback_output(
|
async def _process_callback_output(
|
||||||
self,
|
self, response: Response, result: AsyncIterator[_T]
|
||||||
response: Response,
|
) -> MutableAsyncChain[_T]:
|
||||||
result: Iterable[_T] | AsyncIterator[_T],
|
recovered: MutableAsyncChain[_T] = MutableAsyncChain()
|
||||||
) -> MutableChain[_T] | MutableAsyncChain[_T]:
|
|
||||||
recovered: MutableChain[_T] | MutableAsyncChain[_T]
|
|
||||||
if isinstance(result, AsyncIterator):
|
|
||||||
recovered = MutableAsyncChain()
|
|
||||||
else:
|
|
||||||
recovered = MutableChain()
|
|
||||||
result = self._evaluate_iterable(response, result, 0, recovered)
|
result = self._evaluate_iterable(response, result, 0, recovered)
|
||||||
result = await maybe_deferred_to_future(
|
result = self._process_spider_output(response, result)
|
||||||
cast(
|
return MutableAsyncChain(result, recovered)
|
||||||
"Deferred[Iterable[_T] | AsyncIterator[_T]]",
|
|
||||||
self._process_spider_output(response, result),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if isinstance(result, AsyncIterator):
|
|
||||||
return MutableAsyncChain(result, recovered)
|
|
||||||
if isinstance(recovered, AsyncIterator):
|
|
||||||
recovered_collected = await collect_asyncgen(recovered)
|
|
||||||
recovered = MutableChain(recovered_collected)
|
|
||||||
return MutableChain(result, recovered)
|
|
||||||
|
|
||||||
def scrape_response(
|
def scrape_response(
|
||||||
self,
|
self,
|
||||||
|
|
@ -384,7 +188,7 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
response: Response,
|
response: Response,
|
||||||
request: Request,
|
request: Request,
|
||||||
spider: Spider,
|
spider: Spider,
|
||||||
) -> Deferred[MutableChain[_T] | MutableAsyncChain[_T]]: # pragma: no cover
|
) -> Deferred[MutableAsyncChain[_T]]: # pragma: no cover
|
||||||
warn(
|
warn(
|
||||||
"SpiderMiddlewareManager.scrape_response() is deprecated, use scrape_response_async() instead",
|
"SpiderMiddlewareManager.scrape_response() is deprecated, use scrape_response_async() instead",
|
||||||
ScrapyDeprecationWarning,
|
ScrapyDeprecationWarning,
|
||||||
|
|
@ -407,31 +211,21 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
scrape_func: ScrapeFunc[_T],
|
scrape_func: ScrapeFunc[_T],
|
||||||
response: Response,
|
response: Response,
|
||||||
request: Request,
|
request: Request,
|
||||||
) -> MutableChain[_T] | MutableAsyncChain[_T]:
|
) -> MutableAsyncChain[_T]:
|
||||||
if not self.crawler:
|
if not self.crawler:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"scrape_response_async() called on a SpiderMiddlewareManager"
|
"scrape_response_async() called on a SpiderMiddlewareManager"
|
||||||
" instance created without a crawler."
|
" instance created without a crawler."
|
||||||
)
|
)
|
||||||
|
|
||||||
async def process_callback_output(
|
|
||||||
result: Iterable[_T] | AsyncIterator[_T],
|
|
||||||
) -> MutableChain[_T] | MutableAsyncChain[_T]:
|
|
||||||
return await self._process_callback_output(response, result)
|
|
||||||
|
|
||||||
def process_spider_exception(
|
|
||||||
exception: Exception,
|
|
||||||
) -> MutableChain[_T] | MutableAsyncChain[_T]:
|
|
||||||
return self._process_spider_exception(response, exception)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
it: Iterable[_T] | AsyncIterator[_T] = await self._process_spider_input(
|
it: Iterable[_T] | AsyncIterator[_T] = await self._process_spider_input(
|
||||||
scrape_func, response, request
|
scrape_func, response, request
|
||||||
)
|
)
|
||||||
return await process_callback_output(it)
|
ait = it if isinstance(it, AsyncIterator) else as_async_generator(it)
|
||||||
|
return await self._process_callback_output(response, ait)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
await _defer_sleep_async()
|
await _defer_sleep_async()
|
||||||
return process_spider_exception(ex)
|
return self._process_spider_exception(response, ex)
|
||||||
|
|
||||||
async def process_start(
|
async def process_start(
|
||||||
self, spider: Spider | None = None
|
self, spider: Spider | None = None
|
||||||
|
|
@ -449,112 +243,32 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
)
|
)
|
||||||
warn(msg, category=ScrapyDeprecationWarning, stacklevel=2)
|
warn(msg, category=ScrapyDeprecationWarning, stacklevel=2)
|
||||||
self._set_compat_spider(spider)
|
self._set_compat_spider(spider)
|
||||||
self._check_deprecated_start_requests_use()
|
start = self._spider.start()
|
||||||
if self._use_start_requests:
|
return await self._process_chain("process_start", start)
|
||||||
sync_start = iter(self._spider.start_requests())
|
|
||||||
sync_start = await self._process_chain(
|
|
||||||
"process_start_requests", sync_start, always_add_spider=True
|
|
||||||
)
|
|
||||||
start: AsyncIterator[Any] = as_async_generator(sync_start)
|
|
||||||
else:
|
|
||||||
start = self._spider.start()
|
|
||||||
start = await self._process_chain("process_start", start)
|
|
||||||
return start
|
|
||||||
|
|
||||||
def _check_deprecated_start_requests_use(self) -> None:
|
|
||||||
start_requests_cls = None
|
|
||||||
start_cls = None
|
|
||||||
spidercls = self._spider.__class__
|
|
||||||
mro = spidercls.__mro__
|
|
||||||
|
|
||||||
for cls in mro:
|
|
||||||
cls_dict = cls.__dict__
|
|
||||||
if start_requests_cls is None and "start_requests" in cls_dict:
|
|
||||||
start_requests_cls = cls
|
|
||||||
if start_cls is None and "start" in cls_dict:
|
|
||||||
start_cls = cls
|
|
||||||
if start_requests_cls is not None and start_cls is not None:
|
|
||||||
break
|
|
||||||
|
|
||||||
# Spider defines both, start_requests and start.
|
|
||||||
assert start_requests_cls is not None
|
|
||||||
assert start_cls is not None
|
|
||||||
|
|
||||||
if (
|
|
||||||
start_requests_cls is not Spider
|
|
||||||
and start_cls is not start_requests_cls
|
|
||||||
and mro.index(start_requests_cls) < mro.index(start_cls)
|
|
||||||
):
|
|
||||||
src = global_object_name(start_requests_cls)
|
|
||||||
if start_requests_cls is not spidercls:
|
|
||||||
src += f" (inherited by {global_object_name(spidercls)})"
|
|
||||||
warn(
|
|
||||||
f"{src} defines the deprecated start_requests() method. "
|
|
||||||
f"start_requests() has been deprecated in favor of a new "
|
|
||||||
f"method, start(), to support asynchronous code "
|
|
||||||
f"execution. start_requests() will stop being called in a "
|
|
||||||
f"future version of Scrapy. If you use Scrapy 2.13 or "
|
|
||||||
f"higher only, replace start_requests() with start(); "
|
|
||||||
f"note that start() is a coroutine (async def). If you "
|
|
||||||
f"need to maintain compatibility with lower Scrapy versions, "
|
|
||||||
f"when overriding start_requests() in a spider class, "
|
|
||||||
f"override start() as well; you can use super() to "
|
|
||||||
f"reuse the inherited start() implementation without "
|
|
||||||
f"copy-pasting. See the release notes of Scrapy 2.13 for "
|
|
||||||
f"details: https://docs.scrapy.org/en/2.13/news.html",
|
|
||||||
ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
|
||||||
self._use_start_requests
|
|
||||||
and start_cls is not Spider
|
|
||||||
and start_requests_cls is not start_cls
|
|
||||||
and mro.index(start_cls) < mro.index(start_requests_cls)
|
|
||||||
):
|
|
||||||
src = global_object_name(start_cls)
|
|
||||||
if start_cls is not spidercls:
|
|
||||||
src += f" (inherited by {global_object_name(spidercls)})"
|
|
||||||
raise ValueError(
|
|
||||||
f"{src} does not define the deprecated start_requests() "
|
|
||||||
f"method. However, one or more of your enabled spider "
|
|
||||||
f"middlewares (reported in an earlier deprecation warning) "
|
|
||||||
f"define the process_start_requests() method, and not the "
|
|
||||||
f"process_start() method, making them only compatible with "
|
|
||||||
f"(deprecated) spiders that define the start_requests() "
|
|
||||||
f"method. To solve this issue, disable the offending spider "
|
|
||||||
f"middlewares, upgrade them as described in that earlier "
|
|
||||||
f"deprecation warning, or make your spider compatible with "
|
|
||||||
f"deprecated spider middlewares (and earlier Scrapy versions) "
|
|
||||||
f"by defining a sync start_requests() method that works "
|
|
||||||
f"similarly to its existing start() method. See the "
|
|
||||||
f"release notes of Scrapy 2.13 for details: "
|
|
||||||
f"https://docs.scrapy.org/en/2.13/news.html"
|
|
||||||
)
|
|
||||||
|
|
||||||
# This method is only needed until _async compatibility methods are removed.
|
# This method is only needed until _async compatibility methods are removed.
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_async_method_pair(
|
def _get_process_spider_output(mw: Any) -> Callable[..., Any] | None:
|
||||||
mw: Any, methodname: str
|
normal_method: Callable[..., Any] | None = getattr(
|
||||||
) -> Callable | tuple[Callable, Callable] | None:
|
mw, "process_spider_output", None
|
||||||
normal_method: Callable | None = getattr(mw, methodname, None)
|
)
|
||||||
methodname_async = methodname + "_async"
|
async_method: Callable[..., Any] | None = getattr(
|
||||||
async_method: Callable | None = getattr(mw, methodname_async, None)
|
mw, "process_spider_output_async", None
|
||||||
|
)
|
||||||
if not async_method:
|
if not async_method:
|
||||||
if normal_method and not isasyncgenfunction(normal_method):
|
if normal_method and not isasyncgenfunction(normal_method):
|
||||||
logger.warning(
|
raise TypeError(
|
||||||
f"Middleware {global_object_name(mw.__class__)} doesn't support"
|
f"Middleware {global_object_name(mw.__class__)} doesn't support"
|
||||||
f" asynchronous spider output, this is deprecated and will stop"
|
f" asynchronous spider output. Its process_spider_output() method"
|
||||||
f" working in a future version of Scrapy. The middleware should"
|
f" should be an async generator function or it should additionally"
|
||||||
f" be updated to support it. Please see"
|
f" define a process_spider_output_async() method."
|
||||||
f" https://docs.scrapy.org/en/latest/topics/coroutines.html#for-middleware-users"
|
|
||||||
f" for more information."
|
|
||||||
)
|
)
|
||||||
return normal_method
|
return normal_method
|
||||||
if not normal_method:
|
if not normal_method:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Middleware {global_object_name(mw.__class__)} has {methodname_async} "
|
f"Middleware {global_object_name(mw.__class__)} has"
|
||||||
f"without {methodname}, skipping this method."
|
f" process_spider_output_async() without process_spider_output(),"
|
||||||
|
f" skipping this method. Please rename it to process_spider_output()."
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
if not isasyncgenfunction(async_method):
|
if not isasyncgenfunction(async_method):
|
||||||
|
|
@ -566,8 +280,8 @@ class SpiderMiddlewareManager(MiddlewareManager):
|
||||||
if isasyncgenfunction(normal_method):
|
if isasyncgenfunction(normal_method):
|
||||||
logger.error(
|
logger.error(
|
||||||
f"{global_object_name(normal_method)} is an async "
|
f"{global_object_name(normal_method)} is an async "
|
||||||
f"generator function while {methodname_async} exists, "
|
f"generator function while process_spider_output_async() exists, "
|
||||||
f"skipping both methods."
|
f"skipping both methods. Please remove process_spider_output_async()."
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
return normal_method, async_method
|
return async_method
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import pprint
|
||||||
import signal
|
import signal
|
||||||
import warnings
|
import warnings
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from functools import partial
|
||||||
from typing import TYPE_CHECKING, Any, TypeVar
|
from typing import TYPE_CHECKING, Any, TypeVar
|
||||||
|
|
||||||
from twisted.internet.defer import Deferred, DeferredList, inlineCallbacks
|
from twisted.internet.defer import Deferred, DeferredList, inlineCallbacks
|
||||||
|
|
@ -362,9 +363,12 @@ class CrawlerRunnerBase(ABC):
|
||||||
"""
|
"""
|
||||||
Return a :class:`~scrapy.crawler.Crawler` object.
|
Return a :class:`~scrapy.crawler.Crawler` object.
|
||||||
|
|
||||||
* If ``crawler_or_spidercls`` is a Crawler, it is returned as-is.
|
* If ``crawler_or_spidercls`` is a Crawler, the runner's settings are
|
||||||
|
merged into it as defaults: for each setting, the runner's value
|
||||||
|
is applied only if the Crawler does not already have that setting at
|
||||||
|
an equal or higher priority. The Crawler is then returned.
|
||||||
* If ``crawler_or_spidercls`` is a Spider subclass, a new Crawler
|
* If ``crawler_or_spidercls`` is a Spider subclass, a new Crawler
|
||||||
is constructed for it.
|
is constructed for it using this runner's settings.
|
||||||
* If ``crawler_or_spidercls`` is a string, this function finds
|
* If ``crawler_or_spidercls`` is a string, this function finds
|
||||||
a spider with this name in a Scrapy project (using spider loader),
|
a spider with this name in a Scrapy project (using spider loader),
|
||||||
then creates a Crawler instance for it.
|
then creates a Crawler instance for it.
|
||||||
|
|
@ -375,6 +379,7 @@ class CrawlerRunnerBase(ABC):
|
||||||
"it must be a spider class (or a Crawler object)"
|
"it must be a spider class (or a Crawler object)"
|
||||||
)
|
)
|
||||||
if isinstance(crawler_or_spidercls, Crawler):
|
if isinstance(crawler_or_spidercls, Crawler):
|
||||||
|
crawler_or_spidercls.settings.update(self.settings)
|
||||||
return crawler_or_spidercls
|
return crawler_or_spidercls
|
||||||
return self._create_crawler(crawler_or_spidercls)
|
return self._create_crawler(crawler_or_spidercls)
|
||||||
|
|
||||||
|
|
@ -526,8 +531,8 @@ class AsyncCrawlerRunner(CrawlerRunnerBase):
|
||||||
"""
|
"""
|
||||||
Run a crawler with the provided arguments.
|
Run a crawler with the provided arguments.
|
||||||
|
|
||||||
It will call the given Crawler's :meth:`~Crawler.crawl` method, while
|
It will call the given Crawler's :meth:`~Crawler.crawl_async` method,
|
||||||
keeping track of it so it can be stopped later.
|
while keeping track of it so it can be stopped later.
|
||||||
|
|
||||||
If ``crawler_or_spidercls`` isn't a :class:`~scrapy.crawler.Crawler`
|
If ``crawler_or_spidercls`` isn't a :class:`~scrapy.crawler.Crawler`
|
||||||
instance, this method will try to create one using this parameter as
|
instance, this method will try to create one using this parameter as
|
||||||
|
|
@ -568,28 +573,30 @@ class AsyncCrawlerRunner(CrawlerRunnerBase):
|
||||||
crawler = self.create_crawler(crawler_or_spidercls)
|
crawler = self.create_crawler(crawler_or_spidercls)
|
||||||
return self._crawl(crawler, *args, **kwargs)
|
return self._crawl(crawler, *args, **kwargs)
|
||||||
|
|
||||||
|
async def _crawl_and_track(
|
||||||
|
self, crawler: Crawler, *args: Any, **kwargs: Any
|
||||||
|
) -> None:
|
||||||
|
try:
|
||||||
|
await crawler.crawl_async(*args, **kwargs)
|
||||||
|
except Exception:
|
||||||
|
self.bootstrap_failed = True
|
||||||
|
raise # re-raise so asyncio still logs it to stderr naturally
|
||||||
|
|
||||||
|
def _done(self, task: asyncio.Task[None], crawler: Crawler) -> None:
|
||||||
|
self._active.discard(task)
|
||||||
|
self.crawlers.discard(crawler)
|
||||||
|
self.bootstrap_failed |= not getattr(crawler, "spider", None)
|
||||||
|
|
||||||
def _crawl(self, crawler: Crawler, *args: Any, **kwargs: Any) -> asyncio.Task[None]:
|
def _crawl(self, crawler: Crawler, *args: Any, **kwargs: Any) -> asyncio.Task[None]:
|
||||||
# At this point the asyncio loop has been installed either by the user
|
# At this point the asyncio loop has been installed either by the user
|
||||||
# or by AsyncCrawlerProcess (but it isn't running yet, so no asyncio.create_task()).
|
# or by AsyncCrawlerProcess (but it isn't running yet, so no asyncio.create_task()).
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
self.crawlers.add(crawler)
|
self.crawlers.add(crawler)
|
||||||
|
|
||||||
async def _crawl_and_track() -> None:
|
task = loop.create_task(self._crawl_and_track(crawler, *args, **kwargs))
|
||||||
try:
|
|
||||||
await crawler.crawl_async(*args, **kwargs)
|
|
||||||
except Exception:
|
|
||||||
self.bootstrap_failed = True
|
|
||||||
raise # re-raise so asyncio still logs it to stderr naturally
|
|
||||||
|
|
||||||
task = loop.create_task(_crawl_and_track())
|
|
||||||
self._active.add(task)
|
self._active.add(task)
|
||||||
|
task.add_done_callback(partial(self._done, crawler=crawler))
|
||||||
|
|
||||||
def _done(_: asyncio.Task[None]) -> None:
|
|
||||||
self.crawlers.discard(crawler)
|
|
||||||
self._active.discard(task)
|
|
||||||
self.bootstrap_failed |= not getattr(crawler, "spider", None)
|
|
||||||
|
|
||||||
task.add_done_callback(_done)
|
|
||||||
return task
|
return task
|
||||||
|
|
||||||
async def stop(self) -> None:
|
async def stop(self) -> None:
|
||||||
|
|
@ -766,7 +773,7 @@ class CrawlerProcess(CrawlerProcessBase, CrawlerRunner):
|
||||||
"""
|
"""
|
||||||
This method starts a :mod:`~twisted.internet.reactor`, adjusts its pool
|
This method starts a :mod:`~twisted.internet.reactor`, adjusts its pool
|
||||||
size to :setting:`REACTOR_THREADPOOL_MAXSIZE`, and installs a DNS
|
size to :setting:`REACTOR_THREADPOOL_MAXSIZE`, and installs a DNS
|
||||||
resolver based on :setting:`DNSCACHE_ENABLED`.
|
resolver based on :setting:`TWISTED_DNS_RESOLVER`.
|
||||||
|
|
||||||
If ``stop_after_crawl`` is True, the reactor will be stopped after all
|
If ``stop_after_crawl`` is True, the reactor will be stopped after all
|
||||||
crawlers have finished, using :meth:`join`.
|
crawlers have finished, using :meth:`join`.
|
||||||
|
|
@ -868,10 +875,10 @@ class AsyncCrawlerProcess(CrawlerProcessBase, AsyncCrawlerRunner):
|
||||||
|
|
||||||
When using a reactor it adjusts its pool size to
|
When using a reactor it adjusts its pool size to
|
||||||
:setting:`REACTOR_THREADPOOL_MAXSIZE` and installs a DNS resolver based
|
:setting:`REACTOR_THREADPOOL_MAXSIZE` and installs a DNS resolver based
|
||||||
on :setting:`DNSCACHE_ENABLED`.
|
on :setting:`TWISTED_DNS_RESOLVER`.
|
||||||
|
|
||||||
If ``stop_after_crawl`` is True, the reactor will be stopped after all
|
If ``stop_after_crawl`` is True, the reactor/event loop will be stopped
|
||||||
crawlers have finished, using :meth:`join`.
|
after all crawlers have finished, using :meth:`join`.
|
||||||
|
|
||||||
:param bool stop_after_crawl: stop or not the reactor when all
|
:param bool stop_after_crawl: stop or not the reactor when all
|
||||||
crawlers have finished
|
crawlers have finished
|
||||||
|
|
@ -1007,14 +1014,15 @@ class AsyncCrawlerProcess(CrawlerProcessBase, AsyncCrawlerRunner):
|
||||||
if (loop := self._reactorless_loop) is None:
|
if (loop := self._reactorless_loop) is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
def _create_shutdown_task() -> None:
|
loop.call_soon_threadsafe(self._create_shutdown_task)
|
||||||
coro = self._shutdown_graceful_reactorless()
|
|
||||||
try:
|
|
||||||
loop.create_task(coro)
|
|
||||||
except RuntimeError:
|
|
||||||
coro.close()
|
|
||||||
|
|
||||||
loop.call_soon_threadsafe(_create_shutdown_task)
|
def _create_shutdown_task(self) -> None:
|
||||||
|
assert self._reactorless_loop
|
||||||
|
coro = self._shutdown_graceful_reactorless()
|
||||||
|
try:
|
||||||
|
self._reactorless_loop.create_task(coro)
|
||||||
|
except RuntimeError:
|
||||||
|
coro.close()
|
||||||
|
|
||||||
async def _shutdown_graceful_reactorless(self) -> None:
|
async def _shutdown_graceful_reactorless(self) -> None:
|
||||||
await self.stop()
|
await self.stop()
|
||||||
|
|
|
||||||
|
|
@ -1,114 +0,0 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
from warnings import warn
|
|
||||||
|
|
||||||
from w3lib import html
|
|
||||||
|
|
||||||
from scrapy.exceptions import NotConfigured, ScrapyDeprecationWarning
|
|
||||||
from scrapy.http import HtmlResponse, Response
|
|
||||||
from scrapy.utils.url import escape_ajax
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
# typing.Self requires Python 3.11
|
|
||||||
from typing_extensions import Self
|
|
||||||
|
|
||||||
from scrapy import Request, Spider
|
|
||||||
from scrapy.crawler import Crawler
|
|
||||||
from scrapy.settings import BaseSettings
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class AjaxCrawlMiddleware:
|
|
||||||
"""
|
|
||||||
Handle 'AJAX crawlable' pages marked as crawlable via meta tag.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, settings: BaseSettings):
|
|
||||||
if not settings.getbool("AJAXCRAWL_ENABLED"):
|
|
||||||
raise NotConfigured
|
|
||||||
|
|
||||||
warn(
|
|
||||||
"scrapy.downloadermiddlewares.ajaxcrawl.AjaxCrawlMiddleware is deprecated"
|
|
||||||
" and will be removed in a future Scrapy version.",
|
|
||||||
ScrapyDeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
# XXX: Google parses at least first 100k bytes; scrapy's redirect
|
|
||||||
# middleware parses first 4k. 4k turns out to be insufficient
|
|
||||||
# for this middleware, and parsing 100k could be slow.
|
|
||||||
# We use something in between (32K) by default.
|
|
||||||
self.lookup_bytes: int = settings.getint("AJAXCRAWL_MAXSIZE")
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_crawler(cls, crawler: Crawler) -> Self:
|
|
||||||
return cls(crawler.settings)
|
|
||||||
|
|
||||||
def process_response(
|
|
||||||
self, request: Request, response: Response, spider: Spider
|
|
||||||
) -> Request | Response:
|
|
||||||
if not isinstance(response, HtmlResponse) or response.status != 200:
|
|
||||||
return response
|
|
||||||
|
|
||||||
if request.method != "GET":
|
|
||||||
# other HTTP methods are either not safe or don't have a body
|
|
||||||
return response
|
|
||||||
|
|
||||||
if "ajax_crawlable" in request.meta: # prevent loops
|
|
||||||
return response
|
|
||||||
|
|
||||||
if not self._has_ajax_crawlable_variant(response):
|
|
||||||
return response
|
|
||||||
|
|
||||||
ajax_crawl_request = request.replace(url=escape_ajax(request.url + "#!"))
|
|
||||||
logger.debug(
|
|
||||||
"Downloading AJAX crawlable %(ajax_crawl_request)s instead of %(request)s",
|
|
||||||
{"ajax_crawl_request": ajax_crawl_request, "request": request},
|
|
||||||
extra={"spider": spider},
|
|
||||||
)
|
|
||||||
|
|
||||||
ajax_crawl_request.meta["ajax_crawlable"] = True
|
|
||||||
return ajax_crawl_request
|
|
||||||
|
|
||||||
def _has_ajax_crawlable_variant(self, response: Response) -> bool:
|
|
||||||
"""
|
|
||||||
Return True if a page without hash fragment could be "AJAX crawlable".
|
|
||||||
"""
|
|
||||||
body = response.text[: self.lookup_bytes]
|
|
||||||
return _has_ajaxcrawlable_meta(body)
|
|
||||||
|
|
||||||
|
|
||||||
_ajax_crawlable_re: re.Pattern[str] = re.compile(
|
|
||||||
r'<meta\s+name=["\']fragment["\']\s+content=["\']!["\']/?>'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _has_ajaxcrawlable_meta(text: str) -> bool:
|
|
||||||
"""
|
|
||||||
>>> _has_ajaxcrawlable_meta('<html><head><meta name="fragment" content="!"/></head><body></body></html>')
|
|
||||||
True
|
|
||||||
>>> _has_ajaxcrawlable_meta("<html><head><meta name='fragment' content='!'></head></html>")
|
|
||||||
True
|
|
||||||
>>> _has_ajaxcrawlable_meta('<html><head><!--<meta name="fragment" content="!"/>--></head><body></body></html>')
|
|
||||||
False
|
|
||||||
>>> _has_ajaxcrawlable_meta('<html></html>')
|
|
||||||
False
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Stripping scripts and comments is slow (about 20x slower than
|
|
||||||
# just checking if a string is in text); this is a quick fail-fast
|
|
||||||
# path that should work for most pages.
|
|
||||||
if "fragment" not in text:
|
|
||||||
return False
|
|
||||||
if "content" not in text:
|
|
||||||
return False
|
|
||||||
|
|
||||||
text = html.remove_tags_with_content(text, ("script", "noscript"))
|
|
||||||
text = html.replace_entities(text)
|
|
||||||
text = html.remove_comments(text)
|
|
||||||
return _ajax_crawlable_re.search(text) is not None
|
|
||||||
|
|
@ -12,6 +12,7 @@ from scrapy.http.cookies import CookieJar
|
||||||
from scrapy.utils.decorators import _warn_spider_arg
|
from scrapy.utils.decorators import _warn_spider_arg
|
||||||
from scrapy.utils.httpobj import urlparse_cached
|
from scrapy.utils.httpobj import urlparse_cached
|
||||||
from scrapy.utils.python import to_unicode
|
from scrapy.utils.python import to_unicode
|
||||||
|
from scrapy.utils.request import _decode_cookie, _to_verbose_cookies
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Iterable, Sequence
|
from collections.abc import Iterable, Sequence
|
||||||
|
|
@ -134,29 +135,10 @@ class CookiesMiddleware:
|
||||||
Given a dict consisting of cookie components, return its string representation.
|
Given a dict consisting of cookie components, return its string representation.
|
||||||
Decode from bytes if necessary.
|
Decode from bytes if necessary.
|
||||||
"""
|
"""
|
||||||
decoded = {}
|
decoded = _decode_cookie(cookie, request)
|
||||||
|
if decoded is None:
|
||||||
|
return None
|
||||||
flags = set()
|
flags = set()
|
||||||
for key in ("name", "value", "path", "domain"):
|
|
||||||
value = cookie.get(key)
|
|
||||||
if value is None:
|
|
||||||
if key in {"name", "value"}:
|
|
||||||
msg = f"Invalid cookie found in request {request}: {cookie} ('{key}' is missing)"
|
|
||||||
logger.warning(msg)
|
|
||||||
return None
|
|
||||||
continue
|
|
||||||
if isinstance(value, (bool, float, int, str)):
|
|
||||||
decoded[key] = str(value)
|
|
||||||
else:
|
|
||||||
assert isinstance(value, bytes)
|
|
||||||
try:
|
|
||||||
decoded[key] = value.decode("utf8")
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
logger.warning(
|
|
||||||
"Non UTF-8 encoded cookie found in request %s: %s",
|
|
||||||
request,
|
|
||||||
cookie,
|
|
||||||
)
|
|
||||||
decoded[key] = value.decode("latin1", errors="replace")
|
|
||||||
for flag in ("secure",):
|
for flag in ("secure",):
|
||||||
value = cookie.get(flag, _UNSET)
|
value = cookie.get(flag, _UNSET)
|
||||||
if value is _UNSET or not value:
|
if value is _UNSET or not value:
|
||||||
|
|
@ -177,11 +159,7 @@ class CookiesMiddleware:
|
||||||
"""
|
"""
|
||||||
if not request.cookies:
|
if not request.cookies:
|
||||||
return ()
|
return ()
|
||||||
cookies: Iterable[VerboseCookie]
|
cookies: Iterable[VerboseCookie] = _to_verbose_cookies(request.cookies)
|
||||||
if isinstance(request.cookies, dict):
|
|
||||||
cookies = tuple({"name": k, "value": v} for k, v in request.cookies.items())
|
|
||||||
else:
|
|
||||||
cookies = request.cookies
|
|
||||||
for cookie in cookies:
|
for cookie in cookies:
|
||||||
cookie.setdefault("secure", urlparse_cached(request).scheme == "https")
|
cookie.setdefault("secure", urlparse_cached(request).scheme == "https")
|
||||||
formatted = filter(None, (self._format_cookie(c, request) for c in cookies))
|
formatted = filter(None, (self._format_cookie(c, request) for c in cookies))
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,14 @@ See documentation in docs/topics/downloader-middleware.rst
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import warnings
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from w3lib.http import basic_auth_header
|
from w3lib.http import basic_auth_header
|
||||||
|
|
||||||
from scrapy import Request, Spider, signals
|
from scrapy import Request, Spider, signals
|
||||||
|
from scrapy.exceptions import ScrapyDeprecationWarning
|
||||||
|
from scrapy.settings import SETTINGS_PRIORITIES
|
||||||
from scrapy.utils.decorators import _warn_spider_arg
|
from scrapy.utils.decorators import _warn_spider_arg
|
||||||
from scrapy.utils.url import url_is_from_any_domain
|
from scrapy.utils.url import url_is_from_any_domain
|
||||||
|
|
||||||
|
|
@ -23,12 +26,28 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
|
|
||||||
class HttpAuthMiddleware:
|
class HttpAuthMiddleware:
|
||||||
"""Set Basic HTTP Authorization header
|
"""Set Basic HTTP Authorization header."""
|
||||||
(http_user and http_pass spider class attributes)"""
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._auth: bytes | None = None
|
||||||
|
self._domain: str | None = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_crawler(cls, crawler: Crawler) -> Self:
|
def from_crawler(cls, crawler: Crawler) -> Self:
|
||||||
o = cls()
|
o = cls()
|
||||||
|
usr = crawler.settings.get("HTTPAUTH_USER", "")
|
||||||
|
pwd = crawler.settings.get("HTTPAUTH_PASS", "")
|
||||||
|
if usr or pwd:
|
||||||
|
domain_priority = crawler.settings.getpriority("HTTPAUTH_DOMAIN") or 0
|
||||||
|
if domain_priority <= SETTINGS_PRIORITIES["default"]:
|
||||||
|
raise ValueError(
|
||||||
|
"HTTPAUTH_DOMAIN must be set when HTTPAUTH_USER or HTTPAUTH_PASS "
|
||||||
|
"is configured. Set it to a domain (e.g. 'example.com') to restrict "
|
||||||
|
"credentials to that domain, or set it to None to send credentials "
|
||||||
|
"with all requests."
|
||||||
|
)
|
||||||
|
o._auth = basic_auth_header(usr, pwd)
|
||||||
|
o._domain = crawler.settings.get("HTTPAUTH_DOMAIN")
|
||||||
crawler.signals.connect(o.spider_opened, signal=signals.spider_opened)
|
crawler.signals.connect(o.spider_opened, signal=signals.spider_opened)
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
|
@ -36,18 +55,34 @@ class HttpAuthMiddleware:
|
||||||
usr = getattr(spider, "http_user", "")
|
usr = getattr(spider, "http_user", "")
|
||||||
pwd = getattr(spider, "http_pass", "")
|
pwd = getattr(spider, "http_pass", "")
|
||||||
if usr or pwd:
|
if usr or pwd:
|
||||||
self.auth = basic_auth_header(usr, pwd)
|
warnings.warn(
|
||||||
self.domain = spider.http_auth_domain # type: ignore[attr-defined]
|
"Use the HTTPAUTH_USER, HTTPAUTH_PASS, and HTTPAUTH_DOMAIN settings "
|
||||||
|
"instead of the http_user, http_pass, and http_auth_domain spider "
|
||||||
|
"attributes. Support for the spider attributes will be removed in a "
|
||||||
|
"future version of Scrapy.",
|
||||||
|
category=ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
self._auth = basic_auth_header(usr, pwd)
|
||||||
|
self._domain = spider.http_auth_domain # type: ignore[attr-defined]
|
||||||
|
|
||||||
@_warn_spider_arg
|
@_warn_spider_arg
|
||||||
def process_request(
|
def process_request(
|
||||||
self, request: Request, spider: Spider | None = None
|
self, request: Request, spider: Spider | None = None
|
||||||
) -> Request | Response | None:
|
) -> Request | Response | None:
|
||||||
auth = getattr(self, "auth", None)
|
if b"Authorization" in request.headers:
|
||||||
if (
|
return None
|
||||||
auth
|
# Per-request meta overrides
|
||||||
and b"Authorization" not in request.headers
|
usr = request.meta.get("http_user", "")
|
||||||
and (not self.domain or url_is_from_any_domain(request.url, [self.domain]))
|
pwd = request.meta.get("http_pass", "")
|
||||||
|
if usr or pwd:
|
||||||
|
domain = request.meta.get("http_auth_domain")
|
||||||
|
if not domain or url_is_from_any_domain(request.url, [domain]):
|
||||||
|
request.headers[b"Authorization"] = basic_auth_header(usr, pwd)
|
||||||
|
return None
|
||||||
|
# Middleware-level auth
|
||||||
|
if self._auth and (
|
||||||
|
not self._domain or url_is_from_any_domain(request.url, [self._domain])
|
||||||
):
|
):
|
||||||
request.headers[b"Authorization"] = auth
|
request.headers[b"Authorization"] = self._auth
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from logging import getLogger
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from scrapy import Request, Spider, signals
|
from scrapy import Request, Spider, signals
|
||||||
from scrapy.exceptions import IgnoreRequest, NotConfigured
|
from scrapy.exceptions import IgnoreRequest, NotConfigured, ScrapyDeprecationWarning
|
||||||
from scrapy.http import Response, TextResponse
|
from scrapy.http import Response, TextResponse
|
||||||
from scrapy.responsetypes import responsetypes
|
from scrapy.responsetypes import responsetypes
|
||||||
from scrapy.utils._compression import (
|
from scrapy.utils._compression import (
|
||||||
|
|
@ -60,7 +60,7 @@ else:
|
||||||
|
|
||||||
|
|
||||||
class HttpCompressionMiddleware:
|
class HttpCompressionMiddleware:
|
||||||
"""This middleware allows compressed (gzip, deflate) traffic to be
|
"""This middleware allows compressed (gzip, deflate etc.) traffic to be
|
||||||
sent/received from websites"""
|
sent/received from websites"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -70,6 +70,12 @@ class HttpCompressionMiddleware:
|
||||||
crawler: Crawler | None = None,
|
crawler: Crawler | None = None,
|
||||||
):
|
):
|
||||||
if not crawler:
|
if not crawler:
|
||||||
|
warnings.warn(
|
||||||
|
"Instantiating HttpCompressionMiddleware without a 'crawler' "
|
||||||
|
"argument is deprecated.",
|
||||||
|
category=ScrapyDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
self.stats = stats
|
self.stats = stats
|
||||||
self._max_size = 1073741824
|
self._max_size = 1073741824
|
||||||
self._warn_size = 33554432
|
self._warn_size = 33554432
|
||||||
|
|
@ -108,49 +114,47 @@ class HttpCompressionMiddleware:
|
||||||
) -> Request | Response:
|
) -> Request | Response:
|
||||||
if request.method == "HEAD":
|
if request.method == "HEAD":
|
||||||
return response
|
return response
|
||||||
if isinstance(response, Response):
|
content_encoding = response.headers.getlist("Content-Encoding")
|
||||||
content_encoding = response.headers.getlist("Content-Encoding")
|
if content_encoding:
|
||||||
if content_encoding:
|
max_size = request.meta.get("download_maxsize", self._max_size)
|
||||||
max_size = request.meta.get("download_maxsize", self._max_size)
|
warn_size = request.meta.get("download_warnsize", self._warn_size)
|
||||||
warn_size = request.meta.get("download_warnsize", self._warn_size)
|
try:
|
||||||
try:
|
decoded_body, content_encoding = self._handle_encoding(
|
||||||
decoded_body, content_encoding = self._handle_encoding(
|
response.body, content_encoding, max_size
|
||||||
response.body, content_encoding, max_size
|
|
||||||
)
|
|
||||||
except _DecompressionMaxSizeExceeded as e:
|
|
||||||
raise IgnoreRequest(
|
|
||||||
f"Ignored response {response} because its body "
|
|
||||||
f"({len(response.body)} B compressed, "
|
|
||||||
f"{e.decompressed_size} B decompressed so far) exceeded "
|
|
||||||
f"DOWNLOAD_MAXSIZE ({max_size} B) during decompression."
|
|
||||||
) from e
|
|
||||||
if len(response.body) < warn_size <= len(decoded_body):
|
|
||||||
logger.warning(
|
|
||||||
f"{response} body size after decompression "
|
|
||||||
f"({len(decoded_body)} B) is larger than the "
|
|
||||||
f"download warning size ({warn_size} B)."
|
|
||||||
)
|
|
||||||
if content_encoding:
|
|
||||||
self._warn_unknown_encoding(response, content_encoding)
|
|
||||||
response.headers["Content-Encoding"] = content_encoding
|
|
||||||
if self.stats:
|
|
||||||
self.stats.inc_value(
|
|
||||||
"httpcompression/response_bytes",
|
|
||||||
len(decoded_body),
|
|
||||||
)
|
|
||||||
self.stats.inc_value("httpcompression/response_count")
|
|
||||||
respcls = responsetypes.from_args(
|
|
||||||
headers=response.headers, url=response.url, body=decoded_body
|
|
||||||
)
|
)
|
||||||
kwargs: dict[str, Any] = {"body": decoded_body}
|
except _DecompressionMaxSizeExceeded as e:
|
||||||
if issubclass(respcls, TextResponse):
|
raise IgnoreRequest(
|
||||||
# force recalculating the encoding until we make sure the
|
f"Ignored response {response} because its body "
|
||||||
# responsetypes guessing is reliable
|
f"({len(response.body)} B compressed, "
|
||||||
kwargs["encoding"] = None
|
f"{e.decompressed_size} B decompressed so far) exceeded "
|
||||||
response = response.replace(cls=respcls, **kwargs)
|
f"DOWNLOAD_MAXSIZE ({max_size} B) during decompression."
|
||||||
if not content_encoding:
|
) from e
|
||||||
del response.headers["Content-Encoding"]
|
if len(response.body) < warn_size <= len(decoded_body):
|
||||||
|
logger.warning(
|
||||||
|
f"{response} body size after decompression "
|
||||||
|
f"({len(decoded_body)} B) is larger than the "
|
||||||
|
f"download warning size ({warn_size} B)."
|
||||||
|
)
|
||||||
|
if content_encoding:
|
||||||
|
self._warn_unknown_encoding(response, content_encoding)
|
||||||
|
response.headers["Content-Encoding"] = content_encoding
|
||||||
|
if self.stats:
|
||||||
|
self.stats.inc_value(
|
||||||
|
"httpcompression/response_bytes",
|
||||||
|
len(decoded_body),
|
||||||
|
)
|
||||||
|
self.stats.inc_value("httpcompression/response_count")
|
||||||
|
respcls = responsetypes.from_args(
|
||||||
|
headers=response.headers, url=response.url, body=decoded_body
|
||||||
|
)
|
||||||
|
kwargs: dict[str, Any] = {"body": decoded_body}
|
||||||
|
if issubclass(respcls, TextResponse):
|
||||||
|
# force recalculating the encoding until we make sure the
|
||||||
|
# responsetypes guessing is reliable
|
||||||
|
kwargs["encoding"] = None
|
||||||
|
response = response.replace(cls=respcls, **kwargs)
|
||||||
|
if not content_encoding:
|
||||||
|
del response.headers["Content-Encoding"]
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _handle_encoding(
|
def _handle_encoding(
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import warnings
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from scrapy import Request, Spider, signals
|
from scrapy import Request, Spider, signals
|
||||||
|
|
@ -75,25 +74,10 @@ class OffsiteMiddleware:
|
||||||
allowed_domains = getattr(spider, "allowed_domains", None)
|
allowed_domains = getattr(spider, "allowed_domains", None)
|
||||||
if not allowed_domains:
|
if not allowed_domains:
|
||||||
return re.compile("") # allow all by default
|
return re.compile("") # allow all by default
|
||||||
url_pattern = re.compile(r"^https?://.*$")
|
|
||||||
port_pattern = re.compile(r":\d+$")
|
|
||||||
domains = []
|
domains = []
|
||||||
for domain in allowed_domains:
|
for domain in allowed_domains:
|
||||||
if domain is None:
|
if domain is None:
|
||||||
continue
|
continue
|
||||||
if url_pattern.match(domain):
|
domains.append(re.escape(domain))
|
||||||
message = (
|
|
||||||
"allowed_domains accepts only domains, not URLs. "
|
|
||||||
f"Ignoring URL entry {domain} in allowed_domains."
|
|
||||||
)
|
|
||||||
warnings.warn(message, stacklevel=2)
|
|
||||||
elif port_pattern.search(domain):
|
|
||||||
message = (
|
|
||||||
"allowed_domains accepts only domains without ports. "
|
|
||||||
f"Ignoring entry {domain} in allowed_domains."
|
|
||||||
)
|
|
||||||
warnings.warn(message, stacklevel=2)
|
|
||||||
else:
|
|
||||||
domains.append(re.escape(domain))
|
|
||||||
regex = rf"^(.*\.)?({'|'.join(domains)})$"
|
regex = rf"^(.*\.)?({'|'.join(domains)})$"
|
||||||
return re.compile(regex)
|
return re.compile(regex)
|
||||||
|
|
|
||||||
|
|
@ -196,10 +196,7 @@ class BaseRedirectMiddleware:
|
||||||
|
|
||||||
|
|
||||||
class RedirectMiddleware(BaseRedirectMiddleware):
|
class RedirectMiddleware(BaseRedirectMiddleware):
|
||||||
"""
|
"""Handle redirection of requests based on response status."""
|
||||||
Handle redirection of requests based on response status
|
|
||||||
and meta-refresh html tag.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@_warn_spider_arg
|
@_warn_spider_arg
|
||||||
def process_response(
|
def process_response(
|
||||||
|
|
@ -251,6 +248,8 @@ class RedirectMiddleware(BaseRedirectMiddleware):
|
||||||
|
|
||||||
|
|
||||||
class MetaRefreshMiddleware(BaseRedirectMiddleware):
|
class MetaRefreshMiddleware(BaseRedirectMiddleware):
|
||||||
|
"""Handle redirection of requests based on meta-refresh html tag."""
|
||||||
|
|
||||||
enabled_setting = "METAREFRESH_ENABLED"
|
enabled_setting = "METAREFRESH_ENABLED"
|
||||||
|
|
||||||
def __init__(self, settings: BaseSettings):
|
def __init__(self, settings: BaseSettings):
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,11 @@ problems such as a connection timeout or HTTP 500 error.
|
||||||
You can change the behaviour of this middleware by modifying the scraping settings:
|
You can change the behaviour of this middleware by modifying the scraping settings:
|
||||||
RETRY_TIMES - how many times to retry a failed page
|
RETRY_TIMES - how many times to retry a failed page
|
||||||
RETRY_HTTP_CODES - which HTTP response codes to retry
|
RETRY_HTTP_CODES - which HTTP response codes to retry
|
||||||
|
|
||||||
Failed pages are collected on the scraping process and rescheduled at the end,
|
|
||||||
once the spider has finished crawling all regular (non-failed) pages.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from logging import Logger, getLogger
|
from logging import Logger, getLevelName, getLogger
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from scrapy.exceptions import NotConfigured
|
from scrapy.exceptions import NotConfigured
|
||||||
|
|
@ -43,6 +40,7 @@ def get_retry_request(
|
||||||
max_retry_times: int | None = None,
|
max_retry_times: int | None = None,
|
||||||
priority_adjust: int | None = None,
|
priority_adjust: int | None = None,
|
||||||
logger: Logger = retry_logger,
|
logger: Logger = retry_logger,
|
||||||
|
give_up_log_level: int | str | None = None,
|
||||||
stats_base_key: str = "retry",
|
stats_base_key: str = "retry",
|
||||||
) -> Request | None:
|
) -> Request | None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -51,14 +49,16 @@ def get_retry_request(
|
||||||
exhausted.
|
exhausted.
|
||||||
|
|
||||||
For example, in a :class:`~scrapy.Spider` callback, you could use it as
|
For example, in a :class:`~scrapy.Spider` callback, you could use it as
|
||||||
follows::
|
follows:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
def parse(self, response):
|
def parse(self, response):
|
||||||
if not response.text:
|
if not response.text:
|
||||||
new_request_or_none = get_retry_request(
|
new_request_or_none = get_retry_request(
|
||||||
response.request,
|
response.request,
|
||||||
spider=self,
|
spider=self,
|
||||||
reason='empty',
|
reason="empty",
|
||||||
)
|
)
|
||||||
return new_request_or_none
|
return new_request_or_none
|
||||||
|
|
||||||
|
|
@ -67,8 +67,9 @@ def get_retry_request(
|
||||||
and :ref:`stats <topics-stats>`, and to provide extra logging context (see
|
and :ref:`stats <topics-stats>`, and to provide extra logging context (see
|
||||||
:func:`logging.debug`).
|
:func:`logging.debug`).
|
||||||
|
|
||||||
*reason* is a string or an :class:`Exception` object that indicates the
|
*reason* is a string, an :class:`Exception` subclass or an
|
||||||
reason why the request needs to be retried. It is used to name retry stats.
|
:class:`Exception` object that indicates the reason why the request needs
|
||||||
|
to be retried. It is used to name retry stats.
|
||||||
|
|
||||||
*max_retry_times* is a number that determines the maximum number of times
|
*max_retry_times* is a number that determines the maximum number of times
|
||||||
that *request* can be retried. If not specified or ``None``, the number is
|
that *request* can be retried. If not specified or ``None``, the number is
|
||||||
|
|
@ -82,6 +83,13 @@ def get_retry_request(
|
||||||
|
|
||||||
*logger* is the logging.Logger object to be used when logging messages
|
*logger* is the logging.Logger object to be used when logging messages
|
||||||
|
|
||||||
|
*give_up_log_level* is the :ref:`logging level <levels>` used for the
|
||||||
|
message logged when a request exceeds its retries. See
|
||||||
|
:setting:`RETRY_GIVE_UP_LOG_LEVEL` for details.
|
||||||
|
|
||||||
|
.. versionadded:: 2.17.0
|
||||||
|
The *give_up_log_level* parameter.
|
||||||
|
|
||||||
*stats_base_key* is a string to be used as the base key for the
|
*stats_base_key* is a string to be used as the base key for the
|
||||||
retry-related job stats
|
retry-related job stats
|
||||||
"""
|
"""
|
||||||
|
|
@ -114,8 +122,16 @@ def get_retry_request(
|
||||||
stats.inc_value(f"{stats_base_key}/count")
|
stats.inc_value(f"{stats_base_key}/count")
|
||||||
stats.inc_value(f"{stats_base_key}/reason_count/{reason}")
|
stats.inc_value(f"{stats_base_key}/reason_count/{reason}")
|
||||||
return new_request
|
return new_request
|
||||||
|
if give_up_log_level is None:
|
||||||
|
give_up_log_level = settings["RETRY_GIVE_UP_LOG_LEVEL"]
|
||||||
|
if isinstance(give_up_log_level, str):
|
||||||
|
level = getLevelName(give_up_log_level)
|
||||||
|
if not isinstance(level, int):
|
||||||
|
raise ValueError(f"Invalid give-up log level: {give_up_log_level!r}")
|
||||||
|
give_up_log_level = level
|
||||||
stats.inc_value(f"{stats_base_key}/max_reached")
|
stats.inc_value(f"{stats_base_key}/max_reached")
|
||||||
logger.error(
|
logger.log(
|
||||||
|
give_up_log_level,
|
||||||
"Gave up retrying %(request)s (failed %(retry_times)d times): %(reason)s",
|
"Gave up retrying %(request)s (failed %(retry_times)d times): %(reason)s",
|
||||||
{"request": request, "retry_times": retry_times, "reason": reason},
|
{"request": request, "retry_times": retry_times, "reason": reason},
|
||||||
extra={"spider": spider},
|
extra={"spider": spider},
|
||||||
|
|
@ -132,6 +148,7 @@ class RetryMiddleware:
|
||||||
self.max_retry_times = settings.getint("RETRY_TIMES")
|
self.max_retry_times = settings.getint("RETRY_TIMES")
|
||||||
self.retry_http_codes = {int(x) for x in settings.getlist("RETRY_HTTP_CODES")}
|
self.retry_http_codes = {int(x) for x in settings.getlist("RETRY_HTTP_CODES")}
|
||||||
self.priority_adjust = settings.getint("RETRY_PRIORITY_ADJUST")
|
self.priority_adjust = settings.getint("RETRY_PRIORITY_ADJUST")
|
||||||
|
self.give_up_log_level = settings["RETRY_GIVE_UP_LOG_LEVEL"]
|
||||||
self.exceptions_to_retry = tuple(
|
self.exceptions_to_retry = tuple(
|
||||||
load_object(x) if isinstance(x, str) else x
|
load_object(x) if isinstance(x, str) else x
|
||||||
for x in settings.getlist("RETRY_EXCEPTIONS")
|
for x in settings.getlist("RETRY_EXCEPTIONS")
|
||||||
|
|
@ -175,6 +192,9 @@ class RetryMiddleware:
|
||||||
) -> Request | None:
|
) -> Request | None:
|
||||||
max_retry_times = request.meta.get("max_retry_times", self.max_retry_times)
|
max_retry_times = request.meta.get("max_retry_times", self.max_retry_times)
|
||||||
priority_adjust = request.meta.get("priority_adjust", self.priority_adjust)
|
priority_adjust = request.meta.get("priority_adjust", self.priority_adjust)
|
||||||
|
give_up_log_level = request.meta.get(
|
||||||
|
"give_up_log_level", self.give_up_log_level
|
||||||
|
)
|
||||||
assert self.crawler.spider
|
assert self.crawler.spider
|
||||||
return get_retry_request(
|
return get_retry_request(
|
||||||
request,
|
request,
|
||||||
|
|
@ -182,4 +202,5 @@ class RetryMiddleware:
|
||||||
spider=self.crawler.spider,
|
spider=self.crawler.spider,
|
||||||
max_retry_times=max_retry_times,
|
max_retry_times=max_retry_times,
|
||||||
priority_adjust=priority_adjust,
|
priority_adjust=priority_adjust,
|
||||||
|
give_up_log_level=give_up_log_level,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ class UsageError(Exception):
|
||||||
|
|
||||||
class ScrapyDeprecationWarning(Warning):
|
class ScrapyDeprecationWarning(Warning):
|
||||||
"""Warning category for deprecated features, since the default
|
"""Warning category for deprecated features, since the default
|
||||||
DeprecationWarning is silenced on Python 2.7+
|
:exc:`DeprecationWarning` is silenced.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ Item Exporters are used to export/serialize items into different formats.
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
|
import logging
|
||||||
import marshal
|
import marshal
|
||||||
import pickle
|
import pickle
|
||||||
import pprint
|
import pprint
|
||||||
|
|
@ -24,6 +25,8 @@ from scrapy.utils.serialize import ScrapyJSONEncoder
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from json import JSONEncoder
|
from json import JSONEncoder
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"BaseItemExporter",
|
"BaseItemExporter",
|
||||||
"CsvItemExporter",
|
"CsvItemExporter",
|
||||||
|
|
@ -171,7 +174,15 @@ class XmlItemExporter(BaseItemExporter):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
if not self.encoding:
|
if not self.encoding:
|
||||||
self.encoding = "utf-8"
|
self.encoding = "utf-8"
|
||||||
self.xg = XMLGenerator(file, encoding=self.encoding)
|
# copied from xml.sax.saxutils._gettextwriter()
|
||||||
|
self.stream = TextIOWrapper(
|
||||||
|
file,
|
||||||
|
encoding=self.encoding,
|
||||||
|
errors="xmlcharrefreplace",
|
||||||
|
newline="\n",
|
||||||
|
write_through=True,
|
||||||
|
)
|
||||||
|
self.xg = XMLGenerator(self.stream, encoding=self.encoding)
|
||||||
|
|
||||||
def _beautify_newline(self, new_item: bool = False) -> None:
|
def _beautify_newline(self, new_item: bool = False) -> None:
|
||||||
if self.indent is not None and (self.indent > 0 or new_item):
|
if self.indent is not None and (self.indent > 0 or new_item):
|
||||||
|
|
@ -199,6 +210,7 @@ class XmlItemExporter(BaseItemExporter):
|
||||||
def finish_exporting(self) -> None:
|
def finish_exporting(self) -> None:
|
||||||
self.xg.endElement(self.root_element)
|
self.xg.endElement(self.root_element)
|
||||||
self.xg.endDocument()
|
self.xg.endDocument()
|
||||||
|
self.stream.detach() # Avoid closing the wrapped file.
|
||||||
|
|
||||||
def _export_xml_field(self, name: str, serialized_value: Any, depth: int) -> None:
|
def _export_xml_field(self, name: str, serialized_value: Any, depth: int) -> None:
|
||||||
self._beautify_indent(depth=depth)
|
self._beautify_indent(depth=depth)
|
||||||
|
|
@ -245,6 +257,8 @@ class CsvItemExporter(BaseItemExporter):
|
||||||
self.csv_writer = csv.writer(self.stream, **self._kwargs)
|
self.csv_writer = csv.writer(self.stream, **self._kwargs)
|
||||||
self._headers_not_written = True
|
self._headers_not_written = True
|
||||||
self._join_multivalued = join_multivalued
|
self._join_multivalued = join_multivalued
|
||||||
|
self._autodetected_fields = False
|
||||||
|
self._data_loss_warned = False
|
||||||
|
|
||||||
def serialize_field(
|
def serialize_field(
|
||||||
self, field: Mapping[str, Any] | Field, name: str, value: Any
|
self, field: Mapping[str, Any] | Field, name: str, value: Any
|
||||||
|
|
@ -265,6 +279,22 @@ class CsvItemExporter(BaseItemExporter):
|
||||||
self._headers_not_written = False
|
self._headers_not_written = False
|
||||||
self._write_headers_and_set_fields_to_export(item)
|
self._write_headers_and_set_fields_to_export(item)
|
||||||
|
|
||||||
|
if (
|
||||||
|
self._autodetected_fields
|
||||||
|
and self.fields_to_export is not None
|
||||||
|
and not self._data_loss_warned
|
||||||
|
):
|
||||||
|
item_fields = ItemAdapter(item).field_names()
|
||||||
|
dropped_fields = set(item_fields) - set(self.fields_to_export)
|
||||||
|
|
||||||
|
if dropped_fields:
|
||||||
|
dropped_fields_display = sorted(dropped_fields)
|
||||||
|
logger.warning(
|
||||||
|
f"CSVExporter dropped fields {dropped_fields_display}. "
|
||||||
|
f"To avoid this, fully configure your FEED_EXPORT_FIELDS setting. "
|
||||||
|
f"See: https://docs.scrapy.org/en/latest/topics/feed-exports.html#feed-export-fields",
|
||||||
|
)
|
||||||
|
self._data_loss_warned = True
|
||||||
fields = self._get_serialized_fields(item, default_value="", include_empty=True)
|
fields = self._get_serialized_fields(item, default_value="", include_empty=True)
|
||||||
values = list(self._build_row(x for _, x in fields))
|
values = list(self._build_row(x for _, x in fields))
|
||||||
self.csv_writer.writerow(values)
|
self.csv_writer.writerow(values)
|
||||||
|
|
@ -284,6 +314,7 @@ class CsvItemExporter(BaseItemExporter):
|
||||||
if not self.fields_to_export:
|
if not self.fields_to_export:
|
||||||
# use declared field names, or keys if the item is a dict
|
# use declared field names, or keys if the item is a dict
|
||||||
self.fields_to_export = ItemAdapter(item).field_names()
|
self.fields_to_export = ItemAdapter(item).field_names()
|
||||||
|
self._autodetected_fields = True
|
||||||
fields: Iterable[str]
|
fields: Iterable[str]
|
||||||
if isinstance(self.fields_to_export, Mapping):
|
if isinstance(self.fields_to_export, Mapping):
|
||||||
fields = self.fields_to_export.values()
|
fields = self.fields_to_export.values()
|
||||||
|
|
|
||||||
|
|
@ -20,4 +20,6 @@ class ExtensionManager(MiddlewareManager):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_mwlist_from_settings(cls, settings: Settings) -> list[Any]:
|
def _get_mwlist_from_settings(cls, settings: Settings) -> list[Any]:
|
||||||
return build_component_list(settings.getwithbase("EXTENSIONS"))
|
return build_component_list(
|
||||||
|
settings.get_component_priority_dict_with_base("EXTENSIONS")
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ Extension for collecting core stats like items scraped and start/finish times
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from time import monotonic
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from scrapy import Spider, signals
|
from scrapy import Spider, signals
|
||||||
|
|
@ -21,6 +22,7 @@ class CoreStats:
|
||||||
def __init__(self, stats: StatsCollector):
|
def __init__(self, stats: StatsCollector):
|
||||||
self.stats: StatsCollector = stats
|
self.stats: StatsCollector = stats
|
||||||
self.start_time: datetime | None = None
|
self.start_time: datetime | None = None
|
||||||
|
self._start_time_mono: float | None = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_crawler(cls, crawler: Crawler) -> Self:
|
def from_crawler(cls, crawler: Crawler) -> Self:
|
||||||
|
|
@ -35,13 +37,14 @@ class CoreStats:
|
||||||
|
|
||||||
def spider_opened(self, spider: Spider) -> None:
|
def spider_opened(self, spider: Spider) -> None:
|
||||||
self.start_time = datetime.now(tz=timezone.utc)
|
self.start_time = datetime.now(tz=timezone.utc)
|
||||||
|
self._start_time_mono = monotonic()
|
||||||
self.stats.set_value("start_time", self.start_time)
|
self.stats.set_value("start_time", self.start_time)
|
||||||
|
|
||||||
def spider_closed(self, spider: Spider, reason: str) -> None:
|
def spider_closed(self, spider: Spider, reason: str) -> None:
|
||||||
assert self.start_time is not None
|
assert self.start_time is not None
|
||||||
finish_time = datetime.now(tz=timezone.utc)
|
assert self._start_time_mono is not None
|
||||||
elapsed_time = finish_time - self.start_time
|
finish_time, finish_time_mono = datetime.now(tz=timezone.utc), monotonic()
|
||||||
elapsed_time_seconds = elapsed_time.total_seconds()
|
elapsed_time_seconds = finish_time_mono - self._start_time_mono
|
||||||
self.stats.set_value("elapsed_time_seconds", elapsed_time_seconds)
|
self.stats.set_value("elapsed_time_seconds", elapsed_time_seconds)
|
||||||
self.stats.set_value("finish_time", finish_time)
|
self.stats.set_value("finish_time", finish_time)
|
||||||
self.stats.set_value("finish_reason", reason)
|
self.stats.set_value("finish_reason", reason)
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue