gpt4 book ai didi

python - Scrapy 未获取日期作为项目参数

转载 作者:行者123 更新时间:2023-12-01 04:10:19 24 4
gpt4 key购买 nike

我有这段代码,我想添加当前日期作为项目参数,我在网上找到了不同的解决方案,但我不断收到 TypeError: 'str' object is not callable 或 TypeError: 'ItemMeta' object does不支持项目分配

我的代码是这样的:

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

最佳答案

TypeError: 'str' object is not callable

您不应该调用datascrape - 它是一个字符串:

item['dataoggi'] = datascrape

TypeError: 'ItemMeta' object does not support item assignment

这意味着您正在尝试将字段添加到 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]

关于python - Scrapy 未获取日期作为项目参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com