Python MySQL数据插入

时间:2015-10-09 01:29:09

标签: python mysql beautifulsoup

我试图使用pythonbeautifulsoup4抓取一页。它将从该页面输出标题,价格和链接。

从这4个for循环中,我想在imghref, href, name, newprice db中插入这四个变量MySQL。所以我写了一些像这样的代码:

    db = MySQLdb.connect("host", "root", "pass", "dbname")
    cursor = db.cursor()

    def sample(max_pages):
        page = 1
        while page <= max_pages:

            .... other lines, not related to mysql...

            for link in soup.findAll('a', {'class': 'hoverbuy a-color'}):
                href = link.get('href')
                #print(href)

            for title in soup.findAll('span', {'class': 'title ellipsis'}):
                name = title.string
                #print(name)

            for price in soup.find_all('div', {'class': 'itm-price'}):
                fprice= price.get_text() 
                newprice = fprice.replace("৳", "")
                #print(newprice)

            sql = "INSERT INTO product(link, title,price) \
                      VALUES ('%s', '%s', '%s')" % (href, name, newprice)

            try:  
               cursor.execute(sql)
               db.commit()
            except:
               db.rollback()      

            page += 1

    sample(1)

    db.close()

因此,当我运行此代码时,MySQL中只插入一行(链接,标题,价格),而该页面中有70行或更多行。

所以,我认为while循环没有正常运行,或者我错放了sql查询和page += 1循环

但是,如果我只是打印这些变量,它就可以完美地运行所有70多个输出

2 个答案:

答案 0 :(得分:1)

目前,您只运行1个SQL查询以仅插入1行。我猜它只会在网站上插入最后一个值。

目前,您可以在网站上找到每个项目的所有匹配项,并替换for循环中的最后一项。 我建议将它们全部存储在列表中。然后将您找到的所有内容添加到列表中。

for link in soup.findAll('a', {'class': 'hoverbuy a-color'}):
    href.append(link.get('href'))

for title in soup.findAll('span', {'class': 'title ellipsis'}):
    name.append(title.string)

for price in soup.find_all('div', {'class': 'itm-price'}):
    fprice= price.get_text() 
    newprice.append(fprice.replace("৳", ""))

您可能正在寻找的是executemany()方法。这允许您将整个列表放入数据库。或者,您可以查看我制作的视频,linked to specific time.

cursor.executemany("INSERT INTO product VALUES(?,?,?)",(href, name, newprice))

编辑:您可能需要保持警惕,并确保所有列表的大小相同!

答案 1 :(得分:0)

maxpages = 1,而页面&lt; = maxpages,循环可以运行。 当页面+ = 1时,页面= 2.打破循环。 所以sql只执行一次。

href,name,newprice是单值。所以只有一个记录。你应该使用executemany()来插入多个记录。