【发布时间】:2021-03-31 04:56:52
【问题描述】:
我想用价格和 SKU 制作新的 XML 提要,有一些变体(防止 NULL 值),主要我们从 Google 表格的第 7 列中获取值,但如果我们在表格中没有产品,那么我们将获取价格从主 XML 提要并使用它,一切正常,但一段时间后我收到错误:ConnectionResetError: [WinError 10054] 实际连接已由远程主机结束(我已尝试翻译成英文)。
我也尝试过一段时间睡眠,但我仍然收到此错误..
import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET
import gspread
import time
from oauth2client.service_account import ServiceAccountCredentials
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('lorde-mall-911b82a06953.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("OH-Product-Listings").worksheet('Product_listing_UK')
url = 'https://feed.lordemall.cz/cgi-bin/obchodhracek.xml'
uh = urllib.request.urlopen(url)
tree = ET.parse(uh)
root = tree.getroot()
data = ET.Element('SHOP')
for r in root.findall('SHOPITEM'):
sku = r.find('PRODUCTNO').text
try:
item = sheet.find(sku)
row = item.row
time.sleep(2)
price = sheet.cell(row, 7).value
except gspread.exceptions.CellNotFound:
price = r.find('PRICE_VAT').text
element2 = ET.SubElement(data, 'SHOPITEM')
s_elem2_1 = ET.SubElement(element2, 'PRICE_VAT')
s_elem2_2 = ET.SubElement(element2, 'PRODUCTNO')
s_elem2_1.text = price
s_elem2_2.text = sku
xml_content = ET.tostring(data)
with open('OH_GBP.xml', 'wb') as f:
f.write(xml_content)
f.close()
time.sleep(3)
【问题讨论】:
标签: python python-3.x google-sheets google-drive-api