博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
36.scrapy框架采集全球玻璃网数据
阅读量:7225 次
发布时间:2019-06-29

本文共 12918 字,大约阅读时间需要 43 分钟。

1. 采集目标地址 https://www.glass.cn/gongying/sellindex.aspx  网站比较简单,没什么大的需要注意的问题。 2. 通过分析测试 https://www.glass.cn/gongying/a_l_p1_ky/ 等价于目标采集网站首页,只需设置{}.format 翻页 这个完整比较简单,就是获取一下页码,再做一下翻页,循环采集页面跳转url,再进入url采集页面内容信息。 3. 采集数据过程及结果

 

 

 

#glass_gy.py# -*- coding: utf-8 -*-import scrapyimport refrom glass_gy_web.items import GlassGyWebItemclass GlassGySpider(scrapy.Spider):    name = 'glass_gy'    allowed_domains = ['www.glass.cn']    start_urls = ['https://www.glass.cn/gongying/a_l_p1_ky/']    custom_settings = {        'DOWNLOAD_DELAY': 0.5,        "ITEM_PIPELINES": {            'glass_gy_web.pipelines.MysqlPipeline': 300,        },        "DOWNLOADER_MIDDLEWARES": {            'glass_gy_web.middlewares.GlassGyWebDownloaderMiddleware': 500,        },    }    def parse(self, response):        link_urls = response.xpath("//div[@class='brandinfotxt z']/h4[@class='brandHTit clearfix']/a/@href").extract()        for link_url in link_urls:            # print(link_url)            yield scrapy.Request(url=link_url, callback=self.parse_detail)        # print('*'*100)        # 翻页        pg_num = re.findall('共(.*?)页',response.text)        # print(pg_num[0])        for i in range(2, int(pg_num[0])+1):            url = 'https://www.glass.cn/gongying/a_l_p{}_ky/'.format(i)            yield scrapy.Request(url=url, callback=self.parse)    def parse_detail(self, response):        item=GlassGyWebItem()        # 产品名称        p_name = response.xpath("//div[@class='ProductDel']/h1/text()").extract_first()        item['p_name'] = p_name        # 数量        num = response.xpath("//table//tr/td[@class='bot'][1]/text()").extract_first()        item['num'] = num        # 价格        price = response.xpath("//table//tr/td[@class='bot'][2]/text()").extract_first()        item['price'] = price        # 供货总量        total_supply = re.findall('
  • 供货总量:(.*?)
  • ',response.text) item['total_supply'] = total_supply[0] # 最小起订 _order = re.findall('
  • 最小起订: (.*?)
  • ',response.text) item['_order'] = _order[0] # 发货地址 ship_add = re.findall('
  • 发货地址: (.*?)
  • ',response.text) item['ship_add'] = ship_add[0] # 发货期限 dispatch = re.findall('
  • 发货期限: (.*?)
  • ',response.text) item['dispatch'] = dispatch[0] # 物流运费 fare = re.findall('
  • 物流运费: (.*?)
  • ',response.text) item['fare'] = fare[0] # 发布日期 release_date = re.findall('
  • 发布日期:(.*?)
  • ',response.text) item['release_date'] = release_date[0] # 采购电话 purchase = re.findall('
    采购电话:(.*?)
    ',response.text) item['purchase'] = purchase[0] # 产品详情 content = response.xpath("//div[@id='pdetail']/text()").extract() item['content'] = content[-1].strip() # 公司名称 company = response.xpath("//div[@class='zcbw']/h2/a/text()").extract_first() item['company'] = company # 联系人 linkman = re.findall('联系人:(.*?)',response.text) item['linkman'] = linkman[0] # 手机 phone = re.findall('
  • 手机:(.*?)
  • ',response.text) item['phone'] = phone[0] # 电话 mobile = re.findall('
  • 电话:(.*?)
  • ',response.text) item['mobile'] = mobile[0] # 营业执照 try: licence = re.findall('
  • 营业执照:(.*?)
    经营模式: (.*?)所在地区:(.*?)
  • ',response.text) item['dist'] = dist[0] # 产品数量 quantity = re.findall('
  • 产品数量:(.*?)
  • ',response.text) item['quantity'] = quantity[0] # 玻璃金币 gold = re.findall('
  • 玻璃金币:(.*?)
  • ',response.text) item['gold'] = gold[0] yield item print('*'*100)
    # items.py# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# https://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass GlassGyWebItem(scrapy.Item):    # define the fields for your item here like:    # name = scrapy.Field()    # 产品名称    p_name = scrapy.Field()    # 数量    num = scrapy.Field()    # 价格    price = scrapy.Field()    # 供货总量    total_supply = scrapy.Field()    # 最小起订    _order = scrapy.Field()    # 发货地址    ship_add = scrapy.Field()    # 发货期限    dispatch = scrapy.Field()    # 物流运费    fare = scrapy.Field()    # 发布日期    release_date = scrapy.Field()    # 采购电话    purchase = scrapy.Field()    # 产品详情    content = scrapy.Field()    # 公司名称    company = scrapy.Field()    # 联系人    linkman = scrapy.Field()    # 手机    phone = scrapy.Field()    # 电话    mobile = scrapy.Field()    # 营业执照    licence = scrapy.Field()    # 经营模式    model = scrapy.Field()    # 所在地区    dist = scrapy.Field()    # 产品数量    quantity = scrapy.Field()    # 玻璃金币    gold = scrapy.Field()
    # piplines.py# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.htmlfrom scrapy.conf import settingsimport pymysqlclass GlassGyWebPipeline(object):    def process_item(self, item, spider):        return item# 数据保存mysqlclass MysqlPipeline(object):    def open_spider(self, spider):        self.host = settings.get('MYSQL_HOST')        self.port = settings.get('MYSQL_PORT')        self.user = settings.get('MYSQL_USER')        self.password = settings.get('MYSQL_PASSWORD')        self.db = settings.get(('MYSQL_DB'))        self.table = settings.get('TABLE')        self.client = pymysql.connect(host=self.host, user=self.user, password=self.password, port=self.port, db=self.db, charset='utf8')    def process_item(self, item, spider):        item_dict = dict(item)        cursor = self.client.cursor()        values = ','.join(['%s'] * len(item_dict))        keys = ','.join(item_dict.keys())        sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=self.table, keys=keys, values=values)        try:            if cursor.execute(sql, tuple(item_dict.values())):  # 第一个值为sql语句第二个为 值 为一个元组                print('数据入库成功!')                self.client.commit()        except Exception as e:            print(e)            print('数据已存在!')            self.client.rollback()        return item    def close_spider(self, spider):        self.client.close()
    #settings.py# -*- coding: utf-8 -*-# Scrapy settings for glass_gy_web project## For simplicity, this file contains only settings considered important or# commonly used. You can find more settings consulting the documentation:##     https://doc.scrapy.org/en/latest/topics/settings.html#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#     https://doc.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME = 'glass_gy_web'SPIDER_MODULES = ['glass_gy_web.spiders']NEWSPIDER_MODULE = 'glass_gy_web.spiders'# Crawl responsibly by identifying yourself (and your website) on the user-agent#USER_AGENT = 'glass_gy_web (+http://www.yourdomain.com)'# Obey robots.txt rulesROBOTSTXT_OBEY = False# mysql配置参数MYSQL_HOST = "172.16.10.157"MYSQL_PORT = 3306MYSQL_USER = "root"MYSQL_PASSWORD = "123456"MYSQL_DB = 'web_datas'TABLE = "glass_gy"# Configure maximum concurrent requests performed by Scrapy (default: 16)#CONCURRENT_REQUESTS = 32# Configure a delay for requests for the same website (default: 0)# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay# See also autothrottle settings and docs#DOWNLOAD_DELAY = 3# The download delay setting will honor only one of:#CONCURRENT_REQUESTS_PER_DOMAIN = 16#CONCURRENT_REQUESTS_PER_IP = 16# Disable cookies (enabled by default)#COOKIES_ENABLED = False# Disable Telnet Console (enabled by default)#TELNETCONSOLE_ENABLED = False# Override the default request headers:#DEFAULT_REQUEST_HEADERS = {
    # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',# 'Accept-Language': 'en',#}# Enable or disable spider middlewares# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html#SPIDER_MIDDLEWARES = {
    # 'glass_gy_web.middlewares.GlassGyWebSpiderMiddleware': 543,#}# Enable or disable downloader middlewares# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.htmlDOWNLOADER_MIDDLEWARES = { 'glass_gy_web.middlewares.GlassGyWebDownloaderMiddleware': 543,}# Enable or disable extensions# See https://doc.scrapy.org/en/latest/topics/extensions.html#EXTENSIONS = {
    # 'scrapy.extensions.telnet.TelnetConsole': None,#}# Configure item pipelines# See https://doc.scrapy.org/en/latest/topics/item-pipeline.htmlITEM_PIPELINES = { 'glass_gy_web.pipelines.GlassGyWebPipeline': 300,}# Enable and configure the AutoThrottle extension (disabled by default)# See https://doc.scrapy.org/en/latest/topics/autothrottle.html#AUTOTHROTTLE_ENABLED = True# The initial download delay#AUTOTHROTTLE_START_DELAY = 5# The maximum download delay to be set in case of high latencies#AUTOTHROTTLE_MAX_DELAY = 60# The average number of requests Scrapy should be sending in parallel to# each remote server#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0# Enable showing throttling stats for every response received:#AUTOTHROTTLE_DEBUG = False# Enable and configure HTTP caching (disabled by default)# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings#HTTPCACHE_ENABLED = True#HTTPCACHE_EXPIRATION_SECS = 0#HTTPCACHE_DIR = 'httpcache'#HTTPCACHE_IGNORE_HTTP_CODES = []#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
    #middlewares.py# -*- coding: utf-8 -*-# Define here the models for your spider middleware## See documentation in:# https://doc.scrapy.org/en/latest/topics/spider-middleware.htmlfrom scrapy import signalsclass GlassGyWebSpiderMiddleware(object):    # Not all methods need to be defined. If a method is not defined,    # scrapy acts as if the spider middleware does not modify the    # passed objects.    @classmethod    def from_crawler(cls, crawler):        # This method is used by Scrapy to create your spiders.        s = cls()        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)        return s    def process_spider_input(self, response, spider):        # Called for each response that goes through the spider        # middleware and into the spider.        # Should return None or raise an exception.        return None    def process_spider_output(self, response, result, spider):        # Called with the results returned from the Spider, after        # it has processed the response.        # Must return an iterable of Request, dict or Item objects.        for i in result:            yield i    def process_spider_exception(self, response, exception, spider):        # Called when a spider or process_spider_input() method        # (from other spider middleware) raises an exception.        # Should return either None or an iterable of Response, dict        # or Item objects.        pass    def process_start_requests(self, start_requests, spider):        # Called with the start requests of the spider, and works        # similarly to the process_spider_output() method, except        # that it doesn’t have a response associated.        # Must return only requests (not items).        for r in start_requests:            yield r    def spider_opened(self, spider):        spider.logger.info('Spider opened: %s' % spider.name)class GlassGyWebDownloaderMiddleware(object):    # Not all methods need to be defined. If a method is not defined,    # scrapy acts as if the downloader middleware does not modify the    # passed objects.    @classmethod    def from_crawler(cls, crawler):        # This method is used by Scrapy to create your spiders.        s = cls()        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)        return s    def process_request(self, request, spider):        # Called for each request that goes through the downloader        # middleware.        # Must either:        # - return None: continue processing this request        # - or return a Response object        # - or return a Request object        # - or raise IgnoreRequest: process_exception() methods of        #   installed downloader middleware will be called        return None    def process_response(self, request, response, spider):        # Called with the response returned from the downloader.        # Must either;        # - return a Response object        # - return a Request object        # - or raise IgnoreRequest        return response    def process_exception(self, request, exception, spider):        # Called when a download handler or a process_request()        # (from other downloader middleware) raises an exception.        # Must either:        # - return None: continue processing this exception        # - return a Response object: stops process_exception() chain        # - return a Request object: stops process_exception() chain        pass    def spider_opened(self, spider):        spider.logger.info('Spider opened: %s' % spider.name)

     

    posted on
    2018-10-18 15:35 阅读(
    ...) 评论(
    ...)

    转载于:https://www.cnblogs.com/lvjing/p/9810840.html

    你可能感兴趣的文章
    终于找到解决方案了,Qt的Model/View Framework解析
    查看>>
    线程信息的获取和设置
    查看>>
    Databricks Scala 编程风格指南
    查看>>
    Tkinter,label内容随多选框变化
    查看>>
    PHP开发中的数据类型 ( 第3篇 ) :Heaps
    查看>>
    网络七层协议
    查看>>
    4种删除Word空白页的小技巧,都是你需要用到的!
    查看>>
    单服务器MySQL主从复制实践
    查看>>
    CentOS 7 root口令恢复
    查看>>
    | 刘知远:让计算机听懂人话
    查看>>
    苹果收购初创公司Tueo Health,哮喘监测或将应用到Apple Watch
    查看>>
    CLR存储过程
    查看>>
    初级运维(一)
    查看>>
    C语言字符串常用函数学习(一)
    查看>>
    Lync Server 2010部署与应用(三)---拓扑生成与发布
    查看>>
    安全摘记1:关于安全与黑客
    查看>>
    我的友情链接
    查看>>
    tbox中vector容器的使用
    查看>>
    一个简单的PHP笔试题
    查看>>
    firebug重新载入页面获取源码
    查看>>