I read a few other answers here, but I missed something fundamental. I am trying to extract images from a site using CrawlSpider.
settings.py
BOT_NAME = 'healthycomm'
SPIDER_MODULES = ['healthycomm.spiders']
NEWSPIDER_MODULE = 'healthycomm.spiders'
ITEM_PIPELINES = {'scrapy.contrib.pipeline.images.ImagesPipeline': 1}
IMAGES_STORE = '~/Desktop/scrapy_nsml/healthycomm/images'
items.py
class HealthycommItem(scrapy.Item):
page_heading = scrapy.Field()
page_title = scrapy.Field()
page_link = scrapy.Field()
page_content = scrapy.Field()
page_content_block = scrapy.Field()
image_url = scrapy.Field()
image = scrapy.Field()
HealthycommSpider.py
class HealthycommSpiderSpider(CrawlSpider):
name = "healthycomm_spider"
allowed_domains = ["healthycommunity.org.au"]
start_urls = (
'http://www.healthycommunity.org.au/',
)
rules = (Rule(SgmlLinkExtractor(allow=()), callback="parse_items", follow=False), )
def parse_items(self, response):
content = Selector(response=response).xpath('//body')
for nodes in content:
img_urls = nodes.xpath('//img/@src').extract()
item = HealthycommItem()
item['page_heading'] = nodes.xpath("//title").extract()
item["page_title"] = nodes.xpath("//h1/text()").extract()
item["page_link"] = response.url
item["page_content"] = nodes.xpath('//div[@class="CategoryDescription"]').extract()
item['image_url'] = img_urls
item['image'] = ['http://www.healthycommunity.org.au' + img for img in img_urls]
yield item
I am not very familiar with Python in general, but I feel like I am missing something very simple here.
Thanks Jamie
source
share