Github 鏈接
TypeError: 'NoneType' object is not iterable在被拋出之前,我一直在通過運行這段代碼獲得部分收益。但是,在拋出錯誤之前,我通過許多行寫入我的 csv 檔案。我相信這是由于迭代時不存在其中一個欄位名。有誰知道我可以添加什么來跳過這些欄位并繼續下一個專案?
with open("urls.txt",'r') as urllist, open('data.csv','w') as outfile:
writer = csv.DictWriter(outfile, fieldnames=["product_name","product_image","product_desc","product_company","product_country","product_type","product_abv","product_taste"],quoting=csv.QUOTE_ALL)
writer.writeheader()
for url in urllist.read().splitlines():
data = scrape(url)
if data:
for r in data['product']:
writer.writerow(r)
uj5u.com熱心網友回復:
也許嘗試默認產品密鑰?
if data:
for r in data.get('product', []):
# action
uj5u.com熱心網友回復:
嘗試這個:
with open("urls.txt",'r') as urllist, open('data.csv','w') as outfile:
writer = csv.DictWriter(outfile, fieldnames=["product_name","product_image","product_desc","product_company","product_country","product_type","product_abv","product_taste"],quoting=csv.QUOTE_ALL)
writer.writeheader()
for url in urllist.read().splitlines():
if not url:
continue
data = scrape(url)
if data and data['product']:
for r in data['product']:
writer.writerow(r)
uj5u.com熱心網友回復:
with open('urls.txt', 'r') as urllist:
for line in urls.txt:
if len(line) < 1:
pass
或者也許代替len(line)
if line == '':
pass
這對你有用嗎?當我第一次了解到您不一定要在打開的檔案上呼叫讀取函式時,我感到有點無知。除非我把它與 HTML 決議混淆......和 ??BeautifulSoup。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433953.html
上一篇:將CSV檔案讀入Pandas
