V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zhijiansha
V2EX  ›  Python

python 中十六进制如何转中文??

  •  
  •   zhijiansha · 2016-09-14 20:27:23 +08:00 · 7935 次点击
    这是一个创建于 2779 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import requests,urllib2
    from bs4 import BeautifulSoup
    import sys

    def get_img(url):
    wb = requests.get(url)
    wb.encoding = "utf-8"
    soup = BeautifulSoup(wb.text,'lxml')
    img_url = soup.select('#Zoom img')
    if img_url == []:
    return False
    else:
    return img_url[0].get('src')


    def get_title(url):
    wb = requests.get(url)
    wb.encoding = "utf-8"
    soup = BeautifulSoup(wb.text,'lxml')
    title = soup.select("a[href='#']")
    return title

    def get_url(Url):
    wb = requests.get(Url)
    wb.encoding = 'gb2312'
    soup = BeautifulSoup(wb.text,'lxml')
    title = soup.select('.ulink')
    url = soup.select('.ulink')
    titles_urls = []
    for x,y in zip(title,url):
    reload(sys)
    sys.setdefaultencoding('utf-8')
    data = {
    'title': x.get_text().split("《")[1].split("/")[0].split("》")[0],
    'url':y.get('href'),
    }

    titles_urls.append(data)
    return titles_urls


    for z in range(1,100):
    url = 'http://www.ygdy8.net/html/gndy/dyzz/list_23_%d.html' %z
    for x in get_url(url):
    u = get_img("http://www.ygdy8.net"+str(x['url']))
    if u != False:
    print u
    print x['title']
    y = str(x['title'])
    with open('imgs/'+str(y)+'.jpg', "wb") as f:
    f.write(requests.get(u).content)
    print "第%d 页" %z


    运行结果:

    https://ws3.sinaimg.cn/mw690/81298caagw1f7mwfo0otfj21kw29ku0x.jpg
    琼斯的自由国度
    Traceback (most recent call last): with open('imgs/'+str(y)+'.jpg', "wb") as f:
    IOError: [Errno 2] No such file or directory: 'imgs/\xe7\x90\xbc\xe6\x96\xaf\xe7\x9a\x84\xe8\x87\xaa\xe7\x94\xb1\xe5\x9b\xbd\xe5\xba\xa6.jpg'
    [Finished in 0.7s with exit code 1]
    12 条回复    2016-10-05 12:41:55 +08:00
    codepurple
        1
    codepurple  
       2016-09-14 21:46:06 +08:00
    不是中文转换问题,是 imgs 目录没有创建报的错误
    Arnie97
        2
    Arnie97  
       2016-09-14 21:52:24 +08:00 via Android
    reload(sys); sys.setdefaultencoding('utf-8') 差评
    zhijiansha
        3
    zhijiansha  
    OP
       2016-09-14 22:17:06 +08:00
    @Arnie97 -。-# 这个不要么?
    qqmishi
        4
    qqmishi  
       2016-09-14 22:18:58 +08:00 via Android
    和中文无关,报错是没有这个文件或目录,应该是你目录没建立或者路径不对
    zhijiansha
        5
    zhijiansha  
    OP
       2016-09-14 22:23:26 +08:00
    @qqmishi
    Traceback (most recent call last):
    琼斯的自由国度

    File "C:\Users\123\Desktop\dianyingtiant.py", line 51, in <module>
    with open('imgs/'+str(y)+'.jpg', "wb") as f:
    IOError: [Errno 22] invalid mode ('wb') or filename: 'imgs/\xe7\x90\xbc\xe6\x96\xaf\xe7\x9a\x84\xe8\x87\xaa\xe7\x94\xb1\xe5\x9b\xbd\xe5\xba\xa6.jpg'
    [Finished in 0.7s with exit code 1]
    qqmishi
        6
    qqmishi  
       2016-09-14 22:47:46 +08:00
    @zhijiansha 我在 Ubuntu 下就执行成功了,,,应该是 windows 系统本身的锅。
    qqmishi
        7
    qqmishi  
       2016-09-14 22:49:14 +08:00
    @zhijiansha 反应过来了,,,你这是从例子代码里改出来的吧, windows 和 linux 的分隔符是反的。 http://blog.csdn.net/kazeik/article/details/8742953 ,可以参考一下。
    Arthur2e5
        8
    Arthur2e5  
       2016-09-15 23:10:17 +08:00   ❤️ 1
    @qqmishi 不是分隔符的问题。观察 \x... 那段序列和 get_url 的处理可知这玩意是 UTF-8 ,在 Windows 下默认对 str 用 ANSI API 当然会抓瞎。

    解决方式很简单,不要用编码逻辑混乱的 python2 str ,要用 py2 也给我去用 unicode 。
    qqmishi
        9
    qqmishi  
       2016-09-16 01:21:07 +08:00
    @Arthur2e5 你是对的,改成 with open('imgs/'+unicode(y).encode('gbk')+'.jpg', "wb") as f:在 windows 下可以执行了
    Arthur2e5
        10
    Arthur2e5  
       2016-09-27 11:01:51 +08:00   ❤️ 1
    @qqmishi 我求求你了真的不要 encode gbk ,硬要用 py2 就好好用 unicode 数据类型行不行?

    你用 gbk 对付 cp936 ANSI API 是吧,我一个欧元符号就可以把你搞死。
    更不要说非中文版 Windows 了。
    zhijiansha
        11
    zhijiansha  
    OP
       2016-09-30 22:37:58 +08:00
    @Arthur2e5 那最合适的处理方式应该是??
    Arthur2e5
        12
    Arthur2e5  
       2016-10-05 12:41:55 +08:00
    @zhijiansha 换成 python3 立地成佛,或者 python2 去乖乖用 unicode 数据类型。我感觉我说了很多遍了啊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1438 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:54 · PVG 07:54 · LAX 16:54 · JFK 19:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.