From 3e5e7096bf998bae1587c7816186f60a5274f1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=9B=E9=A3=8E?= Date: Fri, 6 Sep 2019 20:01:24 +0800 Subject: [PATCH] add random --- DoubanSpider/__init__.py | 1 + Spider.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/DoubanSpider/__init__.py b/DoubanSpider/__init__.py index 86c874d..ee0d110 100644 --- a/DoubanSpider/__init__.py +++ b/DoubanSpider/__init__.py @@ -12,6 +12,7 @@ try: import csv import sqlite3 import logging + import random except: print('[System]正在安装支持库...') os.system('pip install SQLAlchemy') diff --git a/Spider.py b/Spider.py index a59c697..d641f08 100644 --- a/Spider.py +++ b/Spider.py @@ -19,7 +19,7 @@ class DoubanBook(object): def get_url(self, tag_name): for num in range(0, 10000, 20): - time.sleep(0.5) + time.sleep(sleeptime) url = self.base_url.format(tag_name) + f'?start={num}&type=T' print(f'正在获取 TAG:<{tag_name}> 书籍信息', num) response = requests.get(url, headers=self.headers) @@ -65,7 +65,7 @@ class DoubanBook(object): def get_data(self): for row in self.session.query(Douban.url, Douban.tag).all(): - time.sleep(0.5) + time.sleep(sleeptime) print(f"正在解析:{row[0]}") response = requests.get(row[0], headers=self.headers) html = response.content.decode() @@ -125,6 +125,7 @@ class DoubanBook(object): if __name__ == '__main__': logger = logging.getLogger("PAPA") + sleeptime = random.randint(0,3) with open("results.csv", "a", encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))