scrapy-抓取天猫女装销量前60的商品名称、价格、链接及打开连接后的店铺名称和链接
来源:互联网 发布:只会php有用吗 编辑:程序博客网 时间:2024/04/29 10:14
目标网址——https://list.tmall.com/search_product.htmspm=a220m.1000858.1000724.4.JnyabL&cat=50025135&sort=d&style=g&from=mallfp..pc_1_searchbutton&tmhkmain=0#J_Filter,界面如下,我们要抓取这个界面的商品价格、名称、和商品的链接
打开商品连接后,我们要获取的信息为店铺名称和链接,如下图所示:
项目结构如图:
贴代码:
tm_goods.py
# -*- coding: utf-8 -*-import scrapyfrom topgoods.items import TopgoodsItemimport sysreload(sys)sys.setdefaultencoding('utf-8')class TmGoodsSpider(scrapy.Spider): name = "tm_goods" allowed_domains =["http://www.tmall.com"] start_urls = ("http://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.4.JnyabL&cat=50025135" "&sort=d&style=g&from=mallfp..pc_1_searchbutton&tmhkmain=0#J_Filter",) count = 0 def parse(self,response): TmGoodsSpider.count += 1 divs = response.xpath('//div[@id="J_ItemList"]/div[@class="product "]/div') #<div class="product-iWrap">是区分他们的共同元素 if not divs: self.log("List Page error __%s" %response.url) print "Goods numbers:",len(divs) for div in divs: item = TopgoodsItem( )#位置不要放错了!!!!!!! item["GOODS_PRICE"] = div.xpath('p[@class="productPrice"]/em/@title')[0].extract() item["GOODS_NAME"] = div.xpath('p[@class="productTitle"]/a/@title')[0].extract() goods_url = div.xpath('p[@class="productTitle"]/a/@href')[0].extract() item["GOODS_URL"] = goods_url if "http:" in goods_url else ("http:"+ goods_url) yield scrapy.Request(url = item["GOODS_URL"],meta = {"item":item},callback = self.parse_detail,dont_filter=True) print item["GOODS_NAME"] def parse_detail(self,response):#处理打开商品链接的那个页面 item = response.meta["item"] divs = response.xpath('//div[@class="slogo"]/a') item["SHOP_NAME"] = divs.xpath('//strong/text()')[0].extract() shop_url = divs.xpath('@href')[0].extract() item["SHOP_URL"] = shop_url if "http:" in shop_url else ("http"+ shop_url) print item["GOODS_PRICE"],item["GOODS_NAME"], item["GOODS_URL"],item["SHOP_NAME"],item["SHOP_URL"] yield item
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# http://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass TopgoodsItem(scrapy.Item): # define the fields for your item here like: GOODS_URL = scrapy.Field() GOODS_PRICE = scrapy.Field() GOODS_NAME = scrapy.Field() SHOP_NAME = scrapy.Field() SHOP_URL = scrapy.Field()
结果如图:
0 0
- scrapy-抓取天猫女装销量前60的商品名称、价格、链接及打开连接后的店铺名称和链接
- java 输入输出--抓取凡客一万多商品名称和价格,图片~用到的
- python抓取京东商城的商品名称和价格
- 抓取凡客一万多商品,链接,商品名称,价格,图片~~~版本1
- 抓取淘宝某类商品名称和价格
- 提取网易的链接及链接名称
- Scrapy不断的抓取下一个链接如何实现
- scrapy抓取csdn中标题带有“语义”关键字的文章的标题和链接
- python beautifulsoup抓取京东商品名称和价格
- Python爬虫学习之抓取商品名称和价格
- 关于scrapy有用的链接
- 火狐浏览器打开天猫店铺提示链接不安全无法访问该怎么办?
- 根据销量排序,获取商品的数据(价格,名称,url,商品图片)
- 抓取某店铺的ebayno
- 硬链接和软连接(符号链接)的区别
- Linux的硬链接和软连接(符号链接)
- 硬链接和软连接(符号链接)的区别 .
- 硬链接和软链接(符号连接)的区别
- Android Studio jar包的导入
- css 介绍
- WPF 绘正方形,设置鼠标不偏离,保持在右下角
- Android最新敲诈者病毒分析及解锁(11月版)
- 用指针数组构造字符串数组
- scrapy-抓取天猫女装销量前60的商品名称、价格、链接及打开连接后的店铺名称和链接
- Home/Search key doesn't work, and Power key cann't lock screen 关于home首页 搜索按键失灵,电源键不能锁屏问题
- Java注解知识点整理
- eclipse运行android时Console最后提示Done!不运行程序
- 虚函数和纯虚函数的作用与区别 http://blog.csdn.net/xwpc702/article/details/8670025
- 用指针实现逆序列存放数组元素值
- layer插件API文档学习
- C/C++之回调函数
- 使用指针连接两个字符串