Scrapy 爬取保险条款 -《狗嗨默示录》-

本文介绍了一个使用Scrapy框架实现的爬虫项目案例,通过定义爬虫类和爬虫项来抓取公司、产品类型及条款等信息。该爬虫会依次访问不同页面并解析所需数据。

items.py

class IachinaItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    COMPANY = scrapy.Field()
    TYPE = scrapy.Field()
    PRODUCT = scrapy.Field()
    CLAUSE = scrapy.Field()
    CLAUSE_URL = scrapy.Field()

 

iachina.py

# -*- coding: utf-8 -*-
import scrapy
from IAChina.items import IachinaItem

class IachinaSpider(scrapy.Spider):
    name = 'iachina'
    allowed_domains = ['old.iachina.cn']
    start_urls = ['http://old.iachina.cn/product.php?action=company&ttype=2&page={}'.format(i) for i in range(1,4)]

    def parse(self, response):
        if not response:
            self.log("Company Page error -- %s"%response.url)
        for sel in response.xpath('//div[@class="prolist"]/ul/li/a'):
            item = IachinaItem()
            item['COMPANY'] = sel.xpath('text()').extract()
            company_href = sel.xpath('@href').extract_first()
            company_url = response.urljoin(company_href)
            yield scrapy.Request(url=company_url,meta={'item':item},callback=self.parse_type)

    def parse_type(self,response):
        if not response:
            self.log("Type Page erroe -- %s"%response.url)
        for sel in response.xpath('//div[@class="prolist"]/ul/li/a'):
            item = response.meta['item']
            item['TYPE'] = sel.xpath('text()').extract()
            type_href = sel.xpath('@href').extract_first()
            type_url = response.urljoin(type_href)
            yield scrapy.Request(url=type_url, meta={'item': item}, callback=self.parse_product)

    def parse_product(self,response):
        if not response:
            self.log("Product Page erroe -- %s"%response.url)
        for sel in response.xpath('//div[@class="prolist"]/ul/li/a'):
            item = response.meta['item']
            item['PRODUCT'] = sel.xpath('text()').extract()
            product_href = sel.xpath('@href').extract_first()
            product_url = response.urljoin(product_href)
            yield scrapy.Request(url=product_url, meta={'item': item}, callback=self.parse_clause)

    def parse_clause(self,response):
        if not response:
            self.log("Clause Page erroe -- %s"%response.url)
        for sel in response.xpath('//div[@class="prolist"]/table/tr[2]/td/a'):
            item = response.meta['item']
            item['CLAUSE'] = sel.xpath('text()').extract()
            clause_href = sel.xpath('@href').extract_first()
            item['CLAUSE_URL'] = response.urljoin(clause_href)
            yield item

 

转载于:https://www.cnblogs.com/LiGoHi/p/7400130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值