【问题标题】:How can I input code stored in a MySQL cell into my Python beautifulsoup如何将存储在 MySQL 单元中的代码输入我的 Python beautifulsoup
【发布时间】:2019-07-09 01:31:22
【问题描述】:

我正在解析许多不同的网站,并希望将它们的唯一标签保留在 MySQL 表中,并使用存储的代码通过使用漂亮的汤来检索新内容。但是,在检索该代码并将其放入我美丽的汤中以获取“标题”之外更复杂的标签时,我遇到了麻烦。

所以它适用于简单的标签,例如“title”,但不适用于 'div'、{'class': 'event-info'},在我的示例中是 row[3]。

'div',{'class':'event-info'} 在我的 mysql 单元格中的 row[3]

Traceback (most recent call last):
  File 
    content = soup.find(row[3]).get_text().strip()
AttributeError: 'NoneType' object has no attribute 'get_text'

我的专栏正在使用“varchar(255) COLLATE utf8mb4_unicode_ci”

【问题讨论】:

    标签: mysql python-3.x beautifulsoup pymysql


    【解决方案1】:

    我认为这是因为您使用字符串调用 find 方法。 为了获得正确的行为,您应该将不同的参数保存在 MySQL 表的不同列中,或者您可以像这样更改值:

    rs = row[3].split(',')
    rtag = rs[0].split('\'')[1]
    
    rclass = dict()
    rclass[rs[1].split('\'')[1]] = rs[1].split('\'')[3]
    

    最后:

    content = soup.find(rtag, rclass).get_text().strip()
    

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 2012-05-03
      相关资源
      最近更新 更多