Scrapy爬虫学习,及实践项目。

作为初学者,首先贴出自己看到的一个教程所提供的实例。。后边会讲解我自身所完成的项目说明。

我自己所做项目下载地址为:Scrapy爬虫项目

自己项目说明:

爬取某网站流行时尚网页项目,,并对具体项目内容进行二次爬取,将爬取到的内容拼接成为新的静态html,存入自身Ftp服务器,并将信息提交到某接口。。(接口中进行数据操作。接口部分未上传)定时爬取。。定义.sh文件,并将文件加入定时任务。#! /bin/shexport PATH=$PATH:/usr/local/appscript/testcd /usr/local/appscript/test/hichaoTopicnohup scrapy crawl hichaotopic示例

scrapy爬取了链接之后,如何继续进一步爬取该链接对应的内容?parse可以返回Request列表,或者items列表,如果返回的是Request,则这个Request会放到下一次需要抓取的队列,如果返回items,则对应的items才能传到pipelines处理(或者直接保存,如果使用默认FEED exporter)。那么如果由parse()方法返回下一个链接,那么items怎么返回保存? Request对象接受一个参数callback指定这个Request返回的网页内容的解析函数(实际上start_urls对应的callback默认是parse方法),所以可以指定parse返回Request,然后指定另一个parse_item方法返回items:

以爬取南京大学bbs为例:

1. spider下的文件:

# -*- coding: utf-8 -*-import chardetfrom scrapy.spider import BaseSpiderfrom scrapy.selector import HtmlXPathSelectorfrom scrapy.utils.url import urljoin_rfcfrom scrapy.http import Requestfrom tutorial.items import bbsItemclass bbsSpider(BaseSpider):name = "boat"allowed_domains = ["bbs.nju.edu.cn"]start_urls = [""]def parseContent(self,content):content = content[0].encode('utf-8')#print chardet.detect(content)#print contentauthorIndex =content.index('信区')author = content[11:authorIndex-2]boardIndex = content.index('标 题')board = content[authorIndex+8:boardIndex-2]timeIndex = content.index('南京大学小百合站 (')time = content[timeIndex+26:timeIndex+50]return (author,board,time)#content = content[timeIndex+58:]#return (author,board,time,content)def parse2(self,response):hxs =HtmlXPathSelector(response)item = response.meta['item']items = []content = hxs.select('/html/body/center/table[1]/tr[2]/td/textarea/text()').extract()parseTuple = self.parseContent(content)item['author'] = parseTuple[0].decode('utf-8')item['board'] =parseTuple[1].decode('utf-8')item['time'] = parseTuple[2]#item['content'] = parseTuple[3]items.append(item)return itemsdef parse(self, response):hxs = HtmlXPathSelector(response)items = []title= hxs.select('/html/body/center/table/tr[position()>1]/td[3]/a/text()').extract()url= hxs.select('/html/body/center/table/tr[position()>1]/td[3]/a/@href').extract()for i in range(0, 10):item = bbsItem()item['link'] = urljoin_rfc('', url[i])item['title'] = title[i][:]items.append(item)for item in items:yield Request(item['link'],meta={'item':item},callback=self.parse2)2. pipelines文件:# -*- coding: utf-8 -*-# Define your item pipelines here# Don’t forget to add your pipeline to the ITEM_PIPELINES setting# See: from scrapy import logfrom twisted.enterprise import adbapifrom scrapy.http import Requestfrom scrapy.exceptions import DropItemfrom scrapy.contrib.pipeline.images import ImagesPipelineimport timeimport MySQLdbimport MySQLdb.cursorsimport socketimport selectimport sysimport osimport errnoclass MySQLStorePipeline(object):def __init__(self):self.dbpool = adbapi.ConnectionPool(‘MySQLdb’,db = ‘test’,user = ‘root’,passwd = ‘root’,cursorclass = MySQLdb.cursors.DictCursor,charset = ‘utf8’,use_unicode = False)def process_item(self, item, spider):query = self.dbpool.runInteraction(self._conditional_insert, item)return itemdef _conditional_insert(self, tx, item):tx.execute(‘insert into info values (%s, %s, %s)’, (item[‘author’], item[‘board’], item[‘time’]))

版权声明:本文为博主原创文章,未经博主允许不得转载。

年轻是我们唯一拥有权利去编织梦想的时光

Scrapy爬虫学习,及实践项目。

相关文章:

你感兴趣的文章:

标签云: