Datasets:
Add detailed dataset card and the scraping pipeline used to build it
Browse files- README.md +52 -9
- scraping/.env.example +52 -0
- scraping/README.md +44 -8
- scraping/crawler.py +75 -38
- scraping/proxies.example.txt +15 -0
README.md
CHANGED
|
@@ -18,6 +18,8 @@ tags:
|
|
| 18 |
- news
|
| 19 |
- news-articles
|
| 20 |
- opinion-journalism
|
|
|
|
|
|
|
| 21 |
- web-scraping
|
| 22 |
- weekly-newspaper
|
| 23 |
annotations_creators:
|
|
@@ -68,8 +70,17 @@ a set of comparable corpora covering the German media landscape from public-serv
|
|
| 68 |
broadcasters to alternative and fringe outlets, all built with the same crawler and the same
|
| 69 |
extraction pipeline so that they can be compared and combined directly.
|
| 70 |
|
| 71 |
-
> ⚠️
|
| 72 |
-
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
## At a glance
|
| 75 |
|
|
@@ -82,6 +93,7 @@ extraction pipeline so that they can be compared and combined directly.
|
|
| 82 |
| **Split** | `train` (single split) |
|
| 83 |
| **Format** | Parquet, 1 shard, ≈ 89 MB download / 138 MB in memory |
|
| 84 |
| **Perceived partisanship** | 2.7 / 7 → *left of centre* ([medienkompass.org](https://medienkompass.org/deutsche-medienlandschaft/) reader survey) |
|
|
|
|
| 85 |
| **Mirror** | [`NLP-UniBW/der_freitag_de`](https://huggingface.co/datasets/NLP-UniBW/der_freitag_de) |
|
| 86 |
|
| 87 |
```python
|
|
@@ -440,13 +452,44 @@ corpus = concatenate_datasets(parts)
|
|
| 440 |
|
| 441 |
## Licensing and intended use
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
§ 60d UrhG
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
## Citation
|
| 452 |
|
|
|
|
| 18 |
- news
|
| 19 |
- news-articles
|
| 20 |
- opinion-journalism
|
| 21 |
+
- research-only
|
| 22 |
+
- text-and-data-mining
|
| 23 |
- web-scraping
|
| 24 |
- weekly-newspaper
|
| 25 |
annotations_creators:
|
|
|
|
| 70 |
broadcasters to alternative and fringe outlets, all built with the same crawler and the same
|
| 71 |
extraction pipeline so that they can be compared and combined directly.
|
| 72 |
|
| 73 |
+
> ## ⚠️ For scientific research only
|
| 74 |
+
>
|
| 75 |
+
> This corpus is published **exclusively for scientific research** under the German
|
| 76 |
+
> research exception for text and data mining — the *Forschungsprivileg* of
|
| 77 |
+
> **§ 60d UrhG** (implementing Art. 3 of EU Directive 2019/790). The article texts remain
|
| 78 |
+
> the copyright of their publisher and are **not** licensed for commercial use,
|
| 79 |
+
> redistribution, or republication. By downloading it you confirm that you use it for
|
| 80 |
+
> non-commercial scientific research. See [Licensing and intended use](#licensing-and-intended-use).
|
| 81 |
+
>
|
| 82 |
+
> The articles are reproduced verbatim from the publisher's website; their content expresses
|
| 83 |
+
> the views of that publisher, not of the dataset author.
|
| 84 |
|
| 85 |
## At a glance
|
| 86 |
|
|
|
|
| 93 |
| **Split** | `train` (single split) |
|
| 94 |
| **Format** | Parquet, 1 shard, ≈ 89 MB download / 138 MB in memory |
|
| 95 |
| **Perceived partisanship** | 2.7 / 7 → *left of centre* ([medienkompass.org](https://medienkompass.org/deutsche-medienlandschaft/) reader survey) |
|
| 96 |
+
| **Intended use** | Scientific research only (*Forschungsprivileg*, § 60d UrhG) |
|
| 97 |
| **Mirror** | [`NLP-UniBW/der_freitag_de`](https://huggingface.co/datasets/NLP-UniBW/der_freitag_de) |
|
| 98 |
|
| 99 |
```python
|
|
|
|
| 452 |
|
| 453 |
## Licensing and intended use
|
| 454 |
|
| 455 |
+
### Scientific research only — *Forschungsprivileg* (§ 60d UrhG)
|
| 456 |
+
|
| 457 |
+
This dataset is made available **solely for the purpose of scientific research**. The legal basis
|
| 458 |
+
is the German text-and-data-mining exception for research, the so-called **Forschungsprivileg**:
|
| 459 |
+
|
| 460 |
+
- **§ 60d UrhG** — *Text und Data Mining für Zwecke der wissenschaftlichen Forschung* — allows
|
| 461 |
+
research organisations to make reproductions of lawfully accessible works in order to carry out
|
| 462 |
+
text and data mining for **non-commercial** scientific research, and to share the resulting
|
| 463 |
+
corpus with a specifically limited circle of people for joint research and with individual third
|
| 464 |
+
parties for the purpose of quality assurance.
|
| 465 |
+
- It implements **Art. 3 of Directive (EU) 2019/790** (the DSM Directive); comparable research
|
| 466 |
+
exceptions exist in the other EU member states.
|
| 467 |
+
- § 60d UrhG does **not** cover commercial exploitation. Research organisations that give a
|
| 468 |
+
commercial enterprise preferential access are explicitly excluded from the privilege.
|
| 469 |
+
|
| 470 |
+
### What this means for you
|
| 471 |
+
|
| 472 |
+
✅ **Permitted** — using the corpus for non-commercial scientific research: text and data mining,
|
| 473 |
+
corpus linguistics, media-bias and discourse analysis, training and evaluating research models,
|
| 474 |
+
reproducing published findings, and quality checks by individual third parties.
|
| 475 |
+
|
| 476 |
+
❌ **Not permitted** — commercial use or commercial model training; redistributing or republishing
|
| 477 |
+
the article texts; presenting the texts as your own; building products or services on the article
|
| 478 |
+
texts; any use that substitutes for reading the publisher's own website.
|
| 479 |
+
|
| 480 |
+
The **article texts remain the copyright of der Freitag and its publisher.** No licence to them is
|
| 481 |
+
granted or implied by this repository. Only the crawling and extraction code in `scraping/` is
|
| 482 |
+
offered for free reuse.
|
| 483 |
+
|
| 484 |
+
If your intended use falls outside the research exception, obtain permission from the rights
|
| 485 |
+
holder, or use only the derived, non-expressive data (statistics, embeddings, counts). Note that
|
| 486 |
+
§ 60d UrhG also expects research corpora to be deleted once they are no longer needed for the
|
| 487 |
+
research purpose, with archiving permitted at trusted institutions.
|
| 488 |
+
|
| 489 |
+
If you are a rights holder and want content removed, please open a discussion on this repository.
|
| 490 |
+
|
| 491 |
+
> This section is an orientation for researchers, not legal advice. Responsibility for your own
|
| 492 |
+
> use of the data remains with you and your institution.
|
| 493 |
|
| 494 |
## Citation
|
| 495 |
|
scraping/.env.example
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---------------------------------------------------------------------------
|
| 2 |
+
# crawler.py configuration for www.freitag.de
|
| 3 |
+
#
|
| 4 |
+
# Copy this file to `.env` and uncomment the lines you need:
|
| 5 |
+
#
|
| 6 |
+
# cp .env.example .env
|
| 7 |
+
#
|
| 8 |
+
# Variables that are already set in the real environment take precedence over
|
| 9 |
+
# this file, so a single run can still be overridden on the command line:
|
| 10 |
+
#
|
| 11 |
+
# SCRAPER_THREADS=4 python crawler.py
|
| 12 |
+
#
|
| 13 |
+
# Everything here is optional - the defaults shown are exactly the values that
|
| 14 |
+
# were used for the original crawl.
|
| 15 |
+
# ---------------------------------------------------------------------------
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# === Proxies ===============================================================
|
| 19 |
+
|
| 20 |
+
# Inline pool. Separate entries with commas, semicolons, spaces or newlines.
|
| 21 |
+
# A scheme is optional - "http://" is assumed when none is given. One entry is
|
| 22 |
+
# picked per request, so several entries spread the load over several exits.
|
| 23 |
+
#SCRAPER_PROXIES=proxy-a.example.com:3128,proxy-b.example.com:3128
|
| 24 |
+
|
| 25 |
+
# ... with an explicit scheme and credentials:
|
| 26 |
+
#SCRAPER_PROXIES=http://user:pass@proxy.example.com:3128
|
| 27 |
+
|
| 28 |
+
# ... SOCKS (requires PySocks):
|
| 29 |
+
#SCRAPER_PROXIES=socks5://localhost:9050
|
| 30 |
+
|
| 31 |
+
# For a whole proxy farm, keep the list in its own file instead: one proxy per
|
| 32 |
+
# line, blank lines and "#" comments allowed. See `proxies.example.txt`.
|
| 33 |
+
# This scales to thousands of entries and can be regenerated by another tool
|
| 34 |
+
# without ever touching this file. Both settings may be combined.
|
| 35 |
+
#SCRAPER_PROXY_FILE=proxies.txt
|
| 36 |
+
|
| 37 |
+
# Leave both unset to crawl over a direct connection - that is the default.
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# === Crawler tuning ========================================================
|
| 41 |
+
|
| 42 |
+
# Number of worker processes sharing the URL frontier.
|
| 43 |
+
#SCRAPER_THREADS=32
|
| 44 |
+
|
| 45 |
+
# Socket timeout per request, in seconds.
|
| 46 |
+
#SCRAPER_TIMEOUT=10.0
|
| 47 |
+
|
| 48 |
+
# Attempts per URL before it is given up on.
|
| 49 |
+
#SCRAPER_RETRIES=10
|
| 50 |
+
|
| 51 |
+
# Seed URL the crawl starts from. Any article URL on the site works.
|
| 52 |
+
#SCRAPER_START_URL=https://www.freitag.de/autoren/franz-schandl/asylpolitik-in-oesterreich-drangsalieren-mehr-faellt-ihnen-nicht-ein
|
scraping/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
| 4 |
|---|---|
|
| 5 |
| [`crawler.py`](./crawler.py) | Distributed whole-site crawler for `www.freitag.de`. Writes one JSON file per article to `pages/`. |
|
| 6 |
| [`finalize.ipynb`](./finalize.ipynb) | Merges `pages/*.json` into a single table, deduplicates it and pushes it to the Hub. |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
```bash
|
| 9 |
pip install mechanicalsoup beautifulsoup4 lxml python-dateutil fake-useragent \
|
|
@@ -13,19 +15,44 @@ python crawler.py # writes pages/*.json; `touch killall.txt` to stop
|
|
| 13 |
jupyter notebook finalize.ipynb
|
| 14 |
```
|
| 15 |
|
| 16 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
The original crawl ran behind a pool of forward proxies on a private network.
|
| 19 |
-
Those addresses have been removed from this published copy.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
One entry is picked per request, so a pool spreads the load over several exits.
|
| 23 |
|
| 24 |
```bash
|
| 25 |
# a single proxy
|
| 26 |
export SCRAPER_PROXIES="proxy-a.example.com:3128"
|
| 27 |
|
| 28 |
-
# a pool, rotated per request
|
| 29 |
export SCRAPER_PROXIES="proxy-a.example.com:3128,proxy-b.example.com:3128"
|
| 30 |
|
| 31 |
# with an explicit scheme and credentials
|
|
@@ -33,13 +60,22 @@ export SCRAPER_PROXIES="http://user:pass@proxy.example.com:3128"
|
|
| 33 |
|
| 34 |
# SOCKS, if PySocks is installed
|
| 35 |
export SCRAPER_PROXIES="socks5://localhost:9050"
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
python crawler.py
|
| 41 |
```
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
The scripts contain no credentials, cookies or personal data.
|
| 44 |
|
| 45 |
Please crawl responsibly: honour `robots.txt` and the site's terms of use, keep
|
|
|
|
| 4 |
|---|---|
|
| 5 |
| [`crawler.py`](./crawler.py) | Distributed whole-site crawler for `www.freitag.de`. Writes one JSON file per article to `pages/`. |
|
| 6 |
| [`finalize.ipynb`](./finalize.ipynb) | Merges `pages/*.json` into a single table, deduplicates it and pushes it to the Hub. |
|
| 7 |
+
| [`.env.example`](./.env.example) | Configuration template. Copy to `.env` and uncomment what you need. |
|
| 8 |
+
| [`proxies.example.txt`](./proxies.example.txt) | Proxy-list template for `SCRAPER_PROXY_FILE` — one proxy per line. |
|
| 9 |
|
| 10 |
```bash
|
| 11 |
pip install mechanicalsoup beautifulsoup4 lxml python-dateutil fake-useragent \
|
|
|
|
| 15 |
jupyter notebook finalize.ipynb
|
| 16 |
```
|
| 17 |
|
| 18 |
+
## Configuration
|
| 19 |
+
|
| 20 |
+
All settings are optional and are read from the environment or from a `.env`
|
| 21 |
+
file next to `crawler.py`. The defaults are exactly the values used for the
|
| 22 |
+
original crawl.
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
cp .env.example .env # then uncomment what you need
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
| Variable | Meaning |
|
| 29 |
+
|---|---|
|
| 30 |
+
| `SCRAPER_PROXIES` | Inline proxy pool — entries separated by commas, semicolons, spaces or newlines. |
|
| 31 |
+
| `SCRAPER_PROXY_FILE` | Path to a file with **one proxy per line** — the practical form for a proxy farm. |
|
| 32 |
+
| `SCRAPER_THREADS` | Number of worker processes sharing the URL frontier. |
|
| 33 |
+
| `SCRAPER_TIMEOUT` | Socket timeout per request, in seconds. |
|
| 34 |
+
| `SCRAPER_RETRIES` | Attempts per URL before giving up. |
|
| 35 |
+
| `SCRAPER_START_URL` | Seed URL the crawl starts from. |
|
| 36 |
+
|
| 37 |
+
Environment variables take precedence over `.env`, so a single run can be
|
| 38 |
+
overridden on the command line:
|
| 39 |
+
|
| 40 |
+
```bash
|
| 41 |
+
SCRAPER_THREADS=4 python crawler.py
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
### Proxies
|
| 45 |
|
| 46 |
The original crawl ran behind a pool of forward proxies on a private network.
|
| 47 |
+
Those addresses have been removed from this published copy. A scheme is
|
| 48 |
+
optional on every entry (`http://` is assumed); one entry is picked per
|
| 49 |
+
request, so a pool spreads the load over several exits.
|
|
|
|
| 50 |
|
| 51 |
```bash
|
| 52 |
# a single proxy
|
| 53 |
export SCRAPER_PROXIES="proxy-a.example.com:3128"
|
| 54 |
|
| 55 |
+
# a small pool, rotated per request
|
| 56 |
export SCRAPER_PROXIES="proxy-a.example.com:3128,proxy-b.example.com:3128"
|
| 57 |
|
| 58 |
# with an explicit scheme and credentials
|
|
|
|
| 60 |
|
| 61 |
# SOCKS, if PySocks is installed
|
| 62 |
export SCRAPER_PROXIES="socks5://localhost:9050"
|
| 63 |
+
```
|
| 64 |
|
| 65 |
+
For a **whole proxy farm**, keep the list in its own file — one proxy per line,
|
| 66 |
+
blank lines and `#` comments allowed — and point `SCRAPER_PROXY_FILE` at it.
|
| 67 |
+
The file can hold thousands of entries and be regenerated by another tool
|
| 68 |
+
without touching `.env` or the script:
|
| 69 |
|
| 70 |
+
```bash
|
| 71 |
+
cp proxies.example.txt proxies.txt # then fill in your own entries
|
| 72 |
+
echo 'SCRAPER_PROXY_FILE=proxies.txt' >> .env
|
| 73 |
python crawler.py
|
| 74 |
```
|
| 75 |
|
| 76 |
+
Leave both unset to crawl over a direct connection — that is the default and
|
| 77 |
+
what most of the crawls in this collection used.
|
| 78 |
+
|
| 79 |
The scripts contain no credentials, cookies or personal data.
|
| 80 |
|
| 81 |
Please crawl responsibly: honour `robots.txt` and the site's terms of use, keep
|
scraping/crawler.py
CHANGED
|
@@ -23,9 +23,14 @@ Usage
|
|
| 23 |
pip install mechanicalsoup beautifulsoup4 lxml python-dateutil fake-useragent
|
| 24 |
python crawler.py
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
Please crawl responsibly: honour robots.txt and the site's terms of use, and
|
| 31 |
lower the worker count unless you have the operator's permission.
|
|
@@ -46,49 +51,81 @@ from dateutil.parser import parse
|
|
| 46 |
|
| 47 |
base_url = "https://www.freitag.de/"
|
| 48 |
|
| 49 |
-
# ---
|
| 50 |
-
#
|
| 51 |
-
#
|
| 52 |
-
#
|
| 53 |
#
|
| 54 |
-
#
|
| 55 |
-
# proxies to send the crawl through your own pool. Entries may be written with
|
| 56 |
-
# or without a scheme ("http://" is assumed when none is given). Each request
|
| 57 |
-
# picks one entry from the pool, so several entries spread the load over
|
| 58 |
-
# several exits. Leave the variable unset or empty to connect directly - that
|
| 59 |
-
# is the default and what most of the crawls in this collection used.
|
| 60 |
#
|
| 61 |
-
#
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def _load_proxy_pool():
|
| 80 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
pool = []
|
| 82 |
-
for entry in
|
| 83 |
entry = entry.strip()
|
| 84 |
if not entry:
|
| 85 |
continue
|
| 86 |
if "://" not in entry:
|
| 87 |
entry = "http://" + entry
|
| 88 |
-
|
|
|
|
| 89 |
return pool
|
| 90 |
|
|
|
|
| 91 |
PROXY_POOL = _load_proxy_pool()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
def log(number, url):
|
| 94 |
if not os.path.isdir("log"):
|
|
@@ -247,7 +284,7 @@ def crawler(iId, iNumberOfThreads, lQueues):
|
|
| 247 |
(not ".epub" in sPage):
|
| 248 |
|
| 249 |
log(iId, sPage)
|
| 250 |
-
for i in range(
|
| 251 |
try:
|
| 252 |
lproxies = PROXY_POOL
|
| 253 |
proxy = lproxies[int((int(time.time())/10))%len(lproxies)] if lproxies else None
|
|
@@ -255,7 +292,7 @@ def crawler(iId, iNumberOfThreads, lQueues):
|
|
| 255 |
browser = mechanicalsoup.StatefulBrowser()
|
| 256 |
browser.session.proxies = proxies
|
| 257 |
|
| 258 |
-
browser.open(sPage, timeout=
|
| 259 |
page = browser.get_current_page()
|
| 260 |
page_str = str(page)
|
| 261 |
if "#challenge-error-text" in page_str:
|
|
@@ -300,7 +337,7 @@ def main():
|
|
| 300 |
if not os.path.isdir("pages"):
|
| 301 |
os.mkdir("pages")
|
| 302 |
|
| 303 |
-
iNumberOfThreads =
|
| 304 |
lQueues = []
|
| 305 |
mgr = multiprocessing.Manager()
|
| 306 |
for i in range(iNumberOfThreads):
|
|
@@ -310,7 +347,7 @@ def main():
|
|
| 310 |
lIds = range(0, iNumberOfThreads)
|
| 311 |
lNumberOfThreads = [iNumberOfThreads] * iNumberOfThreads
|
| 312 |
llQueues = [lQueues] * iNumberOfThreads
|
| 313 |
-
addStartChannel(
|
| 314 |
lArguments = list(zip(lIds, lNumberOfThreads, llQueues))
|
| 315 |
with multiprocessing.Pool(processes=iNumberOfThreads) as pool:
|
| 316 |
pool.starmap(crawler, lArguments)
|
|
|
|
| 23 |
pip install mechanicalsoup beautifulsoup4 lxml python-dateutil fake-useragent
|
| 24 |
python crawler.py
|
| 25 |
|
| 26 |
+
Configuration
|
| 27 |
+
-------------
|
| 28 |
+
Copy ``.env.example`` to ``.env`` and uncomment what you need - proxies, worker
|
| 29 |
+
count, timeout, retries and the seed URL are all read from there (environment
|
| 30 |
+
variables still win over the file). For a large proxy farm put one proxy per
|
| 31 |
+
line into a text file and point ``SCRAPER_PROXY_FILE`` at it; see
|
| 32 |
+
``proxies.example.txt``. The private proxy addresses used in the original crawl
|
| 33 |
+
have been removed from this published copy.
|
| 34 |
|
| 35 |
Please crawl responsibly: honour robots.txt and the site's terms of use, and
|
| 36 |
lower the worker count unless you have the operator's permission.
|
|
|
|
| 51 |
|
| 52 |
base_url = "https://www.freitag.de/"
|
| 53 |
|
| 54 |
+
# --- Configuration ---------------------------------------------------------
|
| 55 |
+
# Every setting below can be supplied through the environment or through a
|
| 56 |
+
# ".env" file placed next to this script. Copy ".env.example" to ".env" and
|
| 57 |
+
# uncomment what you need:
|
| 58 |
#
|
| 59 |
+
# cp .env.example .env
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
#
|
| 61 |
+
# Nothing has to be set - the defaults are exactly the values that were used
|
| 62 |
+
# for the original crawl.
|
| 63 |
+
def _load_dotenv(path=".env"):
|
| 64 |
+
"""Read a ".env" file: KEY=VALUE per line, "#" starts a comment.
|
| 65 |
+
|
| 66 |
+
Variables that are already set in the real environment win, so a single run
|
| 67 |
+
can still be overridden with e.g. ``SCRAPER_THREADS=4 python crawler.py``.
|
| 68 |
+
"""
|
| 69 |
+
if not os.path.isfile(path):
|
| 70 |
+
return
|
| 71 |
+
with io.open(path, "r", encoding="utf-8") as handle:
|
| 72 |
+
for line in handle:
|
| 73 |
+
line = line.strip()
|
| 74 |
+
if not line or line.startswith("#") or "=" not in line:
|
| 75 |
+
continue
|
| 76 |
+
key, _, value = line.partition("=")
|
| 77 |
+
key = key.strip()
|
| 78 |
+
value = value.strip().strip('"').strip("'")
|
| 79 |
+
if key and key not in os.environ:
|
| 80 |
+
os.environ[key] = value
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
_load_dotenv()
|
| 84 |
+
|
| 85 |
+
|
| 86 |
def _load_proxy_pool():
|
| 87 |
+
"""Build the proxy pool from SCRAPER_PROXIES and/or SCRAPER_PROXY_FILE.
|
| 88 |
+
|
| 89 |
+
SCRAPER_PROXIES inline list; entries separated by commas, semicolons,
|
| 90 |
+
spaces or newlines.
|
| 91 |
+
SCRAPER_PROXY_FILE path to a file holding one proxy per line, with blank
|
| 92 |
+
lines and "#" comments allowed. This is the practical
|
| 93 |
+
form for a whole proxy farm: the pool can be swapped or
|
| 94 |
+
regenerated without touching the script or the .env.
|
| 95 |
+
|
| 96 |
+
Both may be combined. Entries may be written with or without a scheme
|
| 97 |
+
("http://" is assumed when none is given). Duplicates are dropped and an
|
| 98 |
+
empty pool simply means "connect directly".
|
| 99 |
+
"""
|
| 100 |
+
raw = os.environ.get("SCRAPER_PROXIES", "")
|
| 101 |
+
proxy_file = os.environ.get("SCRAPER_PROXY_FILE", "").strip()
|
| 102 |
+
if proxy_file:
|
| 103 |
+
if not os.path.isfile(proxy_file):
|
| 104 |
+
raise SystemExit("SCRAPER_PROXY_FILE points to a missing file: " + proxy_file)
|
| 105 |
+
with io.open(proxy_file, "r", encoding="utf-8") as handle:
|
| 106 |
+
raw = raw + "\n" + handle.read()
|
| 107 |
+
cleaned = []
|
| 108 |
+
for line in raw.splitlines():
|
| 109 |
+
line = line.split("#", 1)[0].strip()
|
| 110 |
+
if line:
|
| 111 |
+
cleaned.append(line)
|
| 112 |
pool = []
|
| 113 |
+
for entry in re.split(r"[,;\s]+", " ".join(cleaned)):
|
| 114 |
entry = entry.strip()
|
| 115 |
if not entry:
|
| 116 |
continue
|
| 117 |
if "://" not in entry:
|
| 118 |
entry = "http://" + entry
|
| 119 |
+
if entry not in pool:
|
| 120 |
+
pool.append(entry)
|
| 121 |
return pool
|
| 122 |
|
| 123 |
+
|
| 124 |
PROXY_POOL = _load_proxy_pool()
|
| 125 |
+
NUM_THREADS = int(os.environ.get("SCRAPER_THREADS", "32"))
|
| 126 |
+
REQUEST_TIMEOUT = float(os.environ.get("SCRAPER_TIMEOUT", "10.0"))
|
| 127 |
+
MAX_RETRIES = int(os.environ.get("SCRAPER_RETRIES", "10"))
|
| 128 |
+
START_URL = os.environ.get("SCRAPER_START_URL", "https://www.freitag.de/autoren/franz-schandl/asylpolitik-in-oesterreich-drangsalieren-mehr-faellt-ihnen-nicht-ein")
|
| 129 |
|
| 130 |
def log(number, url):
|
| 131 |
if not os.path.isdir("log"):
|
|
|
|
| 284 |
(not ".epub" in sPage):
|
| 285 |
|
| 286 |
log(iId, sPage)
|
| 287 |
+
for i in range(MAX_RETRIES):
|
| 288 |
try:
|
| 289 |
lproxies = PROXY_POOL
|
| 290 |
proxy = lproxies[int((int(time.time())/10))%len(lproxies)] if lproxies else None
|
|
|
|
| 292 |
browser = mechanicalsoup.StatefulBrowser()
|
| 293 |
browser.session.proxies = proxies
|
| 294 |
|
| 295 |
+
browser.open(sPage, timeout=REQUEST_TIMEOUT)
|
| 296 |
page = browser.get_current_page()
|
| 297 |
page_str = str(page)
|
| 298 |
if "#challenge-error-text" in page_str:
|
|
|
|
| 337 |
if not os.path.isdir("pages"):
|
| 338 |
os.mkdir("pages")
|
| 339 |
|
| 340 |
+
iNumberOfThreads = NUM_THREADS
|
| 341 |
lQueues = []
|
| 342 |
mgr = multiprocessing.Manager()
|
| 343 |
for i in range(iNumberOfThreads):
|
|
|
|
| 347 |
lIds = range(0, iNumberOfThreads)
|
| 348 |
lNumberOfThreads = [iNumberOfThreads] * iNumberOfThreads
|
| 349 |
llQueues = [lQueues] * iNumberOfThreads
|
| 350 |
+
addStartChannel(START_URL, iNumberOfThreads, lQueues)
|
| 351 |
lArguments = list(zip(lIds, lNumberOfThreads, llQueues))
|
| 352 |
with multiprocessing.Pool(processes=iNumberOfThreads) as pool:
|
| 353 |
pool.starmap(crawler, lArguments)
|
scraping/proxies.example.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# One proxy per line. Blank lines and "#" comments are ignored.
|
| 2 |
+
# A scheme is optional - "http://" is assumed when none is given.
|
| 3 |
+
#
|
| 4 |
+
# Copy this file (e.g. to `proxies.txt`), fill in your own entries and point
|
| 5 |
+
# SCRAPER_PROXY_FILE at it:
|
| 6 |
+
#
|
| 7 |
+
# SCRAPER_PROXY_FILE=proxies.txt
|
| 8 |
+
#
|
| 9 |
+
# This is the convenient form for a large proxy farm - the file can hold
|
| 10 |
+
# thousands of lines and be regenerated by another tool at any time.
|
| 11 |
+
|
| 12 |
+
proxy-a.example.com:3128
|
| 13 |
+
proxy-b.example.com:3128
|
| 14 |
+
http://user:pass@proxy-c.example.com:3128
|
| 15 |
+
socks5://localhost:9050
|