36.scrapy框架采集全球玻璃网数据
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框架采集全球玻璃网数据的更多相关文章
- 爬虫入门(四)——Scrapy框架入门:使用Scrapy框架爬取全书网小说数据
		
为了入门scrapy框架,昨天写了一个爬取静态小说网站的小程序 下面我们尝试爬取全书网中网游动漫类小说的书籍信息. 一.准备阶段 明确一下爬虫页面分析的思路: 对于书籍列表页:我们需要知道打开单本书籍 ...
 - scrapy框架基于CrawlSpider的全站数据爬取
		
引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...
 - 使用scrapy框架爬取全书网书籍信息。
		
爬取的内容:书籍名称,作者名称,书籍简介,全书网5041页,写入mysql数据库和.txt文件 1,创建scrapy项目 scrapy startproject numberone 2,创建爬虫主程序 ...
 - Scrapy框架——使用CrawlSpider爬取数据
		
引言 本篇介绍Crawlspider,相比于Spider,Crawlspider更适用于批量爬取网页 Crawlspider Crawlspider适用于对网站爬取批量网页,相对比Spider类,Cr ...
 - 利用python scrapy 框架抓取豆瓣小组数据
		
因为最近在找房子在豆瓣小组-上海租房上找,发现搜索困难,于是想利用爬虫将数据抓取. 顺便熟悉一下Python. 这边有scrapy 入门教程出处:http://www.cnblogs.com/txw1 ...
 - 移动端数据爬取和Scrapy框架
		
移动端数据爬取 注:抓包工具:青花瓷 1.配置fiddler 2.移动端安装fiddler证书 3.配置手机的网络 - 给手机设置一个代理IP:port a. Fiddler设置 打开Fiddler软 ...
 - 使用scrapy框架做赶集网爬虫
		
使用scrapy框架做赶集网爬虫 一.安装 首先scrapy的安装之前需要安装这个模块:wheel.lxml.Twisted.pywin32,最后在安装scrapy pip install wheel ...
 - 使用scrapy框架爬取图片网全站图片(二十多万张),并打包成exe可执行文件
		
目标网站:https://www.mn52.com/ 本文代码已上传至git和百度网盘,链接分享在文末 网站概览 目标,使用scrapy框架抓取全部图片并分类保存到本地. 1.创建scrapy项目 s ...
 - Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
		
1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...
 
随机推荐
- MySQL5.7(5.6)GTID环境下恢复从库思路方法(转发)
			
要讨论如何恢复从库,我们得先来了解如下一些概念: GTID_EXECUTED:它是一组包含已经记录在二进制日志文件中的事务集合 GTID_PURGED:它是一组包含已经从二进制日志删除掉的事务集合. ...
 - 并查集(Union-Find)
			
常见问题: 首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的.最后要解决的是整幅图的连通性问题.比如随意给你两个点,让你判断它们是否连通,或者问你整幅图一共 ...
 - Java 可执行jar的manifest编写
			
Eclipse:形式, 选中项目右键 命令行形式: 1.编写Java类 2.命令行指定到项目/src文件夹,编译 3.编写manifest文件 4.目录重新定位到bin/classes编译文件目录下, ...
 - Scrapy环境安装
			
开始安装前,建议安装Visual C++ 2015 Build Tools,否则会一直出现如下提示: 下载地址:http://landinghub.visualstudio.com/visual-cp ...
 - T-SQL目录汇总1
			
DDL alter create drop DML select update delete insert DCL grant revoke deny ================= ...
 - ASP.NET重写Render 加载CSS样式文件和JS文件(切换CSS换皮肤)
			
网页换皮肤的方式有很多种,最简单的通常就是切换页面CSS,而CSS通常写在外部CSS文件里.那么切换CSS其实就是更换html里的link href路径.我在网上搜索了下. 一般有两种方式: 1.页面 ...
 - js实现星空效果
			
本次主要是来实现上面的星空效果:在黑色的背景下面,有大小不一的星星在闪烁,当鼠标悬浮时,星星放大并旋转. 首先,我们需要一个大的夜空容器和放星星的容器: <!DOCTYPE html> & ...
 - Hive 的基本概念
			
Hadoop开发存在的问题 只能用java语言开发,如果是c语言或其他语言的程序员用Hadoop,存在语言门槛. 需要对Hadoop底层原理,api比较了解才能做开发. Hive概述 Hive是基于H ...
 - flume 1.7在windows下的安装与运行
			
flume 1.7在windows下的安装与运行 一.安装 安装java,配置环境变量. 安装flume,flume的官网http://flume.apache.org/,下载地址,下载后直接解压即可 ...
 - [UE4]正交
			
一.如果两条直线是垂直的,那么就可以说这2条直线是正交的.既然有垂直,为什么还要有正交概念呢? 因为正交可以描述两个或者多个变量之间互不影响.互不干涉,而垂直是完全属于几何的术语. 二.具有正交关系的 ...