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
O21
V2EX  ›  Python

Python 处女座 源码,抓取豆瓣妹子图片,卡壳了。。求助大神

  •  
  •   O21 ·
    ShenXuGongZi · 2014-07-14 22:00:35 +08:00 · 6895 次点击
    这是一个创建于 3579 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码:


    目前路径是定死的。。

    我想用
    img_LuJ = raw_input("路径:") 来获取路径
    然后体现在
    68行
    download_img = urllib.urlretrieve(imgurl,'/home/Hang/文档/PythonEX/Doubanimg/%s.jpg'%img_num)
    download这个函数里面

    download_img = urllib.urlretrieve(imgurl,'%r/%s.jpg'%(img_LuJ,img_num))

    这样写。。 可是没用。。 他会获取到
    '/home/xxx'/'sss.jpg 路径上有引号。。。 我很无语。
    第 1 条附言  ·  2014-07-14 22:40:11 +08:00
    @paulw54jrn
    @izoabr
    @77421
    哈哈 感谢回复 我已经搞定啦~~~~
    方法
    引入 os
    import os
    img_LuJ = raw_input("路径:".decode('utf-8'))
    img_LuJ2 = os.path.abspath(img_LuJ)

    然后在函数里面

    download_img = urllib.urlretrieve(imgurl,img_LuJ2 + '/%s.jpg'%img_num)

    这样就木有问题啦~~ 哈哈哈啊哈哈哈哈哈哈~~~~


    题外话。。。 为什么不让我回复了。。。V2ex怎么设置的啊。。我没灌水。。
    "创建新回复过程中遇到一些问题:
    你回复过于频繁了,请稍等 1800 秒之后再试"
    第 2 条附言  ·  2014-07-15 09:50:59 +08:00
    成品源码: http://www.v2ex.com/t/122642

    欢迎指教 嘿嘿?
    23 条回复    2014-08-01 11:31:46 +08:00
    O21
        1
    O21  
    OP
       2014-07-14 22:01:28 +08:00
    =,= 写的有点乱。。新手,,请各位海涵~~
    77421
        2
    77421  
       2014-07-14 22:09:11 +08:00
    用正则。。
    O21
        3
    O21  
    OP
       2014-07-14 22:13:16 +08:00
    @77421 木有太看懂。。是用正则获取用户输入路径吗?
    izoabr
        4
    izoabr  
       2014-07-14 22:16:16 +08:00
    为什么是3个引号?
    Fotix
        5
    Fotix  
       2014-07-14 22:18:27 +08:00
    一看就是抓晒组的,果不其然
    O21
        6
    O21  
    OP
       2014-07-14 22:21:53 +08:00
    @izoabr
    错误是这个
    IOError: [Errno 2] No such file or directory: u"'/home/o21/img'/p8406921.jpg"
    izoabr
        7
    izoabr  
       2014-07-14 22:24:04 +08:00
    @O21 这个IOError是哪行报出来的?
    你试试先拼接目标路径+文件名到一个变量,不用%号去拼
    paulw54jrn
        8
    paulw54jrn  
       2014-07-14 22:27:16 +08:00
    @O21
    错误:
    IOError: [Errno 2] No such file or directory: u'/home/o21/\u6587\u6863/PythonEX/Doubanimg/p1616253.jpg'
    检查下Unicode的问题
    O21
        9
    O21  
    OP
       2014-07-14 22:29:26 +08:00
    @izoabr 我这样写了。
    img_LuJ = raw_input("路径:".decode('utf-8'))

    download_img = urllib.urlretrieve(imgurl,img_LuJ + '/%s.jpg'%img_num)

    然后报错
    IOError: [Errno 2] No such file or directory: u'/home/o21/img/p1611237.jpg'
    paulw54jrn
        10
    paulw54jrn  
       2014-07-14 22:30:24 +08:00
    把下载路径改成 '/tmp/%s.jpg' % img_num 用做测试的话,程序可以跑起来

    paul@PaulMBP:~/Desktop$ ipython test.py
    ##################################################
    本程序主要采集豆瓣<请不要害羞>小组的图片
    ##################################################
    采集前需要输入代理服务器地址,这样可以防止被豆瓣屏蔽.
    推荐一个代理地址: http://cn-proxy.com/
    只需要输入服务器地址以及端口号,不需要输入http
    例子:127.0.0.1:8080
    ##################################################
    请输入采集代理服务器:121.10.120.135:8001
    请输入采集页码数:1
    http://img3.douban.com/view/group_topic/large/public/p16112371.jpg
    http://img3.douban.com/view/group_topic/large/public/p16112380.jpg
    程序采集完成
    O21
        11
    O21  
    OP
       2014-07-14 22:30:26 +08:00
    @paulw54jrn 我这次木有用中文,也报错。。我楼上写了。。 很郁闷。
    paulw54jrn
        12
    paulw54jrn  
       2014-07-14 22:39:10 +08:00
    这样试试?

    file = "/home/o21/img/%s.jpg" % img_num
    try:
    download_img = urllib.urlretrieve(imgurl,file)
    except IOError:
    os.mkdir(file.[:file.rfind("/")])
    paulw54jrn
        13
    paulw54jrn  
       2014-07-14 22:39:46 +08:00
    唔..偷懒没用gist..
    缩进乱了..不过你懂的..
    kawaiiushio
        14
    kawaiiushio  
       2014-07-15 03:04:34 +08:00
    求成品分享
    O21
        15
    O21  
    OP
       2014-07-15 03:21:41 +08:00
    @kawaiiushio 在这里下载吧 HOHO http://162.244.92.122/DouBanMZ.zip
    WhyLiam
        16
    WhyLiam  
       2014-07-15 07:55:24 +08:00
    和我一样,我也前天写了抓 害羞组 图片的程序。同道中人啊
    但是写的比你简单多了。。。
    已经抓了近1700页了
    eslizn
        17
    eslizn  
       2014-07-15 08:01:55 +08:00
    我是来看害羞组的
    xavierskip
        18
    xavierskip  
       2014-07-15 08:32:50 +08:00
    哈哈,我写过抓取虎扑相册的工具。起初也是为了抓妹子图。

    http://hualbum.duapp.com/
    puyo
        19
    puyo  
       2014-07-15 10:15:15 +08:00
    可以加入多线程,速度要快一些。
    dingyaguang117
        20
    dingyaguang117  
       2014-07-15 10:34:53 +08:00
    至少 socket 的timeout 要加吧,不然死都不知道怎么死的
    sujin190
        21
    sujin190  
       2014-07-15 11:28:10 +08:00
    看来写过多注释也有不利的时候。。
    shyrock
        22
    shyrock  
       2014-07-15 22:27:19 +08:00
    请教解决中文报错那段是啥意思?
    horizon
        23
    horizon  
       2014-08-01 11:31:46 +08:00
    为什么要用代理?在header里直接加入cookies就可以了啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1991 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:27 · PVG 09:27 · LAX 18:27 · JFK 21:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.