简单步骤我大概写一下。
用scrapy创建一个爬虫项目。
【安装scrapy】的过程不在这里多做解释。创建项目命令。
scrapy startproject demo01
如下图所示:

然后用pycharm打开此项目哈哈。如下图。

通过命令查看当前的爬虫项目:
scrapy list
通过Firebug 的firepath查看价格的xpath相关信息
来写代码搞起来。
# -*- coding:utf-8 -*-
# **********************************
# ** http://weibo.com/lixiaodaoaaa #
# ****** by:lixiaodaoaaa ***********
import scrapy
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
'''############################################################*
我发现这样解决:请扩展Xpath方法能够解决你的问题。 *
简单解释下: *
HtmlXPathSelector使用了Xpath来解析数据 *
// div / dl / dd[5] // div / span / 表示选择所有的ul标签下的li标签*
''' ############################################################*
class GanjiSprider(scrapy.Spider):
name = "zufang"
start_urls = ['http://sh.ganji.com/fang1/putuo/m1/']
def parse(self, response):
siteSelector = HtmlXPathSelector(response)
# **************************************************************************
# 来匹配所有div d1 dd div span text的标签下的数据 这个类似于Xpath的写法非常的不错**
## 主要感谢了 HtmlXPathSelector **
# ***************************************************************************
allRawPriceList = siteSelector.select('//div/dl/dd[5]//div/span[1]/text()').extract()
for singlePrice in allRawPriceList:
print singlePrice
*爬出来的结果如下:

网友评论