Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从响应元获取数据或从项目加载器获取数据。哪个更好?_Python_Xpath_Web Scraping_Scrapy - Fatal编程技术网

Python 从响应元获取数据或从项目加载器获取数据。哪个更好?

Python 从响应元获取数据或从项目加载器获取数据。哪个更好?,python,xpath,web-scraping,scrapy,Python,Xpath,Web Scraping,Scrapy,我已经编写了一个spider,在这里我通过meta将一些数据从父函数发送到子函数。在子函数中,我正在解析这些项。但如果在response.meta.get('name')中找不到一些数据,我将在子函数中编写一些xpath 这两种方法中哪一种更好 (一) (二) 为了避免设置和获取项目值(代码片段1)的额外工作,我首先从meta获取值,然后检查它: value = response.meta.get("name") if not value: item.add_xpath("name",

我已经编写了一个spider,在这里我通过meta将一些数据从父函数发送到子函数。在子函数中,我正在解析这些项。但如果在
response.meta.get('name')中找不到一些数据,我将在子函数中编写一些xpath

这两种方法中哪一种更好

(一)

(二)


为了避免设置和获取项目值(代码片段1)的额外工作,我首先从
meta
获取值,然后检查它:

value = response.meta.get("name")
if not value:
    item.add_xpath("name", "xpath")
else:
    item.add_value("name", value)
if response.meta.get('name', ''):
    item.add_value('name', response.meta.get('name'))
else:
    item.add_xpath('name', 'xpath')
value = response.meta.get("name")
if not value:
    item.add_xpath("name", "xpath")
else:
    item.add_value("name", value)