bricks
开发指南
2.2 初始化篇
2.2.1 代码开发

重载 make_seeds 是最简单的定义请求的方式。您可以通过以下方式来定义!

 
from bricks.spider import air  
from bricks.spider.air import Context  
  
  
class MySpider(air.Spider):  
  
    def make_seeds(self, context: Context, **kwargs):  
        # 因为只需要爬这个种子  
        # 所以可以实现 make_seeds 接口之后直接 return 出去即可  
        # 如果有多个, 你有两种方案  
        # 1. 将种子全部放置在一个列表里面, yield 出去, 如 return [{"page":1}, {"page":2}, {"page":3}]        # 2. 使用生成器, 每次生产一部分, 如 yield {"page":1}, yield {"page":2}, yield {"page":3}  
        return {  
            "url": f"https://www.baidu.com/s?ie=utf-8&wd=1",  
            "headers": {  
                'Connection': 'keep-alive',  
                'Pragma': 'no-cache',  
                'Cache-Control': 'no-cache',  
                'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',  
                'sec-ch-ua-mobile': '?0',  
                'DNT': '1',  
                'Upgrade-Insecure-Requests': '1',  
                'User-Agent': 'user_agent.firefox(device="pc")',  
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',  
                'Sec-Fetch-Site': 'none',  
                'Sec-Fetch-Mode': 'navigate',  
                'Sec-Fetch-User': '?1',  
                'Sec-Fetch-Dest': 'document',  
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,pt;q=0.5',  
            }  
        }  
  
  
if __name__ == '__main__':  
    spider = MySpider()  
    spider.run()
 
 

仅定义make_seeds时,你需要return/yield一个字典 / 一个列表嵌套字典的形式作为爬取目标,你返回的值会作为构建Request的必要参数,因此你的字典的 KeyValue 应该与 bricks.Request 的实例化参数保持一致!

如果你需要精简你的种子生成,你也可以重写 make_request 方法,来剥离通用的逻辑,make_seeds 仅需实现返回变量部分即可

 
from bricks import Request  
from bricks.spider import air  
from bricks.spider.air import Context  
  
  
class MySpider(air.Spider):  
  
    def make_seeds(self, context: Context, **kwargs):  
        # 因为只需要爬这个种子  
        # 所以可以实现 make_seeds 接口之后直接 return 出去即可  
        # 如果有多个, 你有两种方案  
        # 1. 将种子全部放置在一个列表里面, yield 出去, 如 return [{"page":1}, {"page":2}, {"page":3}]        # 2. 使用生成器, 每次生产一部分, 如 yield {"page":1}, yield {"page":2}, yield {"page":3}  
        return {  
            "keyword": "1",  
        }  
  
    def make_request(self, context: Context) -> Request:  
        yield Request(  
            url=f"https://www.baidu.com/s?ie=utf-8&wd={context.seeds['keyword']}",  
            headers={  
                'Connection': 'keep-alive',  
                'Pragma': 'no-cache',  
                'Cache-Control': 'no-cache',  
                'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',  
                'sec-ch-ua-mobile': '?0',  
                'DNT': '1',  
                'Upgrade-Insecure-Requests': '1',  
                'User-Agent': 'user_agent.firefox(device="pc")',  
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',  
                'Sec-Fetch-Site': 'none',  
                'Sec-Fetch-Mode': 'navigate',  
                'Sec-Fetch-User': '?1',  
                'Sec-Fetch-Dest': 'document',  
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,pt;q=0.5',  
            }  
        )  
  
if __name__ == '__main__':  
    spider = MySpider()  
    spider.run()
 

可以看到,通过这种方式,我们将请求的关键参数分离成种子和请求了,后续请求一些参数如请求头等需要变更的时候,我们只需要对代码进行改动即可,而不需要从新处理种子,减轻了队列压力,促使改动更加方便!