作者vidmle (tt)
看板Python
标题[问题] 中文编码问题
时间Mon Nov 27 17:30:33 2017
大家好,最近刚学Python,用的版本是Python3.4
在爬虫过程中,遇到编码问题
此为原始码
https://imgur.com/a/mMhKs
我的程式
from selenium import webdriver
from bs4 import BeautifulSoup
from urllib.request import urlopen
import sys
import re
import json
driver =
webdriver.PhantomJS
(executable_path=r'C:\Python34\phantomjs-2.1.1-windows\bin\phantomjs')
#上述三行为同一行
driver.get("
http://opendata2.epa.gov.tw/AQI.json")
pageSource = driver.page_source
bsObj = BeautifulSoup(pageSource, "html.parser")
AQIScript = str(bsObj.html.body.find(text=re.compile("AQI")))
print(AQIScript)
输出後中文字会乱码,请问该如何解决?
https://imgur.com/a/auAuO
最终目的是要将AQIScript丢进json再上传至SQL
→ 我的程式码 js = json.loads(AQIScript)
之前有试过编码是\uXXXX的没问题可以成功显示中文
这是另一个网站的(此输出经过json.loads之後上传至SQL为正常中文显示)
https://imgur.com/a/bEsJZ
请教各位高手,我要怎麽改才能解决乱码的问题呢?
感谢~~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.116.79.197
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1511775036.A.CA4.html
1F:推 HenryLiKing: 我放到json里面也会这样欸QAQ 11/27 20:08
2F:推 TitanEric: 嗨 请参考下面连结 11/27 22:43
4F:→ vidmle: 谢谢你,已成功解决 11/28 11:03