python在多玩图片上下载妹子图的实现代码

yipeiwu_com5年前Python基础

复制代码 代码如下:

# -*- coding:utf-8 -*-
import httplib
import urllib
import string
import re
def getContent():                   #从网站中获取所有内容
 conn = httplib.HTTPConnection("tu.duowan.com")
 conn.request("GET", "/m/meinv/index.html")
 r = conn.getresponse()
 print r.status, r.reason
 data1 = r.read()#.decode('utf-8') #编码根据实际情况酌情处理
 return data1

def getImageUrl(data):            #将获取到img链接写到sour.txt文件中国
 sour = open("test\\sour.txt", 'w')
 pplen = len("/zb_users/upload/202003/wqcgexmhszo.jpg")
 for i in range(len(data) - 3):
  if data[i] == 'i' and data[i + 1] == 'm' and data[i + 2] == 'g':
   for j in xrange(i + 9, i + 9 + pplen):
    sour.write(data[j])
   sour.write('\n')
 sour.close()

 


def downImage():               #根据test\\sour.txt里面的url自动下载图片
 tt = 0    #name
 sour = open('test\\sour.txt')
 while 1:
  line = sour.readline()
  if line:
   Len = len(line)
   #print Len
   if line[Len - 2] == 'g' and line[Len - 3] == 'p' and line[Len - 4] == 'j':
    path = line
    data = urllib.urlopen(line).read()
    f = open('test\\' + str(tt) + '.jpg', 'wb')
    f.write(data)
    f.close()
    tt = tt + 1
  else:
   break
 sour.close()

content = getContent()
getImageUrl(content)
downImage()

相关文章

Python实现二叉搜索树

Python实现二叉搜索树

二叉搜索树 我们已经知道了在一个集合中获取键值对的两种不同的方法。回忆一下这些集合是如何实现ADT(抽象数据类型)MAP的。我们讨论两种ADT MAP的实现方式,基于列表的二分查找和哈...

python通过openpyxl生成Excel文件的方法

本文实例讲述了python通过openpyxl生成Excel文件的方法。分享给大家供大家参考。具体如下: 使用前请先安装openpyxl: easy_install openpyxl...

Python 字符串类型列表转换成真正列表类型过程解析

我们在写代码的过程中,会经常使用到for循环,去循环列表,那么如果我们拿到一个类型为str的列表,对它进行for循环,结果看下面的代码和图: str_list = str(['a',...

python中的实例方法、静态方法、类方法、类变量和实例变量浅析

注:使用的是Python2.7。 一、实例方法 实例方法就是类的实例能够使用的方法。如下:复制代码 代码如下:class Foo:    def __ini...

python os用法总结

python os用法总结

前言:在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块,所...