我有这个代码,我想将当前日期作为项目参数添加,我在网上找到了不同的解决方案,但我一直得到TypeError:' str' object不可调用或TypeError:' ItemMeta'对象不支持项目分配
我的代码是:
datascrape = time.strftime("%d/%m/%Y") #also tried str(datetime.datetime.today())
item['dataoggi'] = datascrape() #i tried also datascrape without ()
我如何得到作为一个项目参数的刮刮日期?
更新的代码:
from bot.items import botitem
import time
class NetbotSpider(scrapy.Spider):
name = "netbot"
allowed_domains = ["example.com"]
start_urls = (
'http://example.com
)
def parse(self, response):
stati = response.xpath('/html/body//div/table/tbody//tr//td//img//@title').extract()
numeri = response.xpath('/html/body//div/table/tbody//tr[2]//td/text()').extract()
for i in range(1, len(stati)):
item = botitem()
datascrape = time.strftime("%d/%m/%Y")
botitem['dataoggi'] = datascrape
botitem['state'] = stati[i]
botitem['number'] = numeri[i]
print botitem
答案 0 :(得分:2)
TypeError:'str'对象不可调用
你不应该打电话给datascrape
- 这是一个字符串:
item['dataoggi'] = datascrape
TypeError:'ItemMeta'对象不支持项目分配
这意味着您正在尝试向Item类添加字段,而不是实例。替换:
item = botitem()
datascrape = time.strftime("%d/%m/%Y")
botitem['dataoggi'] = datascrape
botitem['state'] = stati[i]
botitem['number'] = numeri[i]
使用:
item = botitem()
datascrape = time.strftime("%d/%m/%Y")
item['dataoggi'] = datascrape
item['state'] = stati[i]
item['number'] = numeri[i]
答案 1 :(得分:0)
我认为botitem
是一个类对象,因此必须首先声明它。您可以定义如下所示:
from bot.items import botitem
import time
class NetbotSpider(scrapy.Spider):
name = "netbot"
allowed_domains = ["example.com"]
start_urls = (
'http://example.com'
)
botitem=botitem()
def parse(self, response):
stati=response.xpath('/html/body//div/table/tbody//tr//td//img//@title').extract()
numeri = response.xpath('/html/body//div/table/tbody//tr[2]//td/text()').extract()
for i in range(1, len(stati)):
datascrape = time.strftime("%d/%m/%Y")
botitem['dataoggi'] = datascrape
botitem['state'] = stati[i]
botitem['number'] = numeri[i]
print botitem