Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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_Python 3.x_Python Requests - Fatal编程技术网

类型错误:';非类型';对象在python中不可使用请求

类型错误:';非类型';对象在python中不可使用请求,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,代码: 产品数据=[] 将open(“search\u results\u url.txt”,“r”)作为URL列表,将open(“search\u results\u output.jsonl”,“w”)作为输出文件: 对于url列表中的url.read().splitlines(): 数据=刮取(url) 如果数据: 对于数据中的产品[‘产品’]: 如果数据['products']为无: 持续 其他: 产品['search_url']=url 打印(“保存产品:%s”%Product['ti

代码:

产品数据=[]
将open(“search\u results\u url.txt”,“r”)作为URL列表,将open(“search\u results\u output.jsonl”,“w”)作为输出文件:
对于url列表中的url.read().splitlines():
数据=刮取(url)
如果数据:
对于数据中的产品[‘产品’]:
如果数据['products']为无:
持续
其他:
产品['search_url']=url
打印(“保存产品:%s”%Product['title'])
json.dump(产品,输出文件)
outfile.write(“\n”)
#睡眠(5)
错误:
回溯(最近一次呼叫最后一次):
文件“searchresults.py”,第43行,在
对于数据中的产品[‘产品’]:
TypeError:“非类型”对象不可编辑
如何为数据中的产品解决此问题。get('products',[]): ...
当products为None时,将传递一个空列表。空列表是可编辑的,因此不会抛出错误,但不会发生任何事情。

正如错误所示,您的
数据['product']
尝试在
数据=刮取(url)
之后打印数据值,看起来
数据['product']
是无的,因此您会收到错误。或者您可以添加额外的检查,如
if data和data['products']
product_data = []
with open("search_results_urls.txt",'r') as urllist, open('search_results_output.jsonl','w') as outfile:
    for url in urllist.read().splitlines():
        data = scrape(url) 
        if data:
            for product in data['products']:
              if data['products'] is None:
                 continue
              else:
                  product['search_url'] = url
                  print("Saving Product: %s"%product['title'])
                  json.dump(product,outfile)
                  outfile.write("\n")
                  # sleep(5)
        

Error :
Traceback (most recent call last):
  File "searchresults.py", line 43, in <module>
    for product in data['products']:
TypeError: 'NoneType' object is not iterable