【问题标题】:How to remove "????????" from a string in python?如何删除“?????????”来自python中的字符串?
【发布时间】:2018-07-20 16:09:34
【问题描述】:

我正在尝试从 facebook 中提取数据,但是在抓取时,我遇到了 unicode 类型错误。实际上,我试图抓取的文本包含如下信息:

Hi, this is text????????

抛出 unicodeEncodeError 异常的代码类似于:

driver.find_elements_by_xpath('//p').text

任何解决此问题的提示。

【问题讨论】:

  • 您可以选择在报废时忽略任何非 ascii 字符;
  • (driver.page_source).encode('ascii', 'ignore')
  • 你在加载页面后添加它(driver.get)

标签: python selenium-webdriver scrapy


【解决方案1】:

这个问题和这个问题类似:Selenium webdriver and unicode

他们建议使用以下方法将整个页面转换为 ascii:

(driver.page_source).encode('ascii', 'ignore')

您也可以将其编码为 utf-8:

(driver.page_source).encode('utf-8')

【讨论】:

    【解决方案2】:

    您只需在加载指定页面后添加(driver.page_source).encode(‘ascii’, ‘ignore’)即可。

    您也可以将其编码为 utf-8。

    【讨论】:

      【解决方案3】:

      添加(driver.page_source).encode('ascii', 'ignore')。这就是你所要做的。

      【讨论】:

      • 只是想知道为什么人们会重复其他人的答案。接受的答案包含其他答案所写的部分,并且似乎也没有编辑以包含相同的部分。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2020-11-12
      • 1970-01-01
      • 2020-08-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多