如何用Python来找你喜欢的妹子?

开发 后端 服务器
我之前写了一个抓取妹子资料的文章,主要是使用selenium来模拟网页操作,然后使用动态加载,再用xpath来提取网页的资料,但这种方式效率不高。所以今天我再补一个高效获取数据的办法.由于并没有什么模拟的操作,一切都可以人工来控制,所以也不需要打开网页就能获取数据!

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

 

我之前写了一个抓取妹子资料的文章,主要是使用selenium来模拟网页操作,然后使用动态加载,再用xpath来提取网页的资料,但这种方式效率不高。

所以今天我再补一个高效获取数据的办法.由于并没有什么模拟的操作,一切都可以人工来控制,所以也不需要打开网页就能获取数据!

但我们需要分析这个网页,打开网页 http://www.lovewzly.com/jiaoyou.html 后,按F12,进入Network项中

url在筛选条件后,只有page在发生变化,而且是一页页的累加,而且我们把这个url在浏览器中打开,会得到一批json字符串,所以我可以直接操作这里面的json数据,然后进行存储即可!

代码结构图:

操作流程

  • headers 一定要构建反盗链以及模拟浏览器操作,先这样写,可以避免后续问题!
  • 条件拼装
  • 然后记得数据转json格式
  • 然后对json数据进行提取,
  • 把提取到的数据放到文件或者存储起来

主要学习到的技术:

  • 学习requests+urllib
  • 操作execl
  • 文件操作
  • 字符串
  • 异常处理
  • 另外其它基础

请求数据:

  1. def craw_data(self): 
  2.         '''数据抓取''' 
  3.         headers = { 
  4.             'Referer''http://www.lovewzly.com/jiaoyou.html'
  5.             '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' 
  6.         } 
  7.         page = 1 
  8.         while True
  9.  
  10.             query_data = { 
  11.                 'page':page, 
  12.                 'gender':self.gender, 
  13.                 'starage':self.stargage, 
  14.                 'endage':self.endgage, 
  15.                 'stratheight':self.startheight, 
  16.                 'endheight':self.endheight, 
  17.                 'marry':self.marry, 
  18.                 'salary':self.salary, 
  19.             } 
  20.             url = 'http://www.lovewzly.com/api/user/pc/list/search?'+urllib.urlencode(query_data) 
  21.             print url 
  22.             req = urllib2.Request(url, headers=headers) 
  23.             response = urllib2.urlopen(req).read() 
  24.             # print response 
  25.             self.parse_data(response) 
  26.             page += 1 

字段提取:

  1. def parse_data(self,response): 
  2.       '''数据解析''' 
  3.       persons = json.loads(response).get('data').get('list'
  4.       if persons is None: 
  5.           print '数据已经请求完毕' 
  6.           return 
  7.  
  8.       for person in persons: 
  9.           nick = person.get('username'
  10.           gender = person.get('gender'
  11.           age = 2018 - int(person.get('birthdayyear')) 
  12.           address = person.get('city'
  13.           heart = person.get('monolog'
  14.           height = person.get('height'
  15.           img_url = person.get('avatar'
  16.           education = person.get('education'
  17.           print nick,age,height,address,heart,education 
  18.           self.store_info(nick,age,height,address,heart,education,img_url) 
  19.           self.store_info_execl(nick,age,height,address,heart,education,img_url) 

文件存放:

  1. def store_info(self, nick,age,height,address,heart,education,img_url): 
  2.         ''
  3.         存照片,与他们的内心独白 
  4.         ''
  5.         if age < 22: 
  6.             tag = '22岁以下' 
  7.         elif 22 <= age < 28: 
  8.             tag = '22-28岁' 
  9.         elif 28 <= age < 32: 
  10.             tag = '28-32岁' 
  11.         elif 32 <= age: 
  12.             tag = '32岁以上' 
  13.         filename = u'{}岁_身高{}_学历{}_{}_{}.jpg'.format(age,height,education, address, nick) 
  14.  
  15.         try: 
  16.             # 补全文件目录 
  17.             image_path = u'E:/store/pic/{}'.format(tag) 
  18.             # 判断文件夹是否存在。 
  19.             if not os.path.exists(image_path): 
  20.                 os.makedirs(image_path) 
  21.                 print image_path + ' 创建成功' 
  22.  
  23.             # 注意这里是写入图片,要用二进制格式写入。 
  24.             with open(image_path + '/' + filename, 'wb'as f: 
  25.                 f.write(urllib.urlopen(img_url).read()) 
  26.  
  27.             txt_path = u'E:/store/txt' 
  28.             txt_name = u'内心独白.txt' 
  29.             # 判断文件夹是否存在。 
  30.             if not os.path.exists(txt_path): 
  31.                 os.makedirs(txt_path) 
  32.                 print txt_path + ' 创建成功' 
  33.  
  34.             # 写入txt文本 
  35.             with open(txt_path + '/' + txt_name, 'a'as f: 
  36.                 f.write(heart) 
  37.         except Exception as e: 
  38.             e.message 

execl操作:

  1. def store_info_execl(self,nick,age,height,address,heart,education,img_url): 
  2.        person = [] 
  3.        person.append(self.count)   #正好是数据条 
  4.        person.append(nick) 
  5.        person.append(u'女' if self.gender == 2 else u'男'
  6.        person.append(age) 
  7.        person.append(height) 
  8.        person.append(address) 
  9.        person.append(education) 
  10.        person.append(heart) 
  11.        person.append(img_url) 
  12.  
  13.        for j in range(len(person)): 
  14.            self.sheetInfo.write(self.count, j, person[j]) 
  15.  
  16.        self.f.save(u'我主良缘.xlsx'
  17.        self.count += 1 
  18.        print '插入了{}条数据'.format(self.count

***展现!

源码地址:https://github.com/pythonchannel/python27/blob/master/test/meizhi.py

责任编辑:武晓燕 来源: 养码场
相关推荐

2017-06-29 11:11:17

2018-03-27 18:12:12

PythonHTML

2020-07-10 09:49:53

数据清理数据分析查找异常

2023-02-08 07:09:40

PythonChatGPT语言模型

2011-12-13 14:44:41

51CTO

2019-11-28 09:23:17

Python机器学习数据库

2020-05-09 10:38:31

Python透视表数据

2018-05-17 10:05:24

运行iPadPython

2020-12-10 10:46:23

PythonExcel图片

2021-03-04 13:40:57

Python文件代码

2018-12-06 08:40:43

PythonR函数编程语言

2019-12-26 09:28:34

TCPPython通信

2020-09-25 08:40:02

Python开发网络

2012-09-19 13:03:00

2020-11-06 17:42:02

Python开发工具

2021-06-02 15:10:20

PythonScrapy视频

2019-10-22 08:42:59

AI 数据人工智能

2019-08-01 15:08:37

PythonLine操作系统

2017-07-20 21:06:44

PythonExcelSQL

2020-11-02 08:15:00

Python数据开发
点赞
收藏

51CTO技术栈公众号