python程序乱码解决

安装好python后的牛刀小试

问题1: 在应用python 模块 chardet 运行程序时,若出现ImportError: No module named
chardet

问题2: UE没有python高亮着色

问题3:UserAgent获取

问题4:python程序出现乱码解决


代码片段 1

# coding:utf-8
#    '''
#    贴吧代码拉取
#    '''

import urllib2
import sys
import chardet

def load_page(url):
      '''
          根据url 加载页面
      '''

      user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

      headers = {"User-Agent": user_agent}

      requ = urllib2.Request(url,headers = headers)

      response = urllib2.urlopen(requ)

      html = response.read()

      return html

代码片段 2

def tieba (url,page_begin,page_end):
    '''
        range(1,4)
        http://tieba.baidu.com/f?kw=%E5%AE%8C%E7%BE%8E%E4%B8%96%E7%95%8C&ie=utf-8&pn=50
    '''
    f = open('nihao.txt','w')
    for i in range(page_begin,page_end+1):
        pn = 50 * ( i - 1)

        myUrl = url + str(pn)

        print myUrl

        html =  load_page(myUrl)


        f.write("==========第%d页=========" %(i))
        f.write(html)
        if i == page_end+1 :
              f.close()




url = raw_input("请输入贴吧地址:")
page_begin = int(raw_input("请输入起始页码:"))
page_end = int(raw_input("请输入终止页码:"))
tieba (url,page_begin,page_end)

运行程序后发现,拉取的信息是乱码的。
解决方案:
更改代码片段 1

# coding:utf-8
#    '''
#    贴吧代码拉取
#    '''

import urllib2
import sys
import chardet

def load_page(url):
      '''
          根据url 加载页面
      '''

      user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

      headers = {"User-Agent": user_agent}

      requ = urllib2.Request(url,headers = headers)

      response = urllib2.urlopen(requ)

      html = response.read()

      typeEncode = sys.getfilesystemencoding()

      infoencode = chardet.detect(html).get('encoding','utf-8')

      info = html.decode(infoencode,'ignore').encode(typeEncode)

      return info

添加代码

typeEncode = sys.getfilesystemencoding()

infoencode = chardet.detect(html).get(‘encoding’,’utf-8’)

info = html.decode(infoencode,’ignore’).encode(typeEncode)


完整代码:

# coding:utf-8
#    '''
#    贴吧代码拉取
#    '''

import urllib2
import sys
import chardet

def load_page(url):
      '''
          根据url 加载页面
      '''

      user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

      headers = {"User-Agent": user_agent}

      requ = urllib2.Request(url,headers = headers)

      response = urllib2.urlopen(requ)

      html = response.read()

      typeEncode = sys.getfilesystemencoding()

      infoencode = chardet.detect(html).get('encoding','utf-8')

      info = html.decode(infoencode,'ignore').encode(typeEncode)

      return info


def tieba (url,page_begin,page_end):
    '''
        range(1,4)
        http://tieba.baidu.com/f?kw=%E5%AE%8C%E7%BE%8E%E4%B8%96%E7%95%8C&ie=utf-8&pn=50
    '''
    f = open('nihao.txt','w')
    for i in range(page_begin,page_end+1):
        pn = 50 * ( i - 1)

        myUrl = url + str(pn)

        print myUrl

        html =  load_page(myUrl)


        f.write("==========第%d页=========" %(i))
        f.write(html)
        if i == page_end+1 :
              f.close()




url = raw_input("请输入贴吧地址:")
page_begin = int(raw_input("请输入起始页码:"))
page_end = int(raw_input("请输入终止页码:"))
tieba (url,page_begin,page_end)

在编写这段代码出现的问题小结:

问题1: 在应用python 模块 chardet 运行程序时,若出现

Traceback (most recent call last):
File “tieba.py”, line 8, in
import chardet
ImportError: No module named chardet

解决方案:window下的python
由于下载的python并没有chardet模块导致的。
这个时候需要下载 chardet下载方式,点击chardet即可下载
下载后 解压缩 ,提取 chardet目录所有文件
将其所有内容copy放入\Python27\Lib\site-packages 新建chardet文件夹下

问题2: UE没有python高亮着色
解决方案:
下载对应的python着色 Python 2.6 & 3.0 (python26.uew)
http://www.ultraedit.com/downloads/extras/wordfiles.html

下载后将python26.uew 放到UE的workfile文件夹下,
workfile文件夹目录获取:
UE 选项卡 –>选择高级–>配置–>查找输入框输入 || 语法着色–>文档的完整目录名称就是 workfile的 path了,让后将下载的uew文件copy进去,重启UE,发现py文件的内容着色了,自动匹配查看方式。

问题3:UserAgent获取

<html>

<body>
<script type="text/javascript">
    document.write("

UserAgent: ") document.write(navigator.userAgent + "

"
)
script> body> html>

你可能感兴趣的:(python)