天天看點

實戰篇 | 用Python來找你喜歡的妹子(二)

用Python做有趣的事情

先上效果圖吧,no pic say bird!

實戰篇 | 用Python來找你喜歡的妹子(二)
實戰篇 | 用Python來找你喜歡的妹子(二)
實戰篇 | 用Python來找你喜歡的妹子(二)
實戰篇 | 用Python來找你喜歡的妹子(二)

我之前寫了一個抓取妹子資料的文章,主要是使用selenium來模拟網頁操作,然後使用動态加載,再用xpath來提取網頁的資料,但這種方式效率不高。用Python來找合适的妹子(一)

是以今天我再補一個高效擷取資料的辦法.由于并沒有什麼模拟的操作,一切都可以人工來控制,是以也不需要打開網頁就能擷取資料!

但我們需要分析這個網頁,打開網頁 http://www.lovewzly.com/jiaoyou.html 後,按F12,進入Network項中

url在篩選條件後,隻有page在發生變化,而且是一頁頁的累加,而且我們把這個url在浏覽器中打開,會得到一批json字元串,是以我可以直接操作這裡面的json資料,然後進行存儲即可!

代碼結構圖:

實戰篇 | 用Python來找你喜歡的妹子(二)

操作流程:

headers 一定要建構反盜鍊以及模拟浏覽器操作,先這樣寫,可以避免後續問題!

條件拼裝

然後記得資料轉json格式

然後對json資料進行提取,

把提取到的資料放到檔案或者存儲起來

主要學習到的技術:

  1. 學習requests+urllib
  2. 操作execl
  3. 檔案操作
  4. 字元串
  5. 異常處理
  6. 另外其它基礎

請求資料

def craw_data(self):
        '''資料抓取'''
        headers = {
            'Referer': 'http://www.lovewzly.com/jiaoyou.html',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400'
        }
        page = 1
        while True:

            query_data = {
                'page':page,
                'gender':self.gender,
                'starage':self.stargage,
                'endage':self.endgage,
                'stratheight':self.startheight,
                'endheight':self.endheight,
                'marry':self.marry,
                'salary':self.salary,
            }
            url = 'http://www.lovewzly.com/api/user/pc/list/search?'+urllib.urlencode(query_data)
            print url
            req = urllib2.Request(url, headers=headers)
            response = urllib2.urlopen(req).read()
            # print response
            self.parse_data(response)
            page += 1
           

複制

字段提取

def parse_data(self,response):
        '''資料解析'''
        persons = json.loads(response).get('data').get('list')
        if persons is None:
            print '資料已經請求完畢'
            return

        for person in persons:
            nick = person.get('username')
            gender = person.get('gender')
            age = 2018 - int(person.get('birthdayyear'))
            address = person.get('city')
            heart = person.get('monolog')
            height = person.get('height')
            img_url = person.get('avatar')
            education = person.get('education')
            print nick,age,height,address,heart,education
            self.store_info(nick,age,height,address,heart,education,img_url)
            self.store_info_execl(nick,age,height,address,heart,education,img_url)
           

複制

檔案存放

def store_info(self, nick,age,height,address,heart,education,img_url):
        '''
        存照片,與他們的内心獨白
        '''
        if age < 22:
            tag = '22歲以下'
        elif 22 <= age < 28:
            tag = '22-28歲'
        elif 28 <= age < 32:
            tag = '28-32歲'
        elif 32 <= age:
            tag = '32歲以上'
        filename = u'{}歲_身高{}_學曆{}_{}_{}.jpg'.format(age,height,education, address, nick)

        try:
            # 補全檔案目錄
            image_path = u'E:/store/pic/{}'.format(tag)
            # 判斷檔案夾是否存在。
            if not os.path.exists(image_path):
                os.makedirs(image_path)
                print image_path + ' 建立成功'

            # 注意這裡是寫入圖檔,要用二進制格式寫入。
            with open(image_path + '/' + filename, 'wb') as f:
                f.write(urllib.urlopen(img_url).read())

            txt_path = u'E:/store/txt'
            txt_name = u'内心獨白.txt'
            # 判斷檔案夾是否存在。
            if not os.path.exists(txt_path):
                os.makedirs(txt_path)
                print txt_path + ' 建立成功'

            # 寫入txt文本
            with open(txt_path + '/' + txt_name, 'a') as f:
                f.write(heart)
        except Exception as e:
            e.message
           

複制

execl操作

def store_info_execl(self,nick,age,height,address,heart,education,img_url):
        person = []
        person.append(self.count)   #正好是資料條
        person.append(nick)
        person.append(u'女' if self.gender == 2 else u'男')
        person.append(age)
        person.append(height)
        person.append(address)
        person.append(education)
        person.append(heart)
        person.append(img_url)

        for j in range(len(person)):
            self.sheetInfo.write(self.count, j, person[j])

        self.f.save(u'我主良緣.xlsx')
        self.count += 1
        print '插入了{}條資料'.format(self.count)
           

複制

源碼位址:https://github.com/pythonchannel/python27/blob/master/test/meizhi.py