使用scrapy爬取suning
# -*- coding: utf-8 -*-
import scrapy
from copy import deepcopy
class SuSpider(scrapy.Spider):
name = 'su'
allowed_domains = ['suning.com']
start_urls = ['http://list.suning.com/?safp=d488778a.error1.0.4786e76351']
def parse(self, response):
# 获取大分类列表
bcate_list = response.xpath("//div[@class='allsortLeft']/ul/li")
for bcate in bcate_list:
item = {}
# 获取大分类class的值
class_name = bcate.xpath("./@class").extract_first()
# 获取所有大分类的名称
item["BCate"] = bcate.xpath("./a/span/text()").extract_first()
# print(item["BCate"])
# 根据大分类的class定位每个大分类下的所有小分类
scate_list = response.xpath("//div[@class='{}']/div".format(class_name))
for scate in scate_list:
# 小分类的名称
item["SCate"] = scate.xpath("./div[1]/a/@title").extract_first()
# 获取每个小分类下的所有标签
tag_list = scate.xpath("./div[2]/a")
for tag in tag_list:
# 每个标签的链接和名称
item["tag"] = tag.xpath("./text()").extract_first()
item["tag_link"] = "http:" + tag.xpath("./@href").extract_first()
# 进入列表页
yield scrapy.Request(
item["tag_link"],
callback=self.good_list,
meta={"item": deepcopy(item)}
)
def good_list(self, response):
item = deepcopy(response.meta["item"])
# 获取当前页的所有商品列表
li_list = response.xpath("//div[@id='product-wrap']/div/ul/li")
for li in li_list:
# 获取商品的图片地址,名称,价格,商品详情页的链接
item["good_img"] = "http:"+li.xpath(".//div[@class='res-img']/div/a/img/@src").extract_first()
item["good_name"] = li.xpath(".//div[@class='res-info']/div/a/text()").extract_first()
item["good_price"] = li.xpath(".//div[@class='res-info']/div/span/text()").extract_first()
item["good_href"] = li.xpath(".//div[@class='res-info']/div/a/@href").extract_first()
# 进入商品详情页
if item["good_href"] != "javascript:void(0);":
yield scrapy.Request(
"http:"+item["good_href"],
callback=self.good_detail,
meta={"item": deepcopy(item)}
)
# 翻页
next_url = response.xpath("//a[@id='nextPage']/@href").extract_first()
if next_url:
yield scrapy.Request(
next_url,
callback=self.good_list,
meta={"item": response.meta["item"]}
)
def good_detail(self, response):
item = response.meta["item"]
# 获取当前商品的属性规格:颜色、版本、
size_list = response.xpath("//div[@id='J-TZM']/dl")
for size in size_list:
size_name = size.xpath("./dt/span/text()").extract_first()
size_value = size.xpath("./dd/ul/li/@title").extract()
item[size_name] = size_value
print(item)
使用scrapy爬取suning的更多相关文章
- Scrapy爬取美女图片 (原创)
有半个月没有更新了,最近确实有点忙.先是华为的比赛,接着实验室又有项目,然后又学习了一些新的知识,所以没有更新文章.为了表达我的歉意,我给大家来一波福利... 今天咱们说的是爬虫框架.之前我使用pyt ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- scrapy爬取西刺网站ip
# scrapy爬取西刺网站ip # -*- coding: utf-8 -*- import scrapy from xici.items import XiciItem class Xicispi ...
- scrapy爬取豆瓣电影top250
# -*- coding: utf-8 -*- # scrapy爬取豆瓣电影top250 import scrapy from douban.items import DoubanItem class ...
- scrapy爬取极客学院全部课程
# -*- coding: utf-8 -*- # scrapy爬取极客学院全部课程 import scrapy from pyquery import PyQuery as pq from jike ...
- scrapy爬取全部知乎用户信息
# -*- coding: utf-8 -*- # scrapy爬取全部知乎用户信息 # 1:是否遵守robbots_txt协议改为False # 2: 加入爬取所需的headers: user-ag ...
- Scrapy爬取Ajax(异步加载)网页实例——简书付费连载
这两天学习了Scrapy爬虫框架的基本使用,练习的例子爬取的都是传统的直接加载完网页的内容,就想试试爬取用Ajax技术加载的网页. 这里以简书里的优选连载网页为例分享一下我的爬取过程. 网址为: ht ...
- Scrapy爬取静态页面
Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...
- 用scrapy爬取京东的数据
本文目的是使用scrapy爬取京东上所有的手机数据,并将数据保存到MongoDB中. 一.项目介绍 主要目标 1.使用scrapy爬取京东上所有的手机数据 2.将爬取的数据存储到MongoDB 环境 ...
随机推荐
- 组合数学1.4&3.10 By cellur925
本文引用于清华大学出版社卢开澄.卢华明<组合数学第五版>. 今天我们稍微讨论下圆排列以及$n$对夫妻的问题. 1.4圆周排列 这个问题是:从$n$个人中取$r$个在圆周上,我们用$Q(n, ...
- oauth2(spring security)报错method_not_allowed(Request method 'GET' not supported)解决方法
报错信息 <MethodNotAllowed> <error>method_not_allowed</error> <error_description> ...
- NowCoder数列
题目:https://www.nowcoder.com/questionTerminal/0984adf1f55a4ba18dade28f1ab15003 #include <iostream& ...
- April Fools Contest 2017 A
Description Input The input contains a single integer a (1 ≤ a ≤ 30). Output Output a single integer ...
- selenium2+python自动化2-元素定位
嘻嘻,书接上回,接着唠,这里先补充一下自动化要掌握的四个步骤吧:获取元素.操作元素.获取返回值.断言(返回结果与期望结果是否一致),最后就是自动化测试报告的生成.这一片主要讲一下如何进行元素定位.元素 ...
- web简单的整体测试
网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题 ab测试 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它 ...
- hash系列集合的性能优化
hash系列的集合: HashSet.LinkedHashSet 采用hash算法决定元素在集合中的存储位置 HashMap.LinkedHashMap.Hashtable 采用hash算 ...
- poj3616 Milking Time
思路: dp. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...
- Ajax深入理解
Ajax Asynchronous JavaScript and XML 异步的JavaScript和XML ajax通过与后台服务器进行少量的数据交换,ajax可以使页面实现异步更新,即不需要重新 ...
- 26款优秀的Android逆向工程工具
26款优秀的Android逆向工程工具