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 scrapy
import re
from glass_gy_web.items import GlassGyWebItem
class 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('<li><span>供货总量:</span>(.*?)</li>',response.text)
item['total_supply'] = total_supply[0]
# 最小起订
_order = re.findall('<li><span>最小起订: </span>(.*?)</li>',response.text)
item['_order'] = _order[0]
# 发货地址
ship_add = re.findall('<li><span>发货地址: </span>(.*?)</li>',response.text)
item['ship_add'] = ship_add[0]
# 发货期限
dispatch = re.findall('<li><span>发货期限: </span>(.*?)</li>',response.text)
item['dispatch'] = dispatch[0]
# 物流运费
fare = re.findall('<li><span>物流运费: </span>(.*?)</li>',response.text)
item['fare'] = fare[0]
# 发布日期
release_date = re.findall('<li><span>发布日期:</span>(.*?)</li>',response.text)
item['release_date'] = release_date[0]
# 采购电话
purchase = re.findall('<div class="sell_tel clearfix"><span>采购电话:</span>(.*?)</div>',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('<span>联系人:(.*?)</span>',response.text)
item['linkman'] = linkman[0]
# 手机
phone = re.findall('<li>手机:(.*?)</li>',response.text)
item['phone'] = phone[0]
# 电话
mobile = re.findall('<li>电话:(.*?)</li>',response.text)
item['mobile'] = mobile[0]
# 营业执照
try:
licence = re.findall('<li>营业执照:(.*?) <img',response.text)
licence = licence[0]
except:
licence = ""
item['licence'] = licence
# 经营模式
try:
model = re.findall('<li>经营模式: (.*?)</li',response.text)
model = model[0]
except:
model = ""
item['model'] = model
# 所在地区
dist = re.findall('<li>所在地区:(.*?)</li>',response.text)
item['dist'] = dist[0]
# 产品数量
quantity = re.findall('<li>产品数量:(.*?)</li>',response.text)
item['quantity'] = quantity[0]
# 玻璃金币
gold = re.findall('<li>玻璃金币:(.*?)</li>',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.html import scrapy class 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.html
from scrapy.conf import settings
import pymysql class GlassGyWebPipeline(object):
def process_item(self, item, spider):
return item # 数据保存mysql
class 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.html BOT_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 rules
ROBOTSTXT_OBEY = False # mysql配置参数
MYSQL_HOST = "172.16.10.157"
MYSQL_PORT = 3306
MYSQL_USER = "root"
MYSQL_PASSWORD = ""
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.html
DOWNLOADER_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.html
ITEM_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.html from scrapy import signals class 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)

36.scrapy框架采集全球玻璃网数据的更多相关文章

  1. 爬虫入门(四)——Scrapy框架入门:使用Scrapy框架爬取全书网小说数据

    为了入门scrapy框架,昨天写了一个爬取静态小说网站的小程序 下面我们尝试爬取全书网中网游动漫类小说的书籍信息. 一.准备阶段 明确一下爬虫页面分析的思路: 对于书籍列表页:我们需要知道打开单本书籍 ...

  2. scrapy框架基于CrawlSpider的全站数据爬取

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  3. 使用scrapy框架爬取全书网书籍信息。

    爬取的内容:书籍名称,作者名称,书籍简介,全书网5041页,写入mysql数据库和.txt文件 1,创建scrapy项目 scrapy startproject numberone 2,创建爬虫主程序 ...

  4. Scrapy框架——使用CrawlSpider爬取数据

    引言 本篇介绍Crawlspider,相比于Spider,Crawlspider更适用于批量爬取网页 Crawlspider Crawlspider适用于对网站爬取批量网页,相对比Spider类,Cr ...

  5. 利用python scrapy 框架抓取豆瓣小组数据

    因为最近在找房子在豆瓣小组-上海租房上找,发现搜索困难,于是想利用爬虫将数据抓取. 顺便熟悉一下Python. 这边有scrapy 入门教程出处:http://www.cnblogs.com/txw1 ...

  6. 移动端数据爬取和Scrapy框架

    移动端数据爬取 注:抓包工具:青花瓷 1.配置fiddler 2.移动端安装fiddler证书 3.配置手机的网络 - 给手机设置一个代理IP:port a. Fiddler设置 打开Fiddler软 ...

  7. 使用scrapy框架做赶集网爬虫

    使用scrapy框架做赶集网爬虫 一.安装 首先scrapy的安装之前需要安装这个模块:wheel.lxml.Twisted.pywin32,最后在安装scrapy pip install wheel ...

  8. 使用scrapy框架爬取图片网全站图片(二十多万张),并打包成exe可执行文件

    目标网站:https://www.mn52.com/ 本文代码已上传至git和百度网盘,链接分享在文末 网站概览 目标,使用scrapy框架抓取全部图片并分类保存到本地. 1.创建scrapy项目 s ...

  9. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)

    1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...

随机推荐

  1. SSH实现隧道功能穿墙

    Putty和SSH tunnel 目前寻求FQ的方式无非就几种: 寻找web代理(这个可以进我放置的在线代理进行测试) 自行寻找http/sock5代理(这个可以去网上搜索代理ip) vpnFQ(目前 ...

  2. ALGO-151_蓝桥杯_算法训练_6-2递归求二进制表示位数

    记: 进制转换 AC代码: #include <stdio.h> #define K 2 int main(void) { ; scanf("%d",&n); ...

  3. Lucene 4.0 正式版发布,亮点特性中文解读[转]

    http://blog.csdn.net/accesine960/article/details/8066877 2012年10月12日,Lucene 4.0正式发布了(点击这里下载最新版),这个版本 ...

  4. spring boot学习(8) SpringBoot 之切面AOP

    在方法执行的前后,切入代码:经典的service层切入事务: @Aspect注解是切面注解类 @Pointcut切点定义 @Before是方法执行前调用 @After是方法执行后调用 @AfterRe ...

  5. Flume 安装和配置

    安装步骤 1.安装jdk,1.6版本以上 2.上传flume的安装包 3.解压安装 4.在conf目录下,创建一个配置文件,比如:template.conf(名字可以不固定,后缀也可以不固定) 5.配 ...

  6. file /usr/lib64/mysql/plugin/dialog.so from install of Percona-Server-server-56-5.6.24-rel72.2.el6.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64

    !!!点下面!!! https://www.cnblogs.com/chuijingjing/p/10005922.html

  7. MyBatis 值的传递

    1.值的传递 - Map传值 可以通过对象获取Map传递值,在配置文件中通过 #{} 或 ${} 进行应用 查询30-40岁的用户 <!-- 值的传递 - Map传值 --> <se ...

  8. Java NIO系列教程(五)Buffer

    Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的.交互图如下: 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被 ...

  9. Java-Runoob-高级教程-实例-方法:05. Java 实例 – 阶乘

    ylbtech-Java-Runoob-高级教程-实例-方法:05. Java 实例 – 阶乘 1.返回顶部 1. Java 实例 - 阶乘  Java 实例 一个正整数的阶乘(英语:factoria ...

  10. IntelliJ IDEA2017 激活方法 最新的

    今天打开电脑,非常不幸,idea出问题了!!! 大部分人以前应该都是用的以下方法: 1. 到网站 http://idea.lanyus.com/ 获取注册码 2.填入下面的license server ...