因为最近在找房子在豆瓣小组-上海租房上找,发现搜索困难,于是想利用爬虫将数据抓取. 顺便熟悉一下Python.

这边有scrapy 入门教程出处:http://www.cnblogs.com/txw1958/archive/2012/07/16/scrapy-tutorial.html

差不多跟教程说的一样,问题技术难点是 转码,上述教程并未详细指出. 我还是把代码贴出来,请供参考.

E:\tutorial>tree /f
Folder PATH listing for volume 文档
Volume serial number is -BBB3
E:.
│ scrapy.cfg

└─tutorial
│ items.py
│ items.pyc
│ pipelines.py
│ pipelines.pyc
│ settings.py
│ settings.pyc
│ __init__.py
│ __init__.pyc

└─spiders
douban_spider.py
douban_spider.pyc
__init__.py
__init__.pyc

item.py: 这有一篇很好介绍ITEM的文章(http://blog.csdn.net/iloveyin/article/details/41309609)

from scrapy.item import Item, Field
class DoubanItem(Item):
title = Field()
link = Field()
#resp = Field()
#dateT = Field()

pipelines.py #定义你自己的PipeLine方式,详细中文转码可在此处解决

# -*- coding: utf-8 -*-
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import codecs class TutorialPipeline(object):
def __init__(self):
self.file = codecs.open('items.json', 'wb', encoding='gbk') def process_item(self, item, spider):
line = json.dumps(dict(item)) + '\n'
print line
self.file.write(line.decode("unicode_escape"))
return item

setting.py 加入相应的 ITEM_PIPELINES 属性(红色字体为新加部分)

# -*- coding: utf-8 -*-

# Scrapy settings for tutorial project
#
# For simplicity, this file contains only the most important settings by
# default. All the other settings are documented here:
#
# http://doc.scrapy.org/en/latest/topics/settings.html
# BOT_NAME = 'tutorial' SPIDER_MODULES = ['tutorial.spiders']
NEWSPIDER_MODULE = 'tutorial.spiders'
ITEM_PIPELINES = {
'tutorial.pipelines.TutorialPipeline':300
} # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'tutorial (+http://www.yourdomain.com)'

接下来是spider.py

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from tutorial.items import DoubanItem class DoubanSpider(BaseSpider):
name = "douban"
allowed_domains = ["douban.com"]
start_urls = [
"http://www.douban.com/group/shanghaizufang/discussion?start=0",
"http://www.douban.com/group/shanghaizufang/discussion?start=25",
"http://www.douban.com/group/shanghaizufang/discussion?start=50",
"http://www.douban.com/group/shanghaizufang/discussion?start=75",
"http://www.douban.com/group/shanghaizufang/discussion?start=100",
"http://www.douban.com/group/shanghaizufang/discussion?start=125",
"http://www.douban.com/group/shanghaizufang/discussion?start=150",
"http://www.douban.com/group/shanghaizufang/discussion?start=175",
"http://www.douban.com/group/shanghaizufang/discussion?start=200"
] def parse(self, response):
hxs = HtmlXPathSelector(response)
sites = hxs.xpath('//tr/td')
items=[]
for site in sites:
item = DoubanItem()
item['title'] =site.xpath('a/@title').extract()
item['link'] = site.xpath('a/@href').extract()
# item['resp'] = site.xpath('text()').extract()
# item['dateT'] = site.xpath('text()').extract()
items.append(item)
return items

用JSON数据方式导出:

scrapy crawl douban -o items.json -t json

这有个JSON 转成CSV工具的网站,可以帮助转换:

https://json-csv.com/

结果效果展示,这样方便检索和过滤

利用python scrapy 框架抓取豆瓣小组数据的更多相关文章

  1. Python爬虫之抓取豆瓣影评数据

    脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...

  2. python scrapy框架爬取豆瓣

    刚刚学了一下,还不是很明白.随手记录. 在piplines.py文件中 将爬到的数据 放到json中 class DoubanmoviePipelin2json(object):#打开文件 open_ ...

  3. 如何利用Python网络爬虫抓取微信朋友圈的动态(上)

    今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...

  4. 利用Python网络爬虫抓取微信好友的签名及其可视化展示

    前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...

  5. 利用Python网络爬虫抓取微信好友的所在省位和城市分布及其可视化

    前几天给大家分享了如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,感兴趣的小伙伴可以点击链接进行查看.今天小编给大家介绍如何利用Python网络爬虫抓取微信好友的省位和城市,并且将 ...

  6. 如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例

    前几天给大家分享了利用Python网络爬虫抓取微信朋友圈的动态(上)和利用Python网络爬虫爬取微信朋友圈动态——附代码(下),并且对抓取到的数据进行了Python词云和wordart可视化,感兴趣 ...

  7. Python小爬虫——抓取豆瓣电影Top250数据

    python抓取豆瓣电影Top250数据 1.豆瓣地址:https://movie.douban.com/top250?start=25&filter= 2.主要流程是抓取该网址下的Top25 ...

  8. 【python数据挖掘】爬取豆瓣影评数据

    概述: 爬取豆瓣影评数据步骤: 1.获取网页请求 2.解析获取的网页 3.提速数据 4.保存文件 源代码: # 1.导入需要的库 import urllib.request from bs4 impo ...

  9. 基于python的scrapy框架爬取豆瓣电影及其可视化

    1.Scrapy框架介绍 主要介绍,spiders,engine,scheduler,downloader,Item pipeline scrapy常见命令如下: 对应在scrapy文件中有,自己增加 ...

随机推荐

  1. oracle 游标示例

    declare iCount int:=0; sPath nvarchar2(200); tdzsh nvarchar2(50);begin for x in (select c.imgpath fr ...

  2. 【Linux】基于Bind_DLZ和MySQL数据的DNS搭建

    参考:http://www.linuxfromscratch.org/blfs/view/cvs/server/bind.html 参考:http://bind-dlz.sourceforge.net ...

  3. JS常用的设计模式(14)—— 备忘录模式

    备忘录模式在js中经常用于数据缓存. 比如一个分页控件, 从服务器获得某一页的数据后可以存入缓存.以后再翻回这一页的时候,可以直接使用缓存里的数据而无需再次请求服务器. 实现比较简单,伪代码: var ...

  4. 五、Eclipse编写struts.xml没有提示的问题

      五.Eclipse编写struts.xml没有提示的问题 原因:找不到约束文件 解决: 联网 手工配 a.eclipse的菜单:window\preferences

  5. 新手浅谈C#关于abstract和interface

    abstract:抽象类,是对某一类对象的抽象,继承抽象类的类和抽象类是属于关系 interface:接口,可以说是一种规范,继承接口的类表达则是,我继承了这个接口,我能做什么 举个常用的例子:抽象一 ...

  6. 在信号处理函数中调用longjmp

    错误情况及原因分析 前两天看APUE的时候,有个程序要自己制作一个sleep程序,结果在这个程序中就出现了在信号处理函数中调用longjmp函数的情况,结果就出现了错误,具体错误是啥呢,请参见下面这段 ...

  7. ParameterDirection参数类型

    IDataParameter[] paramArray = new IDataParameter[]{ AdoHelper.GetParameter("ReturnValue",D ...

  8. svn的使用!!!

    1.SVN:subversion(子级源代码版本控制管理软件) 2.SVN的作用 (1)避免开发同一项目不会出现代码覆盖. (2)同一文件可以创建许多不同的版本,并可以随时查看不同版本的内容. (3) ...

  9. Flask Web Development —— Web表单(上)

    Flask-WTF扩展使得处理web表单能获得更愉快的体验.该扩展是一个封装了与框架无关的WTForms包的Flask集成. Flask-WTF和它的依赖集可以通过pip来安装: (venv) $ p ...

  10. WPF控件数据单项绑定

    建立一个姓名,年龄输入框,如图: XAML代码: <TextBox Name="txtName" Text="{Binding Name}" ToolTi ...