一、创建项目

第一步:scrapy startproject boyuan

第二步:cd boyuan

    scrapy genspider product -t crawl  boyuan.com

如图:

二、代码编写

1、item.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 BoyuanItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
address = scrapy.Field()
company = scrapy.Field()
img = scrapy.Field()
time = scrapy.Field()

2、product.py爬虫文件

# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import Rule, CrawlSpider
from scrapy.linkextractors import LinkExtractor
from ..items import BoyuanItem class ProductSpider(CrawlSpider):
name = 'product'
allowed_domains = ['boyuan.com']
offset = 1
url = "http://www.boyuan.com/sell/?page={0}"
start_urls = [url.format(str(offset))] page_link = LinkExtractor(allow=("\?page=\d+")) rules = [
Rule(page_link, callback="parse_content", follow=True)
] def parse_content(self, response):
for each in response.xpath("//div[@class='list']//tr"):
item = BoyuanItem()
item['name'] = each.xpath("./td[4]//strong/text()").extract()[0]
item['company'] = each.xpath("./td[4]//li[4]/a/text()").extract()[0]
address = each.xpath("./td[4]//li[3]/text()").extract()[0]
item['address'] = str(address).strip("[").strip("]")
time = each.xpath("./td[4]//li[3]/span/text()").extract()[0]
item['time'] = str(time).strip()
item['img'] = each.xpath("./td[2]//img/@original").extract()[0]
yield item

3、pipelines.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
import json
import pymongo
from scrapy.conf import settings class BoyuanPipeline(object): def __init__(self):
host = settings.get("MONGO_HOST")
port = settings.get("MONGO_PORT")
db_name = settings.get("MONGO_DB")
collection = settings.get("MONGO_COLLECTION")
self.client = pymongo.MongoClient(host=host, port=int(port))
db = self.client.get_database(db_name)
if collection not in db.list_collection_names():
db.create_collection(collection)
self.col = db[collection] def process_item(self, item, spider):
# 保存到mongodb中
self.col.insert(dict(item))
return item def close_spider(self, spider):
self.client.close()

3、settings.py 配置文件

# mongodb数据库参数
MONGO_HOST = "localhost"
MONGO_PORT = ""
MONGO_DB = "boyuan"
MONGO_COLLECTION = "product"

4、start.py 启动文件

from scrapy import cmdline

if __name__ == '__main__':
cmdline.execute("scrapy crawl product".split())

采集结果如图:

Scrapy框架 之某网站产品采集案例的更多相关文章

  1. Python 之scrapy框架58同城招聘爬取案例

    一.项目目录结构: 代码如下: # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See docu ...

  2. scrapy框架的解析

    1,scrapy框架的官网:https://scrapy.org/ 什么是scrapy框架: scrapy 是一个为了爬取网站数据,提取结构性数据而编写的应用内框架,非常出名,所谓框架就是一个已经继承 ...

  3. Scrapy框架——CrawlSpider类爬虫案例

    Scrapy--CrawlSpider Scrapy框架中分两类爬虫,Spider类和CrawlSpider类. 此案例采用的是CrawlSpider类实现爬虫. 它是Spider的派生类,Spide ...

  4. Scrapy框架——CrawlSpider爬取某招聘信息网站

    CrawlSpider Scrapy框架中分两类爬虫,Spider类和CrawlSpider类. 它是Spider的派生类,Spider类的设计原则是只爬取start_url列表中的网页, 而Craw ...

  5. 爬虫(十四):Scrapy框架(一) 初识Scrapy、第一个案例

    1. Scrapy框架 Scrapy功能非常强大,爬取效率高,相关扩展组件多,可配置和可扩展程度非常高,它几乎可以应对所有反爬网站,是目前Python中使用最广泛的爬虫框架. 1.1 Scrapy介绍 ...

  6. scrapy框架爬取糗妹妹网站妹子图分类的所有图片

    爬取所有图片,一个页面的图片建一个文件夹.难点,图片中有不少.gif图片,需要重写下载规则, 创建scrapy项目 scrapy startproject qiumeimei 创建爬虫应用 cd qi ...

  7. Python爬虫开发【第1篇】【Scrapy框架】

    Scrapy 框架介绍 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架. Srapy框架,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以 ...

  8. python爬虫scrapy框架

    Scrapy 框架 关注公众号"轻松学编程"了解更多. 一.简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量 ...

  9. selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装

    今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...

随机推荐

  1. canvas.clipPath canvas.clipRect() 无效的原因

    今天发现有些机型不能做到像QQ 透明截图那样的功能,本来能够的.一看是部分机器所有都是灰色半透明遮挡住了,没中间的透明效果,. 并且我不是通过遮挡,我是採用 裁剪的方式,至于裁剪代码百度有相关知识,具 ...

  2. NSSet所有API学习。

    /****************集合(NSSet)和数组(NSArray)有相似之处,都是存储不同的对象的地址.只是NSArray是有序的集合,NSSet是无序的集合,同一时候NSSet能够保证数据 ...

  3. JavaScript实现页面重载 - 535种方式

    location = location ... and a 534 other ways to reload the page with JavaScript location = location ...

  4. Codeforces Round #322 (Div. 2) D. Three Logos 模拟

                                                      D. Three Logos Three companies decided to order a ...

  5. go11---方法method

    package main /* 方法method Go 中虽没有class,但依旧有method 通过显示说明receiver来实现与某个类型的组合 只能为同一个包中的类型定义方法 Receiver ...

  6. Android 修改开机动画(bootanimation)【转】

    本文转载自:http://blog.csdn.net/u012301841/article/details/51598115 Android 系统自带的开机动画,是一个白色的 “android” 文字 ...

  7. ROUND function and arithmetic overflow

    遇到如下错误 Arithmetic overflow error converting expression to data type numeric. ), ); https://stackover ...

  8. 获得了Root权限后Read-only file system

    获得了Root权限后,adb shell进入文件系统,有时仍然不能对系统文件夹进行写操作,典型的如删除/system/app下的Apk, 例如系统报:rm failed for xxx.apk, Re ...

  9. poj 3621(最优比率环)

    Sightseeing Cows Farmer John has decided to reward his cows for their hard work by taking them on a ...

  10. 编程细节 —— 按值传递、按引用传递(final、const)

    System.out,out 是 System 类内定义的静态 final PrinterStream 变量: public final class System { ... public final ...