python - 赋值之前引用的局部变量 'price_end'

标签 python sql error-handling web-crawler

我遇到了一个小问题。这是我的代码:

front_deeplink = ("http://www.sozi.com")

user_agent = {'User-agent': 'Chrome/43.0.2357.124'}

Region = "turkey/istanbul"   


def trade_spider(max_pages):
    page = 0
    partner_ID = Yes
    location_ID = No

    try:
        connection = mysql.connector.connect\
            (host = "localhost", user = "root", passwd ="", db = "local")
    except:
        print("Keine Verbindung zum Server")
        sys.exit(0)

    cursor = connection.cursor()

    cursor.execute("DELETE from prices_crawled where LocationID=" + str(location_ID) + " and PartnerID=" + str(partner_ID))
    connection.commit()

    while page <= max_pages:
        page += 1
        r = requests.get("http://www.zosi.com/things-to-do/" + str(Region) + "/?per_page=10&page=" + str(page))
        soup = BeautifulSoup(r.content)


        g_data = soup.find_all("div", {"class": "column info"})

        for item in g_data:
            Header = item.find_all("a")
            for t in set(t.get("data-product-name") for t in Header):
                Header_final = t
            price = item.find_all("div", {"class": "price"})
            Price_final = (price[0].text.strip()[8:])
            if Price_final:
                price_end = int(float(Price_final)*100*Change)
            Deeplink = item.find_all("a")
            for j in set(j.get("href") for j in Deeplink):
                Deeplink_final = (str(front_deeplink) + j)
            Language = "Englisch"


            print("Header: " + Header_final + " | " + "Price: " + str(price_end) + " | " + "Deeplink: " + Deeplink_final + " | " + "PartnerID: " + str(partner_ID) + " | " + "LocationID: " + str(location_ID)+ " | " + "Language: " + Language)

            cursor.execute('''INSERT INTO local2 (price_id, Header,  Price, Deeplink, PartnerID, LocationID, Language) \
                    VALUES(%s, %s, %s, %s, %s, %s, %s)''', ['None'] + [Header_final] + [price_end] + [Deeplink_final] + [partner_ID] + [location_ID] + [Language])

            connection.commit()



    cursor.close()
    connection.close()
trade_spider(8)

Outcome:
File "C:/Users/hmattu/PycharmProjects/untitled1/grayline.com.py", line 84, in <module>
trade_spider(8)
  File "C:/Users/hmattu/PycharmProjects/untitled1/grayline.com.py", line 66, in trade_spider
    print("Header: " + Header_final + " | " + "Price: " + str(price_end) + " | " + "Deeplink: " + Deeplink_final + " | " + "PartnerID: " + str(partner_ID) + " | " + "LocationID: " + str(location_ID)+ " | " + "Language: " + Language)
UnboundLocalError: local variable 'price_end' referenced before assignment

当我启动程序时,问题就开始了。它打开控制台,但立即退出。它也不输出任何信息。
我不太确定发生了什么,如果有人可以帮助我,我会非常感激。

最佳答案

实际上,仅当price_endtruthy时,才为Price_final分配一个值。一种解决方案是自己引发Exception或分配一个字符串,该字符串可以提供一些信息。

if Price_final:
    price_end = int(float(Price_final)*100*Change)
else:
    raise ValueError('No Price can be evaluated')
    #or price_end = 'No price can be evaluated'

关于python - 赋值之前引用的局部变量 'price_end',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948840/

相关文章:

python - 如何使用 Python 可视化 3D 体积?

Python 排序数据 for 循环和 if 语句

SQL,在表中选择

sql - 每个数据库运行时间最长的查询

javascript - Sendgrid node.js 错误 535 错误的用户名/密码

javascript - 在 fetch() 函数中捕获错误

Python heapq heappush 多元素数组真值不明确使用 a.any() 或 a.all()

mysql - 查找仅属于特定办公室的相关邮政编码

c++ - 区分读取循环中的失败和文件结尾

python - 使用 sqlalchemy 的 row_to_json 语法