Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用列表_Python_Python 3.x_Web Scraping_Beautifulsoup - Fatal编程技术网

在python中使用列表

在python中使用列表,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,我有个新问题要问你。我的想法是检查第一个链接的价格是否等于所有价目表的价格。我使用>=查看发送了多少封电子邮件,它发送了7封邮件,而不是2封 我怎样才能解决这个问题 import requests from bs4 import BeautifulSoup import smtplib import time urls = ["https://www.amazon.it/Corsair-Vengeance-Memorie-Desktop-Prestazioni/dp/B0143UM4TC",

我有个新问题要问你。我的想法是检查第一个链接的价格是否等于所有价目表的价格。我使用>=查看发送了多少封电子邮件,它发送了7封邮件,而不是2封

我怎样才能解决这个问题

import requests
from bs4 import BeautifulSoup
import smtplib
import time

urls = ["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"]

prices =[90.70 , 90.20 , 50.80]
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'}
all_product = []

def check_price():
    for url in (urls):
        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)
            #print(all_product)
            for price in (prices):
                if (converted_price>=price): 
                    #send email
        except AttributeError:
            print ("Price not found, check if the product has an exposed price")
    print(all_product)

您的代码现在所做的是从每个URL获取价格,并根据价目表中的每个价格检查该价格

您希望它检查第n个URL和第n个价格。这可以通过以下方式实现:

将两个列表压缩在一起:

urls = [
    "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",
]

prices =[90.70, 90.20, 50.80]
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'}
all_product = []

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)
            if converted_price >= price: 
                # send email
        except AttributeError:
            print("Price not found, check if the product has an exposed price")

或者将价格添加到url列表中:

urls_and_prices = [
    ("https://www.amazon.it/Corsair-Vengeance-Memorie-Desktop-Prestazioni/dp/B0143UM4TC", 90.70),
    ("https://www.amazon.it/AMD-Ryzen-5-3600-Processori/dp/B07STGGQ18", 90.20),
    ("https://www.amazon.it/Apple-iPhone-Grigio-Siderale-Ricondizionato/dp/B07985C44N", 50.80),
]

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'}
all_product = []

def check_price():
    for url, price in urls_and_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)
            if converted_price >= price: 
                # send email
        except AttributeError:
            print("Price not found, check if the product has an exposed price")


我认为后者可能更适合您,因为它更容易从中看到成对的项目,因为它们位于同一行。

此代码检查第一个价格是否>=关于所有价格列表。这是因为您的内部循环遍历价格列表中的价格。您所需要的只是check_price函数中的一个循环。对于url,在zipurls中的价格,价格。