如果.find等于无Python 3,则给出默认值

时间:2017-09-23 16:08:33

标签: python string python-3.x beautifulsoup

我正在试图刮掉亚马逊并且试图获取页面中任何商品的价格,事实上并非亚马逊页面中的所有商品都有价格,所以有时它可能会使其等于无

import requests
from bs4 import BeautifulSoup
import itertools

def spider(max_pages):
    search = str(input("Search whatever you want and I'll find it on Amazon "))
    print("\n")
    page = 1
    while page <= max_pages:
    url = "https://www.amazon.it/s/ref=sr_pg_"+ str(page) + "?rh=n%3A425916031%2Ck%3A" + search + "&page="+ str(page) + "&sort=relevancerank&keywords=" + search
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, "html.parser")
    for link in soup.findAll("a", {"class": "s-access-detail-page"}):
        href = link.get("href")
        title = link.string
        print(title)
        print(single_Data(href))
        print(href)
    page += 1

def single_Data(item_url):
    source_code = requests.get(item_url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, "html.parser")
    priceog= (soup.find("span", {"id": "priceblock_ourprice"}))
    price_in = priceog.string
    return price_in

spider(1)

错误结束

AttributeError: 'NoneType' object has no attribute 'string'

我还习惯用这样的for循环运行single_Data的价格

def single_Data(item_url):
source_code = requests.get(item_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for item_price in soup.findAll("a", {"class": "a-link-normal"}):
    price_in= item_price.string
    return price_in

那么如果找不到任何

,我该怎么设置呢?
("span", {"id": "priceblock_ourprice"})

它不必提出错误或写“无”#39;但是给我的变量price_in我想要的字符串值:&#34;这个项目目前没有价格&#34;。

谢谢xx

2 个答案:

答案 0 :(得分:0)

注意到您的错误消息,我们可以看到当商品没有价格时,item_price ==无。因此,您只需在“price_in = item_price.string”上方添加一条if语句,以检查它是否为None,如果是,则设置price_in =“some phrase”

def single_Data(item_url):
    source_code = requests.get(item_url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, "html.parser")
    for item_price in soup.findAll("a", {"class": "a-link-normal"}):
        if item_price:
            price_in = item_price.string
        else:
            price_in = "There is currently no price for this item"
    return price_in

答案 1 :(得分:0)

def single_Data(item_url): 
source_code = requests.get(item_url) 
plain_text = source_code.text 
soup = BeautifulSoup(plain_text, "html.parser") 
for item_price in soup.findAll("a", {"class": "a-link-normal"}): 
    try:
        price_in = item_price.string
    except AttributeError:
        price_in = "price not found"
return price_in

其中一个蟒蛇咒语就是要求宽恕而不是许可是很容易的。尝试提取字符串,如果无法获取字符串,则返回默认值。

相关问题