当前位置:首页 > Python教程 > python技巧

python 爬图片

学了两天python,语法慢慢熟悉吧,数据结构都没写过。

写了一个爬图片的小东西。挺有意思的。都是女神照 (????)

技术分享

用的是正则表达式,

             1
            ‘‘‘
             2
            符号:

             3
                . 匹配任意字符,n除外

             4
                * 匹配前一个字符一次或无限次

             5
                ? 匹配前一个字符0次或1次

             6
                .*    贪心匹配

             7
                .*?    非贪心匹配

             8
                ()    返回括号内容

             9
            方法:

            10
                findall

            11
                search

            12
                sub

            13
            14
            用的最多的是(.*?)

            15
            ‘‘‘
        

requests的导入,我也是醉了,还要easy_install,pip,

后来一切准备就绪了,浏览器打开的源码http:/,都是这种鬼东西,我就用word替换,发现不行,太多的不可见字符,于是用记事本替换,最后还是最好的办法,我把chrome更新了。

             1
            import
             re

             2
            import
             requests

             3
             4 f = open(html.txt,r)
 5 fileshtml = f.read()
 6f.close()
 7 8 pic_url = re.findall(src2="(.*?)",fileshtml,re.S)
 910 i = 0
11for each in pic_url:
1213if each[0] == h:
14print each
15         pic = requests.get(each)
16         fp = open(pic\ + str(i) + .jpg,wb)
17        fp.write(pic.content)
18        fp.close()
19         i += 1

 

原文:http://www.cnblogs.com/TreeDream/p/6363479.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!