修改engine,兼容基类
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
@@ -7,5 +9,8 @@ class Downloader:
|
||||
pass
|
||||
|
||||
def download(self,url):
|
||||
response = requests.get(url)
|
||||
print(response)
|
||||
# response = requests.get(url)
|
||||
# print(response)
|
||||
|
||||
time.sleep(0.1)
|
||||
print('aaaa')
|
||||
@@ -1,10 +1,18 @@
|
||||
from typing import Optional,Generator
|
||||
from baid_spider.core.downloader import Downloader
|
||||
|
||||
class Engine:
|
||||
|
||||
def __init__(self):
|
||||
self.downloader = Downloader()
|
||||
self.downloader :Optional[Downloader] = Downloader()
|
||||
self.start_requests: Optional[Generator] = None
|
||||
|
||||
def start_spider(self,spider):
|
||||
start_url = spider.start_requests()
|
||||
self.downloader.download(start_url)
|
||||
self.start_requests = iter(spider.start_requests())
|
||||
self.crawl()
|
||||
#self.downloader.download(start_url)
|
||||
|
||||
def crawl(self):
|
||||
'''主逻辑'''
|
||||
start_request = next(self.start_requests)
|
||||
self.downloader.download(start_request)
|
||||
@@ -8,7 +8,7 @@ class Spider:
|
||||
def start_requests(self):
|
||||
if self.start_urls:
|
||||
for url in self.start_urls:
|
||||
return url
|
||||
yield url
|
||||
else:
|
||||
if hasattr(self,'start_url') and isinstance(getattr(self,'start_url'),str):
|
||||
return getattr(self,'start_url')
|
||||
yield getattr(self,'start_url')
|
||||
@@ -1,7 +1,7 @@
|
||||
from baid_spider.spider import Spider
|
||||
class Baidu_Spider(Spider):
|
||||
|
||||
start_urls = ['https://www.baidu.com']
|
||||
start_urls = ['https://www.baidu.com','https://www.baidu.com']
|
||||
|
||||
# def start_requests(self):
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user