【问题标题】:psycopg2.ProgrammingError: can't adapt type 'Tag'psycopg2.ProgrammingError:无法适应类型“标签”
【发布时间】:2020-07-23 07:42:09
【问题描述】:

Traceback(最近一次调用最后一次): https://retailer.partykungen.se/sjalvlysande-armband.html 文件“C:/Users/xyz/PycharmProjects/crawler/back-end.py”,第 121 行,在 cur.execute('''INSERT INTO crawler(url, title, varient, category, cmets, price, description, tag, image, article, stock, is_retailer) VALUES (%s, %s, %s, %s, % s, %s, %s, %s, %s, %s, %s, %s)''',(link, ti​​tle, name1, cat, cmets, price, description, tag, image, art, stock, is_retailer)) psycopg2.ProgrammingError: can't adapt type 'Tag'

【问题讨论】:

  • 请解释您正在尝试做什么,您尝试了什么以及失败了什么。还有你期望看到的结果。
  • 与您尝试插入标签字段的数据有关,如果您认为它看起来不错,请打开日志记录以查看在 Postgesql 收到的值。如果您捕获 sql,则可以在 PGadmin 中直接应用 sql 时尝试一些变体

标签: python postgresql


【解决方案1】:

如上所述,我会检查您的数据。因此,如果您尝试将 pandas 数据框导入 Postgres 表,我会执行以下操作:

确保您已完全删除所有标签。因此,首先通过检查您的数据框来彻底检查,因为标签会抛出 psycopg2 驱动程序,因为它无法正确识别对象。因此,请确保首先从 HTML 中提取所需的所有信息。使用 getText() 获取更“顽固”的标签。

    list_titles = []
    movie_titles = all_movies[n].find_all("h1",{"class":"blah"})
    for k in movie_titles:
        title = k.getText()
        list_titles.append(title)

【讨论】:

    【解决方案2】:

    这可能是您在 PostgreSQL 表中定义数据类型的方式与解析器对象返回类型之间的 TypeError 冲突。

    例如,使用 BeautifulSoup4 (bs4) 来爬取你的文件,你的树中元素的返回类型是 type: Tag (参见:@987654321 中的“Kind of Objects” @)。

    我的建议:

    1) 将所有 Postgre 表类型定义为文本(以在调试期间测试成功插入)

    2) 将所有变量转换为字符串:例如,

    ... (str(link), str(titl), str(name1), ...) 
    

    3) 这应该可以让您成功插入,然后您可以向后走,确保每个元素都与表中所需的类型匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-08
      • 2019-11-10
      • 2020-08-21
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多