所以我已经和deq玩过abit,并且几乎达到了终点,但人们担心由于deq的长度,它已经打印了8次,而我只想打印一次。
我所做的是:
old_list = []
deq = deque(old_list, maxlen=8)
url = 'https://www.supremecommunity.com/restocks/eu/'
while True:
try:
new_list = []
bs4 = soup(requests.get(url).text, "html.parser")
for item in bs4.findAll('div', {'class': 'restock-item'}):
if item.find('div', {'class': 'user-detail'}):
name = item.find('h5', {'class': 'handle restock-name'}).string
color = item.find('h6', {'class': 'restock-colorway'}).string
new_list.append(name + color)
for newitem in new_list:
if newitem not in deq:
print(name)
print(color)
deq.append(newitem)
else:
print('Sleeping 5 sec')
time.sleep(5)
except:
continue 基本上,它检查网站并打印出名称和颜色,然后将其添加到deq列表中。但是,由于maxlen=8和我的问题,输出输出了8次相同的名称和颜色:
我怎么做才能印出来一次?
发布于 2018-08-23 10:09:31
您总是打印相同的变量name和color,就像它们在上面的for-loop中定义的一样。
name = item.find('h5', {'class': 'handle restock-name'}).string
color = item.find('h6', {'class': 'restock-colorway'}).string当您在第二个print(name) -loop中打印for和-loop时,它总是引用name和color所拥有的最后一个值。
要解决这个问题,您应该参考打印语句中的变量newitem。
编辑:
在这里,您只是连接这两个字符串。
new_list.append(name + color)我建议你把它列成一个清单。
new_list.append([name,color])然后可以使用print(newitem[0])和print(newitem[1])打印不同的名称和颜色。
https://stackoverflow.com/questions/51983062
复制相似问题