Python-BeautifulSoup : How to save html into database?

标签 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 返回 getattr(self, "_{0}_to_mysql".format(type_name))(value) AttributeError:“MySQLConverter”对象没有属性“_tag_to_mysql” "MySQL 类型".format(type_name)) TypeError:Python“标签”无法转换为 MySQL 类型

最佳答案

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

tostring = str(数据)

关于Python-BeautifulSoup : How to save html into database?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728615/

相关文章:

mysql - 将 MySql 数据从 Latin1 转换为 UTF8

python - 使用 BeautifulSoup 在 python 中进行编码

python - 在搜索栏中输入查询并抓取结果

python - Keras:使用模型的一阶和二阶导数之和作为最终输出

python - 将 Flask 中的 URL 还原为端点 + 参数

python - 将 xpath 应用于 xpath 操作的结果

php - 调用文件时 PDF 不下载

python - 从字典列表中获取特定的键和值

php - 将 Magento 的 var 目录放入 RAM

python - BeautifulSoup 使用两个不同的 div 类查找数据