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_ ...
随机推荐
- LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- RPC简单设计方案
服务端: 启动后,等待客户端发来信息,收到信息后进行处理,返回结果. 客户端: 主线程中发起一次RPC,那么就将信息封装成一个任务,提交到线程池,阻塞等待结果. 线程池中工作线程执行任务,发送信息,等 ...
- VUE 开发报表,非编码方式
官网:http://doc.sougn.com 下载地址:https://pan.baidu.com/share/init?surl=P0O9sjfzC8nuQxirDfjW1A 密码:4oev 先 ...
- Codeforces_714
A.相遇时间段l = max(l1,l2),r = min(r1,r2),再判断k是否在里面. #include <iostream> using namespace std; long ...
- java11类和对象
import java.util.Scanner; public class jh_01_如何认识事物 { public static void main(String[] args) { Scann ...
- 小白学 Python 数据分析(6):Pandas (五)基础操作(2)数据选择
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- php 截取字符串长度 并 把超出规定长度的内容用...替代
<?php header("content-type:text/html;charset=utf-8");#设置文件编码 error_reporting(E_ALL);#设置 ...
- Maven项目pom文件的节点释意
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- k8s Pipline CI/CD
一.Pipeline介绍 pipeline是一套jenkins官方提供的插件,它可以用来在jenkins中实现和集成连续交付 用户可以利用Pipeline的许多功能: 代码:pipeline在代码中实 ...
- k8s系列---k8s认证及serviceaccount、RBAC
http://blog.itpub.net/28916011/viewspace-2215100/ 对作者文章有点改动 注意kubeadm创建的k8s集群里面的认证key是有有效期的,这是一个大坑!! ...