我对这只蜘蛛已经有了很大的进步-我只是越来越习惯于编码,并享受它的每一分钟。然而,随着我的学习,我的大部分编程都是解决问题的。以下是我当前的错误:
我的蜘蛛在终端窗口显示我想要的所有数据。当我去输出时,什么也没显示出来。这是我的密码。在
import re
import json
from urlparse import urlparse
from scrapy.selector import Selector
try:
from scrapy.spider import Spider
except:
from scrapy.spider import BaseSpider as Spider
from scrapy.utils.response import get_base_url
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
from scrapy.selector import HtmlXPathSelector
from database.items import databaseItem
from scrapy.log import *
class CommonSpider(CrawlSpider):
name = 'fenders.py'
allowed_domains = ['usedprice.com']
start_urls = ['http://www.usedprice.com/items/guitars-musical-instruments/fender/?ob=model_asc#results']
rules = (
Rule(LinkExtractor(allow=( )), callback='parse_item'),
)
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
item = []
data = hxs.select('//tr[@class="oddItemColor baseText"]')
tmpNextPage = hxs.select('//div[@class="baseText blue"]/span[@id="pnLink"]/a/@href').extract()
for attr in data:
#item = RowItem()
instrInfo = attr.select('//td[@class="itemResult"]/text()').extract()
print "Instrument Info: ", instrInfo
yield instrInfo
正如JoeLinux所说,您正在生成一个字符串,而不是返回该项。如果你大部分时间都在学习教程,你可能会有一个“项目.py“文件到某个地方(也许是其他名称),在那里你的项目被定义-它似乎被称为”RowItem()”。这里有几个字段,或者只有一个字段。在
您需要做的是找出如何在项中存储数据。所以,做一个粗略的假设,您可能希望RowItem()包含一个名为instrInfo的字段。所以你的项目.py文件可能包含以下内容:
那么你的蜘蛛应该包括以下内容:
^{pr2}$这会将项目发送到您的管道进行处理。在
正如我所说,一些关于你要做什么的粗略假设,以及你的信息格式,但希望这能让你开始。在
另外,打印功能可能不需要。当返回项目时,它会在spider运行时显示在终端(或日志)中。在
祝你好运!在
相关问题 更多 >
编程相关推荐