鱼C论坛

 找回密码
 立即注册
查看: 1529|回复: 3

关于初学scrapy框架保存问题

[复制链接]
发表于 2018-4-5 21:26:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wongyusing 于 2018-10-14 19:10 编辑

代码如下:
  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from op.items import OpItem
  4. import re
  5. import os
  6. from scrapy.http import Request
  7. import requests

  8. class MyspiderSpider(scrapy.Spider):
  9.     name = 'myspider'
  10.     #allowed_domains = ['http://www.zhaojianpu.com/liuxing/']
  11.     start_urls = ['http://www.zhaojianpu.com/liuxing//']
  12.     # 文件保存地址
  13.     base = r'/home/wongyusing/桌面/op/op/download/'
  14.     def parse(self, response):
  15.         items = []

  16.         pattern = r'<li><a href="(.*?)" target="_blank">(.*?)</a></li>'
  17.         mains = re.findall(pattern, response.text)
  18.         reg = '<font color="FF0000">(.*?)</font>'
  19.         #item['page_Name'] = '第' + re.findall(reg,response.text)[0] + '页'
  20.         #print(mains, page_Name)
  21.         for main in mains:
  22.             item = OpItem()
  23.             item['siteURL'] = main[0]
  24.             item['title'] = main[1]
  25.             item['page_Name'] = self.base + '第' + re.findall(reg, response.text)[0] + '页'
  26.             item['fileName'] = item['page_Name'] + '/' + item['title']       #6.os.path.exists(path)如果path存在,返回True;如果path不存在,返回False。
  27.             items.append(item)

  28.         for item in items:  #创建文件夹
  29.             fileName=item['fileName']
  30.             if not os.path.exists(fileName):
  31.                 os.makedirs(fileName)
  32.                 # 6.os.path.exists(path)如果path存在,返回True
  33.                 #用meta传入下一层
  34.             yield Request(url=item['siteURL'],meta={'item1':item},callback=self.parse_two)

  35.     def parse_two(self,response):
  36.         detailURL = response.xpath('//*[@id="Article"]/div[1]/img/@src').extract()
  37.         content = response.xpath('//*[@id="Article"]/div[1]/p[1]/text()').extract()


复制代码


问题如下:
1.我想让保存的时候是按(‘流行曲目’→→→第一页(页码数)→→→歌曲名(文件夹)→→→图片,简介)
的方式保存,需要在那个位置修改代码?(这个问题解决了)

2.该网站的排序是反转的,第二页的是305,第三页是304,以此类推到尾页是1,
该如何能让保存的时候是”305文件夹“对应第二页,”304“对应的内容是第三页呢??(这个也解决了)

3.代码中的meta={'item1':item}是什么意思作用是什么??(这个是我看别人写的,不太懂这个意思)
4.图片有两种类型,jpg格式,gif格式,scrapy内置的图片保存函数能用吗??该怎么用?

5.为什么我在打印的时候,shell只返回两个内容?但经观察,每一页有60篇乐谱,如下图所示:
yuepu.png

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-6 03:19:55 | 显示全部楼层
本帖最后由 wongyusing 于 2018-4-6 03:21 编辑

问题已解决,代码等我写好注释再放上来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-6 13:01:03 | 显示全部楼层
完成了,
spider.py
  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from op.items import OpItem
  4. import re
  5. import os
  6. from scrapy.http import Request
  7. import requests
  8. import time

  9. class MyspiderSpider(scrapy.Spider):
  10.     name = 'myspider'
  11.     #allowed_domains = ['http://www.zhaojianpu.com/liuxing/']
  12.     start_urls = ['http://www.zhaojianpu.com/liuxing//']
  13.     # 文件保存路径
  14.     base = r'/home/wongyusing/桌面/op/op/download/'
  15.     def parse(self, response):
  16.         items = []

  17.         pattern = r'<li><a href="(.*?)" target="_blank">(.*?)</a></li>'
  18.         mains = re.findall(pattern, response.text)
  19.         reg = '<font color="FF0000">(.*?)</font>'#获取当前页,并用做文件夹名

  20.         for main in mains:
  21.             item = OpItem()
  22.             #获取乐谱的url
  23.             item['siteURL'] = main[0]
  24.             #获取标题
  25.             item['title'] = main[1]
  26.             # 获取当前页,并用做文件夹名,/home/wongyusing/桌面/op/op/download/第3页/
  27.             item['page_Name'] = self.base + '第' + re.findall(reg, response.text)[0] + '页'
  28.             #制作文件夹路径,“/home/wongyusing/桌面/op/op/download/第3页/爱了再说简谱图片”
  29.             item['fileName'] = item['page_Name'] + '/' + item['title']       #6.os.path.exists(path)如果path存在,返回True;如果path不存在,返回False。
  30.             items.append(item)


  31.         for item in items:  #创建文件夹
  32.             fileName=item['fileName']
  33.             if not os.path.exists(fileName):
  34.                 os.makedirs(fileName)#/home/wongyusing/桌面/op/op/download/第3页/爱了再说简谱图片
  35.                 # 6.os.path.exists(path)如果path存在,返回True
  36.                 #用meta传入下一层
  37.             yield Request(url=item['siteURL'],meta={'item1':item},callback=self.parse_two)

  38.         #获取乐谱数共18374篇乐谱
  39.         all_score = response.xpath('/html/body/div[7]/div/b[1]/text()').extract()[0]
  40.         #获取一页有多少篇乐谱
  41.         row = response.xpath('/html/body/div[7]/div/b[2]/text()').extract()[0]
  42.         max_page = int(all_score)//int(row)#地板除得出总页数

  43.         for pa in range(1,max_page+1):#拼接URL,回调上去继续获取乐谱url列表

  44.             page_url = 'http://www.zhaojianpu.com/liuxing/List_' + str(pa) + '.html'

  45.             yield Request(page_url, callback=self.parse)



  46.     def parse_two(self,response):
  47.         item = OpItem()
  48.         #获取图片的url
  49.         url_3 = response.xpath('//*[@id="Article"]/div[1]/img/@src').extract()[0]
  50.         #item2 = response.meta['item1']
  51.         suffix = url_3[-4:]   #获取后缀,因为图片有两种格式.gif和.jpg格式
  52.         #print(url_3)
  53.         time.sleep(10)
  54.         item['detailURL'] = 'http://www.zhaojianpu.com' + response.xpath('//*[@id="Article"]/div[1]/img/@src').extract()[0]

  55.         item['content'] = response.xpath('//*[@id="Article"]/div[1]/p[1]/text()').extract()[0]
  56.         item2 = response.meta['item1']
  57.         item['path'] = item2['fileName'] + '/' + 'music' + suffix #生成绝对路径保存图片
  58.         item['path2'] = item2['fileName'] + '/' + '简介' + '.txt' #生成绝对路径保存简介
  59.         #print(item['path'])
  60.         yield item
  61.         







复制代码

items.py

  1. import scrapy



  2. class OpItem(scrapy.Item):
  3.     # define the fields for your item here like:
  4.     # name = scrapy.Field()
  5.     siteURL = scrapy.Field()  # 首页中乐谱的URL
  6.     pageURL = scrapy.Field() #每一张图片入口URL
  7.     page_Name = scrapy.Field()#页码名字,用作保存
  8.     detailURL = scrapy.Field() #图片原图地址
  9.     content = scrapy.Field()#乐谱简介
  10.     title = scrapy.Field() #乐谱的标题
  11.     fileName = scrapy.Field() #文件夹名,每一个乐谱一个文件夹
  12.     path = scrapy.Field() #图片存储路径(绝对路径)
  13.     path2 = scrapy.Field()#简介的绝对路径
复制代码

pipe文件
  1. import requests
  2. import sys
  3. from op.items import OpItem





  4. class OpPipeline(object):
  5.     def process_item(self, item, spider):
  6.         detailURL = item['detailURL']
  7.         path = item['path']
  8.         image = requests.get(detailURL)
  9.         f = open(path, 'wb')
  10.         f.write(image.content)
  11.         f.close()
  12.         content = item['content']
  13.         filename_path = item['path2']
  14.         with open(filename_path, 'w', encoding='utf-8') as f:
  15.             f.write(content + "\n")
  16.         return item


复制代码

从4点钟运行到现在13点,没断过。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-6 13:02:12 | 显示全部楼层
怎么把帖子设为已解决啊????
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-10 05:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表