【问题标题】:Selenium webdriver and URIError: "String contained an illegal UTF-16 sequence"Selenium webdriver 和 URIError:“字符串包含非法 UTF-16 序列”
【发布时间】:2017-09-27 21:48:48
【问题描述】:

背景:我刚学了两天“Webdriver”和“Beautifulsoup”。

问题: 我使用以下代码下载网页:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.PhantomJS(executable_path)
driver.get('https://mojim.com/twy100468x17x18.htm')
pageSource = driver.page_source
...

然后,我遇到了这个错误

WebDriverException: Message: URIError - String contained an illegal UTF-16 sequence.

尝试:我尝试将pageSource = browser.page_source 替换为
(driver.page_source).encode('ascii', 'ignore')
(driver.page_source).encode('utf-8')here建议)
但仍然以同样的错误结束....

网页来源 here

我该怎么办? html中是否有非法文本或什么?
谢谢

【问题讨论】:

    标签: html selenium beautifulsoup python-3.5


    【解决方案1】:

    我刚刚克服了这种情况。这是由不同的非 UTF 字符引起的

    我用 Edge 驱动程序解决了这个问题(Chrome 和 Mozilla 不处理)。所以你可以使用它:

    from selenium import webdriver
    from bs4 import BeautifulSoup
    
    driver = webdriver.Edge()
    driver.get('https://mojim.com/twy100468x17x18.htm')
    pageSource = driver.page_source
    

    问题是 Edge 不像 PhantomJS 那样无头,所以在抓取时我只在这个坏的例外链接上使用它。此外,Egde 几乎和 PhantomJS 一样快。

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 2018-06-04
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2019-09-21
      • 2011-08-06
      相关资源
      最近更新 更多