我写了一个蜘蛛从网站下载数据并按照链接获取详细数据。 蜘蛛还使用默认的scrapy图像管道下载图像。到目前为止一切正常。
但是当我第二次启动蜘蛛[使用另一个搜索词]时,图像下载不再起作用了。爬行的工作就像应该的那样。我没有任何错误。
这是蜘蛛:
class DiscoSpider(BaseSpider):
def __init__(self, query):
super( BaseSpider, self ).__init__()
self.name = "discogs"
self.allowed_domains = ["discogs.com"]
self.start_urls = [
"http://www.discogs.com/search?q=%s&type=release" % query
]
# parse all releases for the current search
def parse(self, response):
logging.debug('scrapy.parse')
hxs = HtmlXPathSelector(response)
li = hxs.select("//div[@id='page_content']/ol/li")
items = []
for l in li:
item = DiscogsItem()
...
# get the link for the callback for the tracklist
link = l.select("a/@href").extract()[0]
item['link'] = '' if link == None else link
# get the img location
img = l.select("a/img/@src").extract()
item['image_urls'] = [None] if img == None else img
# get the url for the tracklist callback
url = urlparse.urljoin('%s%s' % ('http://www.', self.allowed_domains[0]), link)
# request and callback to get tracklist for release
item = Request(url, meta={'item':item}, callback=self.parse_tracklist)
items.append(item)
yield item
# callback to get the tracklist for each release
def parse_tracklist(self, response):
item = response.request.meta['item']
hxs = HtmlXPathSelector(response)
rows = hxs.select("//div[@class='section_content']/table[@class='playlist mini_playlist']/tr")
tracklist = []
for row in rows:
track = {}
title = row.select("td[@class='track']/span[@class='track_title']/text()").extract()
track['title'] = '' if title in [None, '', []] else self.clean_track(title[0])
...
tracklist.append(track)
item['tracklist'] = tracklist
yield item
这是项目:
class DiscogsItem(Item):
# define the fields for your item here like:
link = Field()
artist = Field()
release = Field()
label = Field()
year = Field()
tracklist = Field()
image_urls = Field()
images = Field()
thumb = Field()
在我的scrapy-settings中:
ITEM_PIPELINES = ['scrapy.contrib.pipeline.images.ImagesPipeline']
IMAGES_STORE = '/home/f/work/py/discogs/tmp'
CONCURRENT_REQUESTS = 100
CONCURRENT_REQUESTS_PER_IP = 20
IMAGES_EXPIRES = 0
我在一个单独的过程中从PyQt-UI运行蜘蛛,我是Scrapy / PyQT / StackOverflow的新手(很抱歉格式错误)。
我使用的是Python 2.7,PyQt4和Scrapy 0.12.0.2546的Xubuntu 12.04盒子。
有谁知道为什么第二张图片下载不起作用?
提前致谢。
答案 0 :(得分:0)
我现在回答我自己的问题,虽然我真的不知道问题是什么。 我所做的是改变蜘蛛的构造函数,如下所示:
class DiscogsSpider(CrawlSpider):
name = "discogs"
allowed_domains = ["discogs.com"]
def __init__(self, query):
super( DiscogsSpider, self ).__init__()
self.start_urls = [
"http://www.discogs.com/search?q=%s&type=release" % query
]
我现在也扩展了CrawlSpider而不是BasSpider。图像下载现在按预期工作。
感谢。