【发布时间】:2020-04-06 11:13:50
【问题描述】:
例如,在这个 python 代码中。
from scrapy.loader import ItemLoader
from myproject.items import Product
def parse(self, response):
l = ItemLoader(item=Product(), response=response)
l.add_xpath('name', '//div[@class="product_name"]')
l.add_xpath('name', '//div[@class="product_title"]')
l.add_xpath('price', '//p[@id="price"]')
l.add_css('stock', 'p#stock]')
l.add_value('last_updated', 'today') # you can also use literal values
return l.load_item()
如何避免使用 'name', 'price' 作为字符串。 有什么方法可以使用,比如
l.add_xpath(getname(Product.name), '//div[@class="product_title"]')
l.add_xpath((getname(Product.price), '//p[@id="price"]')
谢谢
【问题讨论】:
-
为什么?如果您打错字,Scrapy Item 实例将引发异常。这只是为了在 IDE 中获得自动完成支持,还是其他什么?
标签: python scrapy magic-string