Home  >  Q&A  >  body text

python - 使用selenium,用PhantomJS抓取网页后保存为.html后出现中文乱码?

保存的html文档中出现中文乱码:
<meta name="keywords" content="鈽呯敤閽㈢惔璇犻噴鍛ㄦ澃浼︹櫔鏃犱笌浼︽瘮涓嶉€濈粡鍏革紝姊︽兂瀹禯eDragon锛岄挗鐞达紝缁忓吀锛岃交闊充箰">

代码:

from selenium import webdriver
browser = webdriver.PhantomJS( )
url = 'http://music.163.com/#/playlist?id=11362719'
browser.get(url)  # 打开网页
browser.switch_to.frame(browser.find_element_by_xpath("//iframe"))
#title = browser.find_elements_by_xpath('//*[@id="play-count"]')
#title = browser.find_elements_by_xpath('//*tr/@class')  
#print(browser.page_source.encoding('utf-8'))
print(browser.page_source,file=open('C:/Users/welwel/Desktop/source.html','w',encoding='utf-8'))
browser.quit()

1.使用type(browser.page_source)查看类型是str,无法使用。decode转换格式
2.用的是win7下python3.5 的IDLE,使用sys.getdefaultencoding()查看默认编码是‘utf-8’
3.直接使用print(browser.page_source)报错:

Traceback (most recent call last):
  File "C:\Users\welwel\Desktop\wangyi.py", line 8, in <module>
print(browser.page_source)
  File "C:\Python35-32\lib\idlelib\PyShell.py", line 1344, in write
return self.shell.write(s, self.tags)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 55288-55288: Non-BMP character not supported in Tk

这个问题在第一次爬取网页时使用print(browser.page_source,file=open('C:/Users/welwel/Desktop/source.html','w',encoding='utf-8'))
不会报错,但是加在for循环中就会从第二个开始,出现中文乱码,不知是不是bug。有没有遇到过的。

阿神阿神2719 days ago557

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-18 09:48:03

    Try this:

    print(browser.page_source.encode('utf-8').decode(), file=open("xxx.html","w", encoding='utf-8'))

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:48:03

    print(browser.page_source,file=open('C:/Users/welwel/Desktop/source.html','w'))

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:48:03

    Okay, my need is to crawl the comments and song titles of songs. I originally planned to crawl down the webpage and slowly regularize it. Later, I found that the odd-numbered webpages were normal in Chinese, and the even-numbered webpages were garbled. (I want to crawl 50 of them. web page), and later it was reversed. This means that there is a bug on win7 and linux is not installed. Use xpath analysis according to requirements,
    eg:
    ele_com = browser.find_element_by_xpath("//p[@class='cnt f-brk']")
    The returned data is normal. Still running on cmd.
    So if you want to crawl data, just use the tools on the module, don’t mess around

    reply
    0
  • Cancelreply