zoukankan      html  css  js  c++  java
  • python 爬图片

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

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

    用的是正则表达式,

     1 '''
     2 符号:
     3     . 匹配任意字符,
    除外
     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()
     6 f.close()
     7 
     8 pic_url = re.findall('src2="(.*?)"',fileshtml,re.S)
     9 
    10 i = 0
    11 for each in pic_url:
    12 
    13     if each[0] == 'h':
    14         print 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
  • 相关阅读:
    2018.7.9 模拟赛
    树状数组||归并排序求逆序对+离散化 nlogn
    LCS nlogn
    孤岛营救问题
    [POJ 3621] Sighting Cows
    树状数组求LIS
    nlogn求逆序对&&陌上花开
    最长可重区间集
    LCA模板
    [BZOJ] 4196 [Noi2015]软件包管理器
  • 原文地址:https://www.cnblogs.com/TreeDream/p/6363479.html
Copyright © 2011-2022 走看看