因为最近在找房子在豆瓣小组-上海租房上找,发现搜索困难,于是想利用爬虫将数据抓取. 顺便熟悉一下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. word-wrap:break-word和word-break:break-all的小小比较

    个人感觉word-break:break-all;的用法比word-wrap:break-word;要好点, 当然这是出于某些需求而言,下面简单说下word-break:break-all的优点 wo ...

  2. udp打洞( NAT traversal )的方法介绍

    http://www.cnblogs.com/whyandinside/archive/2010/12/08/1900492.html http://www.gzsec.com/oldversion/ ...

  3. c++ builder ListView实现可编辑任意列(转)

    // --------------------------------------------------------------------------- // Form的构造函数中填充StrinG ...

  4. HBase简介(很好的梳理资料)

    http://www.tuicool.com/articles/iieIz2 一.   简介 history  started by chad walters and jim 2006.11 G re ...

  5. windows7安装远程服务器AD域管理工具

    目的:在win7上安装“远程服务器管理工具”,这样可以在客户端进行对服务器的AD域的操作,避免了远程登陆进服务器的麻烦. 前提条件:一般此工具只有管理员才具有有效使用权限,所以,在域administr ...

  6. if [-f build/core/envsetup.mk -a -f Makefile ]; then

    这个语法是什么意思?

  7. CentOS6.5 python 2.6升级到2.7

    在CentOS6.5下,将自带的python2.6.6升级到python2.7.3,解决方法如下: 下载python2.7.3包,并解压缩,输入命令:#wget http://python.org/f ...

  8. 计算2的n次方的三种方法(C语言实现)

    C代码如下: #include <stdio.h> int func1(int n) { <<n; } int func2(int n) { ) { ; } )*; } int ...

  9. debug版本和release版本的区别?

    好久没有做web项目了,这项目完成了要发布网站,不好忘了 以前操作过的? 还好脑子还是有点印象 现还是 写个文档吧记录吧 免得 以后作别的了又忘了 那可不妙啊 网站发布步骤:1.先将

  10. C#使用结构来传递多个参数

    当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } clas ...