python - 类型错误 : not all arguments converted during string formatting

标签 python mysql sql database web-scraping

我有一个 python 网页抓取代码,如果我不在数据库中插入任何结果,它运行得很好。即当我注释掉这部分代码时

“”“ 连接到数据库并将数据放入 “”“

db= MySQLdb.connect("localhost","XXX","XXX","hmm_Raw_Data")
cursor=db.cursor()


#checking phase to stop scraping


sql = """SELECT Short_link FROM RentalWanted WHERE Short_link=%s"""

rows = cursor.execute(sql,(link_result))

if rows>=1:
    duplicate_count+=1
    print duplicate_count

    # if duplicate_count>=15:
    #   print "The program has started getting duplicates now- The program is terminating"
    #   sys.exit()
else:
    query="""INSERT INTO RentalWanted 
    (Sale_Rent, 
    Type, 
    Area, 
    Nearby, 
    Title,
    Price, 
    PricePerSqrFt, 
    Bedroom,
    Agency_Fee, 
    Bathroom, 
    Size,
    ZonedFor, 
    Freehold, 
    Prop_ref,
    Furnished_status,
    Rent_payment,
    Building_info,
    Amenities,
    Trade_name,
    Licence, 
    RERA_ID,
    Phone_info,
    Short_link) 
    values(
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s,
    %s)"""
    cursor.execute(query,(
    Sale_Rent_result,
    Type_result,
    area_result,
    nearby_result, 
    title_result, 
    price_result, 
    Pricepersq_result, 
    bedroom_result, 
    agencyfee_result, 
    bathroom_result, 
    size_result,
    Zoned_for_result, 
    Freehold_result, 
    propertyref_result, 
    furnished_result, 
    rent_is_paid_result, 
    building_result, 
    Amenities_result, 
    tradename_result, 
    licencenum_result, 
    reraid_result, 
    phone_result, 
    link_result))


db.commit()
cursor.close()
db.close()

输入上述代码时出现的错误是这样的:

Traceback (most recent call last):   File "RentalWanted.py", line 461, in <module>
    getting_urls_of_all_pages()   File "RentalWanted.py", line 45, in getting_urls_of_all_pages
    every_property_in_a_page_data_extraction(a['href'])    File "RentalWanted.py", line 365, in every_property_in_a_page_data_extraction
    rows = cursor.execute(sql,(link_result))   File "/usr/lib/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg/MySQLdb/cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args]) TypeError: not all arguments converted during string formatting

我认为我所做的查询有问题。

任何人都可以帮我弄清楚哪个部分需要修复。我花了几个小时但不知道我错在哪里

谢谢

最佳答案

你真的有 23 个独立的变量吗?最好把所有内容都放入一本字典中,这样更清楚,哪些内容属于同一组,而且您不必计算那么多。错误是,execute 需要一个列表作为最后一个参数,而 link_result 可能是一个包含多个字符的字符串,例如包含多个元素的列表:

result = {
    "Sale_Rent": Sale_Rent_result,
    "Type": Type_result,
    "Area": area_result,
    "Nearby": nearby_result,
    "Title": title_result,
    "Price": price_result,
    "PricePerSqrFt": Pricepersq_result,
    "Bedroom": bedroom_result,
    "Agency_Fee": agencyfee_result,
    "Bathroom": bathroom_result,
    "Size": size_result,
    "ZonedFor": Zoned_for_result,
    "Freehold": Freehold_result,
    "Prop_ref": propertyref_result,
    "Furnished_status": furnished_result,
    "Rent_payment": rent_is_paid_result,
    "Building_info": building_result,
    "Amenities": Amenities_result,
    "Trade_name": tradename_result,
    "Licence": licencenum_result,
    "RERA_ID": reraid_result,
    "Phone_info": phone_result,
    "Short_link": link_result,
}

db= MySQLdb.connect("localhost","XXX","XXX","hmm_Raw_Data")
cursor=db.cursor()


#checking phase to stop scrapping


sql = """SELECT Short_link FROM RentalWanted WHERE Short_link=%s"""

rows = cursor.execute(sql,(result["Short_link"],))

if rows>=1:
    duplicate_count+=1
    print duplicate_count

    # if duplicate_count>=15:
    #   print "The program has started getting duplicates now- The program is terminating"
    #   sys.exit()
else:
    query = """INSERT INTO RentalWanted ({fields}) VALUES ({values})"""
    query = query.format(fields=','.join(result), values=','.join(['%s']*len(result)))
    cursor.execute(query, result.values())

db.commit()
cursor.close()
db.close()

如果您尝试插入具有相同链接的另一行,最好使 Short_link 列唯一并捕获错误,而不是手动检查约束:

db= MySQLdb.connect("localhost","XXX","XXX","hmm_Raw_Data")
cursor=db.cursor()
try:
    query = """INSERT INTO RentalWanted ({fields}) VALUES ({values})"""
    query = query.format(fields=','.join(result), values=','.join(['%s']*len(result)))
    cursor.execute(query, result.values())
except mysql.connector.IntegrityError:
    duplicate_count+=1
    print duplicate_count
else:
    db.commit()
cursor.close()
db.close()

关于python - 类型错误 : not all arguments converted during string formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27343430/

相关文章:

python - 使用 selenium 和 python 在 Linkedin 上添加连接

php - 使用外键将数据插入表中php

PHP 注册时出错

java - 使用文本字段插入数据很困难(jdbc swing)

mysql - 如何使用 JOIN 而不是子查询(NOT IN)

python - 按索引创建嵌套列表的最小值列表

Python Requests/BeautifulSoup 访问分页

php - 在 ProductSell 模型中,有一个日期列类型为 varchar,格式为 d/m/Y 我如何使用其间检索数据?

mysql - SQL/MySQL - 按列长度排序

python - 如何从文件中保存数据而不是在 Python 中的变量或列表中?