爬虫

爬虫   创建项目    :scrapy startproject Myspider(项目名)

     创建爬虫     :scrapy genspider baidu (爬虫名) "域名"

     运行爬虫      : scrapy crawl  爬虫

    allowed_domains = ['tencent.com/']   这是错误的写法, 这样会导致不能翻页等一些问题, 应该前后都不能加/ 或者http之类的   

    start_urls = ['https://hr.tencent.com/position.php']   这是一个列表

   提取数据  :

        name = node.xpath('./h3/text()').extract()[0]    这个可能会出现角标错误
        name = node.xpath('./h3/text()').extract_first()  这个即使拿不到数据 也只会返回None           item['name'] = name    item是类,和字典类型相似,保存数据用字典方法

    爬虫返回数据用 yield  ,管道放回数据用return 

  翻页 用  yield scrapy.Request(next_url, callback=self.parse)  调用 callback 翻页解析

meta 传参 重点,就是把列表页的数据传递到详情页    yield scrapy.Request(item['detail_link'],callback=self.parse_detail,meta={"k":item})  然后在回调函数中接受数据  item = respose.meta['k']  这样就传递进来

crawlspider类 创建  用 scrapy genspider -t crawl baidu (爬虫名) "域名"  

  Rule(LinkExtractor(allow=r'questionType'), callback='parse_item', follow=True),  理解各个参数的意思, follow =True 用于翻页,等等, allow 用于正则提取连接

用Flask+Redis维护Cookies池

  

为什什么要⽤用Cookies池?
  ⽹网站需要登录才可爬取,例例如新浪微博,爬取过程中如果频率过⾼高会导致封号, 需要维护多个账号的Cookies池实现⼤大规模爬取

原文地址:https://www.cnblogs.com/ithairy/p/9187591.html