我写了蜘蛛从网站中提取图像。但items.json文件只有[中的字符。 请帮我。 我的蜘蛛文件是: -
# -*- coding: utf-8 -*-
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import HtmlXPathSelector
from captcha.items import CaptchaItem
class CaptchaSpider(CrawlSpider):
name = "CaptchaSpider"
allowed_domains = ["*****.ac.in"]
start_urls = [
"https://*****.ac.in/*****.asp"
]
def parse_item(self, response):
item = CaptchaItem()
hxs = HtmlXPathSelector(response)
item['im'] = hxs.select('//img/@src').extract()
return item
我的items.py文件是这样的: -
import scrapy
class CaptchaItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
im = scrapy.Field()
pass
答案 0 :(得分:1)
如果抓取时出现错误,或者没有返回任何商品,则输出文件将仅包含[
。
在你的情况下,由于缩进,parse_item()
应该缩进:
class CaptchaSpider(CrawlSpider):
name = "CaptchaSpider"
allowed_domains = ["*****.ac.in"]
start_urls = [
"https://*****.ac.in/*****.asp"
]
def parse_item(self, response):
item = CaptchaItem()
hxs = HtmlXPathSelector(response)
item['im'] = hxs.select('//img/@src').extract()
return item
我实际上已经测试并复制了它:
$ scrapy runspider spider.py -o items.json
...
$ cat items.json
[