日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python txt转json_实战篇 | 用Python来找你喜欢的妹子(二)

發布時間:2024/7/23 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python txt转json_实战篇 | 用Python来找你喜欢的妹子(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用Python做有趣的事情

最近整理一個爬蟲系列方面的文章,不管大家的基礎如何,我從頭開始整一個爬蟲系列方面的文章,讓大家循序漸進的學習爬蟲,小白也沒有學習障礙.爬蟲篇:使用Python動態爬取某大V微博,再用詞云分析爬蟲篇 | 動態爬取QQ說說并生成詞云,分析朋友狀況爬蟲篇 | 200 行代碼實現一個滑動驗證碼爬蟲篇 | 學習Selenium并使用Selenium模擬登錄知乎爬蟲篇 | Python使用正則來爬取豆瓣圖書數據爬蟲篇 | 不會這幾個庫,都不敢說我會Python爬蟲爬蟲篇 | Python現學現用xpath爬取豆瓣音樂爬蟲篇 |?Python最重要與重用的庫Request爬蟲篇 | Python爬蟲學前普及基礎篇 | Python基礎部分

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

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

所以今天我再補一個高效獲取數據的辦法.由于并沒有什么模擬的操作,一切都可以人工來控制,所以也不需要打開網頁就能獲取數據!

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

url在篩選條件后,只有page在發生變化,而且是一頁頁的累加,而且我們把這個url在瀏覽器中打開,會得到一批json字符串,所以我可以直接操作這里面的json數據,然后進行存儲即可!

代碼結構圖:

操作流程:

headers 一定要構建反盜鏈以及模擬瀏覽器操作,先這樣寫,可以避免后續問題!
條件拼裝
然后記得數據轉json格式
然后對json數據進行提取,
把提取到的數據放到文件或者存儲起來

主要學習到的技術:

  • 學習requests+urllib

  • 操作execl

  • 文件操作

  • 字符串

  • 異常處理

  • 另外其它基礎

  • 請求數據

    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

    如果你覺得文章還不錯,請大家點贊分享下。你的肯定是我最大的鼓勵和支持。【完】如果覺得有料,來個在看,讓朋友知道你越來越優秀了說句題外話,有不少人想加我微信,看我朋友圈的每日分享,我姑且放出來,但名額有限,先來先得。我的朋友圈不止有技術分享,更有我的日常感悟,還有我個人商業思維觀點?速速掃碼添加!掃碼添加,備注:公號鐵粉推薦閱讀

    爬蟲篇:使用Python動態爬取某大V微博,再用詞云分析

    爬蟲篇?|?動態爬取QQ說說并生成詞云,分析朋友狀況爬蟲篇 | 200 行代碼實現一個滑動驗證碼爬蟲篇 | 學習Selenium并使用Selenium模擬登錄知乎爬蟲篇 | Python使用正則來爬取豆瓣圖書數據爬蟲篇 | 不會這幾個庫,都不敢說我會Python爬蟲爬蟲篇 | Python現學現用xpath爬取豆瓣音樂爬蟲篇 |?Python最重要與重用的庫Request爬蟲篇 | Python爬蟲學前普及基礎篇 | Python基礎部分講真,做Python一定不要只會一個方向!喜歡就在

    總結

    以上是生活随笔為你收集整理的python txt转json_实战篇 | 用Python来找你喜欢的妹子(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。