Python-BeautifulSoup:如何将html保存到数据库中?

时间:2018-06-06 20:00:35

标签: python mysql beautifulsoup

我正在尝试将产品说明保存到MySQL数据库中。到目前为止,我已经尝试将数据类型更改为BLOB,LONGBLOB,TEXT,LONGTEXT但它不起作用。

    import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import mysql.connector

cnx = mysql.connector.connect(user='root', password='Kradz579032!!',
                              host='127.0.0.1',
                              database='aliexpressapidb')
cursor = cnx.cursor()

add_data = ("INSERT INTO productdetails"
               "(description) "
               "VALUES (%s)")

my_url = 'https://www.aliexpress.com/item/Cheap-Necklace-Jewelry-Alloy-Men-Vintage-Personality-Pendant-Creativity-Simple-Accessories-Symbol-Necklace-Wholesale-Fashion/32879629913.html?spm=a2g01.11147086.layer-iabdzn.4.4a716140Ix00VA&scm=1007.16233.91830.0&scm_id=1007.16233.91830.0&scm-url=1007.16233.91830.0&pvid=acdbf117-c0fb-458f-b8a9-ea73bc0d174b'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
description = page_soup.findAll("div", {"class": "ui-box-body"})
#print(description)
data_insert = description
cursor.execute(add_data, data_insert)




cnx.commit()

cursor.close()
cnx.close()

不断收到错误:

文件“/Users/reezalaq/.conda/envs/untitled3/lib/python3.6/site-packages/mysql/connector/conversion.py”,第160行,在to_mysql中     return getattr(self,“_ {0} _to_mysql”.format(type_name))(value) AttributeError:'MySQLConverter'对象没有属性'_tag_to_mysql' “MySQL类型”.format(type_name)) TypeError:Python'tag'无法转换为MySQL类型

3 个答案:

答案 0 :(得分:0)

description = page_soup.findAll("div", {"class": "ui-box-body"})

^这里 findAll 将返回匹配标记的列表,但是您需要传递一个字符串cursor.execute()。因此,您应该从这些匹配的标记中获取字符串。以下可能是一种方法:

description = page_soup.findAll("div", {"class": "ui-box-body"})
data_insert = description[0].get_text()
cursor.execute(add_data, data_insert)

答案 1 :(得分:0)

通过@MicahB建议将html数据转换为字符串来解决问题。

tostring = str(data)

答案 2 :(得分:0)

maindiv = str(soup.find("body"))

这会将正文标签html返回为String。

sql = "UPDATE `abc` SET `html` = %s WHERE `abc`.`id` = %s"
val = (maindiv,1)
mycursor.execute(sql, val)
mydb.commit()

以上代码会更新记录