Python 在scrapy中连接已刮取的数据
如何连接数据,使我的代码类似于我们NJ Camden:Python 在scrapy中连接已刮取的数据,python,scrapy,Python,Scrapy,如何连接数据,使我的代码类似于我们NJ Camden: def parse_items(self, response): hxs = HtmlXPathSelector(response) loc_Con = hxs.select('/tr/td[2]/span/span/span') #for country loc_Reg = hxs.select('/tr/td[2]/span/span') #for region loc_Loc = hxs.sel
def parse_items(self, response):
hxs = HtmlXPathSelector(response)
loc_Con = hxs.select('/tr/td[2]/span/span/span') #for country
loc_Reg = hxs.select('/tr/td[2]/span/span') #for region
loc_Loc = hxs.select('//tr[3]/td[2]/span/span') #for local
items = []
for titles in titles:
item = somethingItem()
item ["country"] = loc_Con.select('text()').extract()
item ["region"] = loc_Reg.select('text()').extract()
item ["location"] = loc_Loc.select('text()').extract()
item ["code"] = #["country"]-item ["region"]-item ["location"] like the above example
按以下方式使用format()
:
item["code"] = "{}-{}-{}".format(item["country"], item["region"], item["location"])
item["code"] = "{country}-{region}-{location}".format(country=item["country"],
region=item["region"],
location=item["location"])
或者这样:
item["code"] = "{}-{}-{}".format(item["country"], item["region"], item["location"])
item["code"] = "{country}-{region}-{location}".format(country=item["country"],
region=item["region"],
location=item["location"])
或旧式格式:
item["code"] = "%s-%s-%s" % (item["country"], item["region"], item["location"])
UPD:
并且,不要忘记从提取的列表中获取第0项:
item ["country"] = loc_Con.select('text()').extract()
item ["region"] = loc_Reg.select('text()').extract()
item ["location"] = loc_Loc.select('text()').extract()
item ["country"] = item ["country"][0] if item ["country"] else ""
item ["region"] = item ["region"][0] if item ["region"] else ""
item ["location"] = item ["location"][0] if item ["location"] else ""
如果我只想在我的csv中添加条目[“code”],我应该如何编写它呢?@chano然后不要在条目中定义
国家
、地区
和位置
,只需代码
。它会在条目中不包含country=Field()的情况下运行吗?是的,顺便说一句,如果您不需要这些字段,请将它们从somethingItem
类中删除。尝试了该格式,但在检查csv文件时,我看到代码的结果为[u'US']-[u'NJ']-[u'Camden']