我正在尝试将网站上的信息放入小部件中,它可以工作,但是我想让它'发现'的每段文字都显示在新行上。我试过+'/ n'和+''''''),但出现相同的错误。
def Supplies_scraper():
if instorechoices1.counter !=1:
url='(url)'
web_page=urlopen(url)
html_code=web_page.read().decode("UTF-8")
web_page.close()
products = (findall('''data-desc =(.*?)data-price =''',html_code))[:5],
shopping_list.delete(0.0, END)
shopping_cart.delete(0.0, END)
if products:
shopping_list.insert(0.0, products + '/n')
shopping_list.insert(0.0, '''Source: (URL)
''')
shopping_list.insert(0.0,'''--------{ Best Health Supplies }-------
''')
elif instorechoices1.counter ==1:
shopping_list.delete(0.0, END)
shopping_list.insert(END, 'Due to shortages there is only 1 of each item category per order'+ '\n')
if instorechoices1.counter != 2:
instorechoices1.counter +=1
注意,您在此行中有尾随,
products = (findall('''data-desc =(.*?)data-price =''',html_code))[:5],
这意味着在products
为tuple
之后,尝试将具有str的元组或具有元组的str并置的尝试会导致
TypeError: can only concatenate tuple (not "str") to tuple
您的代码中是否有尾随,
?如果没有将其删除,请检查其类型,例如通过执行以下操作:
products = (findall('''data-desc =(.*?)data-price =''',html_code))[:5]
print(type(products))
如果为str
,则可以将其与\n
并置;如果为tuple
或list
,则如果list
或tuple
的所有元素均为str
,则需要先将其转换为str ] s,您可以通过以下方式使用.join
:
my_tuple = ('1','2','3')
my_list = ['1','2','3']
joined_tuple = ','.join(my_tuple)
joined_list = ','.join(my_list)
print(joined_tuple) # 1,2,3
print(joined_list) # 1,2,3