简体   繁体   English

从列表中删除元素

[英]Removing elements from lists

i'm having trouble removing elements from lists .我无法从lists中删除元素。 When an e-mail is send I want to remove from urls[] and prices[] the relative elements.发送e-mail时,我想从urls[]prices[]中删除相关元素。

Ex.前任。 if email has: like a url an Iphone X and like a price 500€ and it had been sent, i want to "rewrite" the elements in urls[] and prices[] lists removing iphone's url and 500€ if email has: like a url an Iphone X and like a price 500€ and it had been sent, i want to "rewrite" the elements in urls[] and prices[] lists removing iphone's url and 500€

import requests
from bs4 import BeautifulSoup
import smtplib
import time

#   https://www.amazon.it/Corsair-Vengeance-Memorie-Desktop-Prestazioni/dp/B0143UM4TC
#   https://www.amazon.it/AMD-Ryzen-5-3600-Processori/dp/B07STGGQ18
#   https://www.amazon.it/Apple-iPhone-Grigio-Siderale-Ricondizionato/dp/B07985C44N

urls = []
prices=[]
all_product = []
n = int(input("Inserisci il numero di prodotti: "))

#agginge il link da controllare
print("\nInserisci i link:")
for i in range(0, n): 
    link = str(input()) 
    urls.append(link)    

#aggiunge il realtivi prezzi ai link
print("\nInserisci i prezzi:")
for i in range(0, n): 
    money = int(input()) 
    prices.append(money) 

#headers per i diversi motori di ricerca
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0 Chrome/83.0.4103.97 Safari/537.36'}

def check_price():
    for url, price in zip(urls, prices):
        soup  = BeautifulSoup(requests.get(url, headers=headers).content, 'lxml')
        title = soup.find(id='productTitle').get_text(strip=True)    
        try:
            products = soup.find(id='priceblock_ourprice').get_text()
            fix_string = products.replace(",", ".")      
            converted_price = float(fix_string[0:5])
            all_product.append(converted_price)
            money_saved=converted_price-price
            if (converted_price<=price): 
                 #send email
                remove_link=str(url)
                remove_price=price
                if(urls.index(remove_link)&prices.index(remove_price)):
                    urls.pop((urls.index(remove_link)))
                    prices.pop(prices.index(remove_price))
         except AttributeError:
            print ("Prezzo non trovato, controlla se il prodotto ha un prezzo esposto")
    print(all_product)

In Python it is not good idea to remove from list which is used in for... in.... because when you remove element then other elements are moved (so next element is in place of removed element) but for doesn't know it and it jumps to next element on list and it skip element which was moved to place of removed element.在 Python 中,从用于for... in....的列表中删除不是一个好主意,因为当您删除元素时,其他元素会被移动(因此下一个元素代替已删除的元素)但for不会知道它,它会跳转到列表中的下一个元素,并跳过已移动到已删除元素位置的元素。

Better before loop create empty list ( keep_urls = [] ), inside loop append to this list elements which you want to keep ( keep_urls.append(url) ), and after loop assing this list to old variable ( urls = keep_urls ).最好在循环之前创建空列表( keep_urls = [] ),在循环 append 到要保留的列表元素( keep_urls.append(url) ),然后在循环之后将此列表分配给旧变量( urls = keep_urls )。 After that you can run it all again and it will use list without removed element.之后,您可以再次运行它,它将使用没有删除元素的列表。

This code shows how I see it.这段代码显示了我的看法。

BTW: because adding data using input() is long and borring so I added code which read data from files.顺便说一句:因为使用input()添加数据很长而且很无聊,所以我添加了从文件中读取数据的代码。

import requests
from bs4 import BeautifulSoup
import smtplib
import time

# --- functions ---

def ask_for_data():
    urls = []
    prices = []

    n = int(input("Inserisci il numero di prodotti: "))

    #agginge il link da controllare
    print("\nInserisci i link:")

    for i in range(n): 
        link = input()
        urls.append(link)    

    #aggiunge il realtivi prezzi ai link
    print("\nInserisci i prezzi:")

    for i in range(n): 
        money = input()
        prices.append(money) 

    return urls, prices

def read_data():
    with open('urls.txt') as fh:
        text = fh.read()
        urls = text.split('\n')

    with open('prices.txt') as fh:
        text = fh.read()
        prices = text.split('\n')

    return urls, prices

def write_data(urls, prices):
    with open('urls.txt', 'w') as fh:
        text = "\n".join(urls)
        fh.write(text)

    with open('prices.txt', 'w') as fh:
        text = "\n".join(prices)
        fh.write(text)

def send_email(url, price, converted_price):
    #money_saved = converted_price-price
    print('TODO: send mail with', url, price, converted_price)

# --- main ---

# - start -
#urls, prices = ask_for_data()
urls, prices = read_data()

#headers per i diversi motori di ricerca
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0 Chrome/83.0.4103.97 Safari/537.36'
}

while True:

    # - before loop -
    keep_urls = []
    keep_prices = []
    all_products = []

    # - loop -
    for url, price in zip(urls, prices):
        r = requests.get(url, headers=headers)
        #print(r.status_code)
        soup  = BeautifulSoup(r.content, 'lxml')
        title = soup.find(id='productTitle').get_text(strip=True)    
        try:
            products = soup.find(id='priceblock_ourprice').get_text()
            fix_string = products.replace(",", ".")      
            converted_price = float(fix_string[0:5])

            all_products.append(converted_price)

            if converted_price <= price: 
                send_email(url, price, converted_price)
            else:
                keep_urls.append(url)
                keep_prices.append(price)

         except AttributeError as ex:
            print('Ex:', ex)
            print("Prezzo non trovato, controlla se il prodotto ha un prezzo esposto")

    # - loop -
    urls = keep_urls
    prices = keep_prices

    print(all_products)

    time_sleep(60)

# - end -
write_data(urls, prices)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM