一、创建项目

第一步: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. Linux VSFTP服务器

    Linux VSFTP服务器 一.Linux FTP服务器分类: <1>wu-ftp <2>proftp=profession ftp <3>vsftp=very ...

  2. linux c 获取当前执行进程总数

    获取当前执行进程总数的命令为: ps auxw | wc -l 获取当前执行进程总数的源代码例如以下: #include <stdio.h> #include <stdlib.h&g ...

  3. linux恢复误删除文件-extundelete

     经过本人測试该工具支持ext3和ext4文件系统 当发现某个分区的数据被误删除后.要做的第一件事是立马卸载被误删除文件所在的分区,或者又一次以仅仅读方式挂载此分区. 这么做的原因事实上非常eas ...

  4. JQuery图例

  5. python generator iterator和iterable object

    1 iterable object list.dict.set.tuple.file(在每行上iterate)等都是iterable object,但是它们不是iterator.但是它们可以转换成it ...

  6. expand_dims

    tf.expand_dims  |  TensorFlow https://tensorflow.google.cn/api_docs/python/tf/expand_dims tf.expand_ ...

  7. 如何配置MYSQL的MASTER---SLAVE复制备份?

    如何配置MYSQL的MASTER---SLAVE复制备份? 一.配置一个mysql服务器做master:     在配置文件my.ini中添加如下内容: log-bin=matster-binlog- ...

  8. javax.servlet.http.Part 文件上传

    编辑jsp页面: <html> <head> <base href="<%=basePath%>"> <title>My ...

  9. Joseph问题 (线段树)

    Joseph问题似乎是入门题,就是那个报数出圈的问题,不过它暴力模拟的复杂度是O(nm)的,如果题目的数据范围达到了30000,那就超时了.怎么用线段树维护呢? 我们可以这么考虑,每次我们其实要查询在 ...

  10. ORACLE游标概念讲解

    1,什么是游标? ①从表中检索出结果集,从中每次指向一条记录进行交互的机制. ②关系数据库中的操作是在完整的行集合上执行的.   由 SELECT 语句返回的行集合包括满足该语句的 WHERE 子句所 ...