Repository navigation
Expand file tree
/
Copy pathcrawl.py
More file actions
executable file
·57 lines (49 loc) · 1.81 KB
/
Copy pathcrawl.py
File metadata and controls
executable file
·57 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys
from scrapy.crawler import CrawlerProcess
from plog import logger
from spiders.base import BaseSpider
process = CrawlerProcess(
settings={
"ITEM_PIPELINES": {"pipelines.PostgresPipeline": 300},
# Add browser-like headers
"DEFAULT_REQUEST_HEADERS": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
# Add reasonable delays between requests
"DOWNLOAD_DELAY": 2,
"RANDOMIZE_DOWNLOAD_DELAY": True,
# Respect robots.txt but with fallback
"ROBOTSTXT_OBEY": False,
# Retry on common errors
"RETRY_ENABLED": True,
"RETRY_TIMES": 3,
"RETRY_HTTP_CODES": [500, 502, 503, 504, 400, 403, 404, 408],
}
)
def run(spider_name=None):
if spider_name:
# Find the spider class with matching name
spider_class = next(
(cls for cls in BaseSpider.__subclasses__() if cls.name == spider_name),
None
)
if spider_class:
process.crawl(spider_class)
else:
logger.error(f"Spider '{spider_name}' not found")
return
else:
# Run all spiders if no specific spider is specified
for crawler_class in BaseSpider.__subclasses__():
process.crawl(crawler_class)
process.start()
if __name__ == "__main__":
logger.info("Starting crawlers...")
# Get spider name from command line argument if provided
spider_name = sys.argv[1] if len(sys.argv) > 1 else None
if spider_name:
logger.info(f"Running spider: {spider_name}")
run(spider_name)
logger.info("Done!")