我无法在loop-python中将数据保存到sql

时间:2017-11-25 14:01:17

标签: python sql

我试图将数据放到sql表但是总是有空白文件。 当我评论插入,并在屏幕上打印数据,然后我看到一切。 谁能帮我?

import csv, sqlite3
#filename = "data_.csv"
#f = open(filename, "w")
qlite_file = 'my_first_db.sqlite.db'  
con = sqlite3.connect("tedsddwwsssat.db")
cur = con.cursor()
cur.execute("CREATE TABLE t (var0, var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15);")
    for a in range(1,10):
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup as soup
    my_url = 'https://www.auto-data.net/en/?f=showCar&car_id='+ str(a)
    try:
        uClient = uReq(my_url)
        page_html = uClient.read()
        uClient.close();
        page_soup = soup(page_html,"html.parser")
        container = page_soup.findAll("table", {"class":"carData rightT"})
        x = container[0].findAll("tr")
        xxx = len(x)
        print(xxx)
        #if xxx>15:
        #   xxx=15
        if x[0].findAll("td")[1].text in ("Nissan ","volkswagen ","AC ", "BMW ", "Citroen ", "Seat ", "Toyota ", "Tesla", "Volvo ", "Jeep ", "Kia ", "Renault", "Fiat ", "Ford ", "Mercedes-Benz "):
            for xx in range(10):
                x1 = x[xx].findAll("td")[0].text
                x2 = x[xx].findAll("td")[1].text
                put = print("var"+str(xx))
                print(x2)
                #cur.executemany("INSERT INTO t (?) VALUES (?)", (put, x2))

            con.commit()    
            #f.write(x1 + ": " + x2 + ",")
        f.write("\n")
    except:
        pass
    #print("done " + str(a))
con.close()

1 个答案:

答案 0 :(得分:0)

print函数的返回值为NoneNone已分配给变量put。您可能想要更改此行:

put = print("var"+str(xx))