scrapy框架爬取多级页面
spides.py
# -*- coding: utf-8 -*-
import scrapy
from weather.items import WeatherItem
from scrapy.crawler import CrawlerProcess
import re
'''
多级分类爬取
'''
class IgxSpider(scrapy.Spider):
name = 'igx_result'
allowed_domains = ['www.igxpt.com']
# start_urls = ['http://www.igxpt.com/cate/192/'] def start_requests(self):
start_urls = ['http://www.igxpt.com/cate/{}/'.format(str(i)) for i in range(192, 194)] #这里我是简写的,当然也可以进入主页面,爬取这些url 那就要多一级分类了
for url in start_urls:
yield scrapy.Request(url=url) def parse(self, response): '''得到分页页码-----start'''
page = response.xpath('//div[@class="dataTables_paginate paging_simple_numbers"]/span/text()').extract_first()
ret = re.search('共(\d+)页', page)
number = ret.group(1)
#print(page,number)
page_link = response.xpath('//ul[@class="pagination"]/li/a/@href').extract_first()
current_url = 'http://www.igxpt.com'+page_link.split('=')[0]+'='
'''得到分页页码-----end''' clearfix = response.xpath('//ul[@class="shop-list-recommend mt20 clearfix"]/li')
for li in clearfix:
item = WeatherItem()
item['name'] = li.xpath('./a/p[1]/text()').extract_first()
url_img = li.xpath('./a/div/img/@src').extract_first()
item['url'] = "http://www.igxpt.com" + (url_img)
price_alia = li.xpath('./a/p[2]/span[@class="blue"]/text()').extract_first()
item['price'] = price_alia + "元"
yield item # 拼接url 递归调用分页
urls = [current_url + '{}'.format(str(i)) for i in range(1, int(number) + 1)]
for se in urls:
yield scrapy.Request(url=se, callback=self.parse)
items.py
import scrapy class WeatherItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
url = scrapy.Field()
price= scrapy.Field()
pipelines.py
# -*- coding: utf-8 -*- # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html import pymysql
import urllib.request
class WeatherPipeline(object):
def process_item(self, item, spider):
name = item['name']
url = item['url']
price= item['price'] connection = pymysql.connect(
host='127.0.0.1',
user='root',
passwd='root',
db='scrapy',
# charset='utf-8',
cursorclass=pymysql.cursors.DictCursor
) try: # '''下载图片'''
# imgname = url.split('/')[-1]
# path = r"D:\Python\weather\weather\images\%s" % (imgname)
# urllib.request.urlretrieve(url, filename=path)
'''插入数据库'''
with connection.cursor() as cursor: sql = """INSERT INTO `goods_info_detail` (name, url, price) VALUES (%s, %s, %s) """
cursor.execute(
sql,(name,url,price)
)
connection.commit() except ValueError as e:
print(e) finally:
connection.close() return item
settings.py
LOG_LEVEL = 'WARNING'
BOT_NAME = 'weather' SPIDER_MODULES = ['weather.spiders']
NEWSPIDER_MODULE = 'weather.spiders' '''
管道
'''
ITEM_PIPELINES = {
'weather.pipelines.WeatherPipeline': 300,
}
scrapy框架爬取多级页面的更多相关文章
- 使用scrapy框架爬取自己的博文(2)
之前写了一篇用scrapy框架爬取自己博文的博客,后来发现对于中文的处理一直有问题- - 显示的时候 [u'python\u4e0b\u722c\u67d0\u4e2a\u7f51\u9875\u76 ...
- scrapy框架爬取豆瓣读书(1)
1.scrapy框架 Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试 ...
- scrapy(四): 爬取二级页面的内容
scrapy爬取二级页面的内容 1.定义数据结构item.py文件 # -*- coding: utf-8 -*- ''' field: item.py ''' # Define here the m ...
- scrapy框架爬取笔趣阁完整版
继续上一篇,这一次的爬取了小说内容 pipelines.py import csv class ScrapytestPipeline(object): # 爬虫文件中提取数据的方法每yield一次it ...
- scrapy框架爬取笔趣阁
笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在biquge.py里在加一个爬取循环,在pipelines.py添加保存函数即可 1 创建一个scrapy项目 ...
- Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...
- 爬虫入门(四)——Scrapy框架入门:使用Scrapy框架爬取全书网小说数据
为了入门scrapy框架,昨天写了一个爬取静态小说网站的小程序 下面我们尝试爬取全书网中网游动漫类小说的书籍信息. 一.准备阶段 明确一下爬虫页面分析的思路: 对于书籍列表页:我们需要知道打开单本书籍 ...
- scrapy框架爬取糗妹妹网站妹子图分类的所有图片
爬取所有图片,一个页面的图片建一个文件夹.难点,图片中有不少.gif图片,需要重写下载规则, 创建scrapy项目 scrapy startproject qiumeimei 创建爬虫应用 cd qi ...
- 使用scrapy框架爬取自己的博文(3)
既然如此,何不再抓一抓网页的文字内容呢? 谷歌浏览器有个审查元素的功能,就是按树的结构查看html的组织形式,如图: 这样已经比较明显了,博客的正文内容主要在div 的class = cnblogs_ ...
随机推荐
- 基于Mybatis的bookstore架构模型
总共分为Control,dao,enter,entity,service,util,view这几层.同时还含有一个mapperconfig.xml文件. 1,mapperconfig.xml 这里面用 ...
- ROS中的3D机器人建模(二)
一,创建我们的第一个URDF模型 我们设计的第一个机器人模型是pan-and-tilt机械结构,代码如下 pan_tilt.urdf: 1 <?xml version="1.0&quo ...
- [C/C++]_[Unicode转Utf8,Ansi转Unicode,Ansi文件转Utf8文件]
http://blog.csdn.net/infoworld/article/details/15337665 场景: 1.也就只有windows需要那么麻烦,还搞一个ANSI编码.学学mac os ...
- excel 2010 如何设置日期选择器
excel 中想输入很多的日期.如果每个日期都直接手动输入太过于繁琐,而且容易出错.想制作一个日期选择器,直接鼠标点选就可以了. 效果如下: 具体实现参考 http://wenku.baidu.com ...
- WeChall_Encodings: URL (Training, Encoding)
Your task is to decode the following: %59%69%70%70%65%68%21%20%59%6F%75%72%20%55%52%4C%20%69%73%20%6 ...
- Linux7种运行模式
1)在超级权限下(#)vim /etc/inittab 2)文件内容模式的理解: 0 代表:关机模式(此模式 linux是关机状态) 1 代表:单用户模式(例如root的密码忘记了,可以在该模式下完成 ...
- php gettype()函数
gettype() 会根据 参数类型返回值: boolean:表示变量为布尔类型 integer:表示变量为整数类型 double :表示变量为float类型(历史原因) string:表示变量为 ...
- linux shell 操作 mysql命令(不进入mysql操作界面)
由于需要,需要将一系列mysql的操作制作成.sh文件,只需要shell操作bash命令就可以傻瓜式的完成黑盒任务. #!/bin/bash mysql -uroot -p??? -e "c ...
- aws 社交媒体技术大会 部分总结
早上会议总结:. 1. 介绍了aws的应用,无技术知识点.略 2. 云原生: 就是一种在云中原生的一种技术 新常态:已经成为一种常态,基本都有的服务都可以部署在云端. 对 “互联网+” 和 “+互联 ...
- hive on spark 编译时遇到的问题
1.官方网站下载spark 1.5.0的源码 2.根据官方编译即可. export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedC ...